我有一个用Visual开发的动态链接库,我已经开始使用C++将它的功能导入到c#项目。我已经实现了一些方法,它们运行得很好。
对于特定的方法,我得到以下错误:
Additional information: A call to PInvoke function 'SdkTest!SdkTest.Program::CLIENT_RealPlay' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.我试图实现的c++方法具有以下签名:
CLIENT_NET_API LLONG CALL_METHOD CLIENT_RealPlay(LLONG lLoginID, int nChannelID, HWND hWnd);定义如下:
#define CLIENT_NET_API __declspec(dllimport)
#define CALL_METHOD __stdcall
#define LLONG LONG我的c#增强如下:
[DllImport("dhnetsdk.dll")]
public static extern long CLIENT_RealPlay(long lLoginID, int nChannelID, IntPtr hWnd);(我读过HWND在c#中的等价物是IntPtr,但我也试着把int,long,object.)
我还尝试以以下方式执行DllImport (正如在某些帖子中所建议的那样,并为我正在使用的其他方法工作):
[DllImport("dhnetsdk.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]不管我怎么尝试,我都会犯同样的错误。我有什么好怀念的?如果抛出c++代码中的内部异常,代码中会出现什么样的异常?
发布于 2017-03-23 11:04:31
#define LLONG LONG现在,LONG映射到long,这是一个在Windows上签名的32位类型。因此,在您的long代码中使用C#是错误的,因为C# long是64位类型。您需要使用int代替。如下所示:
[DllImport("dhnetsdk.dll")]
public static extern int CLIENT_RealPlay(int lLoginID, int nChannelID, IntPtr hWnd);发布于 2017-03-23 09:56:00
c++函数是用调用约定stdcall来声明的,但是您要用cdecl调用它。
在我的经验中,调用堆栈损坏主要是由于使用错误的调用约定造成的。
https://stackoverflow.com/questions/42972542
复制相似问题