相关疑难解决方法(0)

为什么Cdecl调用在"标准"P/Invoke公约中经常不匹配?

我正在开发一个相当大的代码库,其中C++函数是从C#调用P /.

我们的代码库中有很多调用,比如......

C++:

extern "C" int __stdcall InvokedFunction(int);
Run Code Online (Sandbox Code Playgroud)

使用相应的C#:

[DllImport("CPlusPlus.dll", ExactSpelling = true, SetLastError = true, CallingConvention = CallingConvention.Cdecl)]
    private static extern int InvokedFunction(IntPtr intArg);
Run Code Online (Sandbox Code Playgroud)

我已经搜索了网(在我有能力的范围内),因为为什么存在这种明显的不匹配.例如,为什么C#中有Cdecl,而C++中有__stdcall?显然,这会导致堆栈被清除两次,但是,在这两种情况下,变量都以相同的相反顺序被压入堆栈,这样我就不会看到任何错误,尽管在发生的情况下可能会清除返回信息.在调试期间尝试跟踪?

来自MSDN:http: //msdn.microsoft.com/en-us/library/2x8kf7zx%28v=vs.100%29.aspx

// explicit DLLImport needed here to use P/Invoke marshalling
[DllImport("msvcrt.dll", EntryPoint = "printf", CallingConvention = CallingConvention::Cdecl,  CharSet = CharSet::Ansi)]

// Implicit DLLImport specifying calling convention
extern "C" int __stdcall MessageBeep(int);
Run Code Online (Sandbox Code Playgroud)

再一次,extern "C"C++代码和CallingConvention.CdeclC#中都有.为什么不CallingConvention.Stdcall呢?或者,此外,为什么__stdcallC++中存在?

提前致谢!

c# c++ pinvoke cdecl stdcall

53
推荐指数
2
解决办法
2万
查看次数

标签 统计

c# ×1

c++ ×1

cdecl ×1

pinvoke ×1

stdcall ×1