【问题标题】:Import C++ DLL Pointers-to-functions to C#将 C++ DLL 指向函数的指针导入 C#
【发布时间】:2021-07-04 03:45:39
【问题描述】:

我正在将 C++ DLL Pointers-to-functions 导入 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 函数吗?

【问题讨论】:

  • 这能回答你的问题吗? using a class defined in a c++ dll in c# code
  • @Blu3Souls 我已尝试按照链接中的方式编组 C++。但是不知道怎么调用解析int (__stdcall *Device_setPassword)(const char* value);

标签: c# c++ dll dllimport


【解决方案1】:

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

[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);

【讨论】:

    猜你喜欢
    • 2020-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多