简体   繁体   English

从C#中的C ++ DLL P /调用DLL函数

[英]P/invoke DLL functions from C++ dll in C#

I have a problem with invoking a few functions from DLL (SDK of some camera). 我从DLL(某些相机的SDK)调用一些功能时遇到问题。 In source of .dll, there is function: 在.dll的源代码中,有以下功能:

NET_SDK_API LONG CALL_METHOD NET_SDK_Login(char *sDVRIP,WORD wDVRPort,char *sUserName,char *sPassword,LPNET_SDK_DEVICEINFO lpDeviceInfo);

and I am trying to call it from .Net console app with following code: 我正在尝试使用以下代码从.Net控制台应用程序调用它:

[STAThread]
static void Main(string[] args)
{
    long userid = 0;

    _net_sdk_deviceinfo dinfo = new _net_sdk_deviceinfo();
    short port = 6036;

    try
    {

        if (DVR.NET_SDK_Init())
        {
            Console.WriteLine("ok");
            userid = DVR.NET_SDK_Login("192.168.1.132", port, "admin", "123456", out dinfo);
            userid.ToString();
        }
        else
        {
            Console.WriteLine("err");
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.ToString());
    }
    Console.ReadKey();

}

i get following error: 我得到以下错误:

A call to PInvoke function 'DVRtest!DVRtest.DVR::NET_SDK_Login' has unbalanced the stack. 调用PInvoke函数'DVRtest!DVRtest.DVR :: NET_SDK_Login'会使堆栈不平衡。 This is likely because the managed PInvoke signature does not match the unmanaged target signature. 这可能是因为托管PInvoke签名与非托管目标签名不匹配。 Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature. 检查PInvoke签名的调用约定和参数是否与目标非托管签名匹配。

Init passes fine but i cant get anything else. 初始化很好,但是我什么也做不了。 I tried dozen of solutions and I'm getting nowhere. 我尝试了许多解决方案,但是却一无所获。

Here is source of dll and source of my .Net app. 是dll的源代码 ,也是我的.Net应用程序的源代码 Thanks! 谢谢!

[edit] [编辑]

As @david pointed out, CallingConvention was wrong and now, i get following error: 正如@david指出的那样,CallingConvention是错误的,现在,我得到以下错误:

The runtime has encountered a fatal error. 运行时遇到致命错误。 The address of the error was at 0x6fda02c7, on thread 0x2554. 错误的地址在线程0x2554上的0x6fda02c7处。 The error code is 0xc0000005. 错误代码为0xc0000005。 This error may be a bug in the CLR or in the unsafe or non-verifiable portions of user code. 此错误可能是CLR或用户代码中不安全或不可验证部分的错误。 Common sources of this bug include user marshaling errors for COM-interop or PInvoke, which may corrupt the stack. 该错误的常见来源包括COM-interop或PInvoke的用户封送处理错误,这些错误可能会破坏堆栈。

Is this error from DLL or CLR (.Net)? 是DLL还是CLR(.Net)的错误? I never imported any functions from DLL to .Net, so any help is appreciated. 我从未将任何功能从DLL导入到.Net,因此不胜感激。

From the unmanaged source: 从非托管来源:

#define CALL_METHOD __stdcall

And from the managed source: 并从托管来源:

[DllImport("DVR_NET_SDK.dll", CallingConvention = CallingConvention.Cdecl)]

Your calling conventions do not match. 您的通话约定不匹配。


As for the edit to the question, that is presumably because the C# struct definition does not match the unmanaged struct. 至于对问题的编辑,大概是因为C#结构定义与非托管结构不匹配。 You have failed to translate any of the arrays correctly. 您未能正确转换任何数组。 They will require the use of [MarshalAs(UnmanagedType.ByValArray, SizeConst=...)] . 他们将需要使用[MarshalAs(UnmanagedType.ByValArray, SizeConst=...)]

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

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