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

标签 c# c++ dll dllimport

我正在致力于将 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);

关于c# - 将 C++ DLL 函数指针导入到 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67001163/

相关文章:

c# - 将列表值拆分为逗号分隔的字符串

c++ - 使用类型特征为字符串迭代器专门化模板函数

c - 如何链接 C 包以便在程序中使用它们?

java - 无法添加 Eclipse 中 JNI 文件夹中的所有类文件(JAVA、Windows 7)

c# - 使用c#并行下载多个文件

c# - 在 C# 中调用 C++ 库

c++ - 从整数 vector 列表中删除重复项的快速方法

c++ - 使用回调开关对 vector 进行C++冒泡排序

windows - 如何检查 DLL 依赖?

c# - 创建IQueryable <T>的通用过滤器,其中该过滤器使用ISomeInterface中定义的属性