简体   繁体   English

在C#中导入Visual C ++ DLL时的非托管签名

[英]Unmanaged signature when importing Visual c++ DLL in c#

I have a DLL that was developed in Visual C++, and I've started importing it's functionality to ac# project using DllImport . 我有一个用Visual C ++开发的DLL,我已经开始使用DllImport将其功能导入ac#项目。 I've already implemented a few methods and they work well. 我已经实现了一些方法,并且效果很好。

For that specific method I'm getting the following error: 对于该特定方法,我收到以下错误:

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.

The c++ method I'm trying to implement has this signature: 我尝试实现的c ++方法具有以下签名:

CLIENT_NET_API LLONG CALL_METHOD CLIENT_RealPlay(LLONG lLoginID, int nChannelID, HWND hWnd);

With the following definitions: 具有以下定义:

#define CLIENT_NET_API  __declspec(dllimport)
#define CALL_METHOD     __stdcall
#define LLONG   LONG

My c# impelmentation is the following: 我的C#实现如下:

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

(I've read that HWND equivalent in c# is IntPtr , but I've also tried to put int, long, object...) (我已经读过c#中的HWND等效项是IntPtr ,但我也尝试过将int,long,object ...)

I also tried doing DllImport in the following way (As suggested in some posts and worked for some other methods I'm using): 我还尝试通过以下方式进行DllImport (如某些帖子中所建议,并针对我正在使用的其他方法工作):

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

No matter what I try I'm getting the same error. 无论我尝试什么,都会遇到相同的错误。 What am I miss understanding? 我想念什么? If an internal exception in the c++ code is thrown, what kind of exception will I get in my code? 如果在c ++代码中引发了内部异常,我的代码将得到哪种异常?

#define LLONG   LONG

Now, LONG maps to long , which is a signed 32 bit type on Windows. 现在, LONG映射到long ,这是Windows上的带符号的32位类型。 Therefore, using long in your C# code is wrong because C# long is a 64 bit type. 因此,在C#代码中使用long是错误的,因为C# long是64位类型。 You need to use int instead. 您需要使用int代替。 Like this: 像这样:

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

The c++ function is declared with calling convention stdcall, but you are calling it with cdecl. c ++函数是通过调用约定stdcall声明的,但是您正在使用cdecl调用它。

In my experience, call stack corruptions are mostly caused by using the wrong calling convention. 以我的经验,调用堆栈损坏主要是由使用错误的调用约定引起的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM