C# 在C中导入VisualC++的DLL时的非托管签名 我在VisualC++中开发了一个DLL,并且我已经开始使用DLimPART将它的功能导入到C项目中。我已经实现了一些方法,它们工作得很好

C# 在C中导入VisualC++的DLL时的非托管签名 我在VisualC++中开发了一个DLL,并且我已经开始使用DLimPART将它的功能导入到C项目中。我已经实现了一些方法,它们工作得很好,c#,visual-c++,dllimport,C#,Visual C++,Dllimport,对于该特定方法,我得到以下错误: 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 th

对于该特定方法,我得到以下错误:

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
我的简历如下:

[DllImport("dhnetsdk.dll")]
public static extern long CLIENT_RealPlay(long lLoginID, int nChannelID, IntPtr hWnd);
我读过c中的HWND等价物是IntPtr,但我也试着把int,long,object

我还试着按照一些帖子中的建议,以以下方式进行DllImport,并使用了其他一些方法:

[DllImport("dhnetsdk.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]

不管我怎么做,我都会犯同样的错误。我错过了什么?如果抛出C++代码中的内部异常,我将在代码中得到什么样的异常?

< p> C++函数用调用约定STDCULL声明,但您用CDELL调用它。< /P> 根据我的经验,调用堆栈损坏主要是由于使用了错误的调用约定造成的

#define LLONG   LONG
现在,LONG映射为LONG,在Windows上是一种有符号的32位类型。因此,在C代码中使用long是错误的,因为C long是64位类型。您需要改用int。像这样:

[DllImport("dhnetsdk.dll")]
public static extern int CLIENT_RealPlay(int lLoginID, int nChannelID, IntPtr hWnd);

我已经将CallingConvention=CallingConvention.StdCall添加到dlliport属性中,但结果仍然相同。很抱歉,默认值是StdCall,我错过了C++LONG==C int。