PInvoke的和EntryPointNotFoundException

PInvoke的和EntryPointNotFoundException

问题描述:

我不明白什么是错用下面的PInvoke导致成EntryPointNotFoundException:

I can't understand what is wrong with a pinvoke below which results into an EntryPointNotFoundException:

在C中的函数与结构说明:

A function in C with a structure declaration:

    extern "C"__declspec (dllimport) __stdcall
    LONG NET_DVR_Login_V30 (char *sDVRIP,  WORD wDVRPort,  char *sUserName,
                        char *sPassword,  LPNET_DVR_DEVICEINFO_V30 lpDeviceInfo);

    typedef struct
    {
        BYTE sSerialNumber[48]; 
        BYTE byAlarmInPortNum;
        BYTE byAlarmOutPortNum;
        BYTE byDiskNum;
        BYTE byDVRType;
        BYTE byChanNum;
        BYTE byStartChan;
        BYTE byAudioChanNum;
        BYTE byIPChanNum;
        BYTE byZeroChanNum;
        BYTE byMainProto;
        BYTE bySubProto;
        BYTE bySupport;
        BYTE byRes1[20];
    }NET_DVR_DEVICEINFO_V30,  *LPNET_DVR_DEVICEINFO_V30; 

在C#中的进口,结构声明和PInvoke的:

The import in C#, the structure declaration and the pinvoke:

    [DllImport("SDK.dll", SetLastError = true,
        CallingConvention = CallingConvention.StdCall)]
        public extern static int NET_DVR_Login_V30(
            [MarshalAs(UnmanagedType.LPStr)] string sDVRIP,
            ushort wDVRPort,
            [MarshalAs(UnmanagedType.LPStr)] string sUserName,
            [MarshalAs(UnmanagedType.LPStr)] string sPassword,
            ref NET_DVR_DEVICEINFO_V30 lpDeviceInfo);

    [StructLayout(LayoutKind.Sequential,
        CharSet = CharSet.Ansi)]
        public struct NET_DVR_DEVICEINFO_V30
        {
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 48)]
            public string sSerialNumber;
            public byte byAlarmOutPortNum;
            public byte byDiskNum;
            public byte byDVRType;
            public byte byChanNum;
            public byte byStartChan;
            public byte byAudioChanNum;
            public byte byIPChanNum;
            public byte byZeroChanNum;
            public byte byMainProto;
            public byte bySubProto;
            public byte bySupport;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 20)]
            public string byRes1;
        }

           NET_DVR_DEVICEINFO_V30 deviceInfo = new NET_DVR_DEVICEINFO_V30();
           int result = Functions.NET_DVR_Login_V30(ip, port, user,
                                                 password, ref deviceInfo);

我检查通过DUMPBIN函数名和它没有错位。所以,我不知道为什么一个EntryPointNotFoundException发生时,如果有什么是错的参数,例如,会发生PInvokeStackImbalance错误,让我们说。 任何想法可能是错误的这PInvoke的?

I inspected the function name via dumpbin and it is not mangled. So I wonder why an EntryPointNotFoundException occurs, if anything were wrong with the parameters for example, a PInvokeStackImbalance error would occur, let's say. Any ideas what could be wrong with this pinvoke?

有一个名为的Dependency Walker(带的Depends.exe )的工具,这将有助于通过显示调试此问题您SDK.DLL的导入/导出表 - 我会看看那个。是的另一件事可能的(这似乎犯罪嫌疑人对我来说)是发生的事情是,既然你使用的char *,.NET是对你的函数名称后添加一个A。这可能是梦呓虽然。

There is a tool called Dependency Walker (depends.exe) that will help debug this issue by displaying the import/export table of your SDK.DLL - I'd take a look at that. One other thing that might (this seems suspect to me) be happening is, that since you're using char*, .NET is adding an "A" on the end of your function name. That could be balderdash though.