将 C++ DLL 函数指针导入到 C#

Import C++ DLL Pointers-to-functions to C#

我正在致力于将 C++ DLL 函数指针导入 C#。

我的 C++ 代码:

class __declspec(dllexport) deviceImport
{
public:
    deviceImport();
    virtual ~deviceImport();
    int __stdcall init(char* deviceName); 
    int __stdcall close();
    int __stdcall getDLLVersion(char* value); 

private:
    int handle;
    HINSTANCE hDLLDevice;
    bool __stdcall getProcAddresses( HINSTANCE *p_hLibrary, const char* p_dllName, int p_count, ... ); 
    int (__stdcall *Device_setPassword)(const char* value);
    int (__stdcall *Device_getDLLVersion)(int p_handle, char* value); 

};

我的 C# 代码:

[DllImport(dllLocation, CallingConvention = CallingConvention.Cdecl)]
public static extern int init([MarshalAs(UnmanagedType.LPStr)]string device);

//How to Import Device_setPassword and Device_getDLLVersion functions???

我可以导入 init 函数,但你能帮我导入 Device_setPassword 和 Device_getDLLVersion 函数吗?

我的问题解决了。它可能会帮助别人。解决方案:

[DllImport(dllLocation, CallingConvention = CallingConvention.StdCall)]
public static extern int Device_setPassword(string password);

[DllImport(dllLocation, CallingConvention = CallingConvention.StdCall)]
public static extern int Device_getDLLVersion(int handle, out string value);