c# - 如何在c#代码中访问c++ dll类

标签 c# winapi dllimport

我的第 3 方 dll 中有一个 C++ 类。

如果我调用 Assembly.LoadFrom(),VS 会抛出未处理的异常,因为模块中不包含 list 。

我可以使用 DllImport 调用全局函数来获取某个类的实例。

然后我如何调用它的成员函数之一?

最佳答案

使用 C++/CLI 公开 C++ 函数创建包装 DLL

例如:

//class in the 3rd party dll
class NativeClass
{
    public:
    int NativeMethod(int a)
    {
        return 1;
    }   
};

//wrapper for the NativeClass
class ref RefClass
{
    NativeClass * m_pNative;

    public:
    RefClass():m_pNative(NULL)
    {
        m_pNative = new NativeClass();
    }

    int WrapperForNativeMethod(int a)
    {
        return m_pNative->NativeMethod(a);
    }

    ~RefClass()
    {
        this->!RefClass();
    }

    //Finalizer
    !RefClass()
    {
        delete m_pNative;
        m_pNative = NULL;
    }
};

关于c# - 如何在c#代码中访问c++ dll类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10620577/

相关文章:

c# - WPF C# 应用程序中的 C++ Dll

C# - C 互操作性

c# - 如何使用 ninject 将单个工厂实例注入(inject)多个存储库和工作单元?

.net - 将 .NET 表单设置为 HWND 的父级

c++ - GetVolumeInformation() 产生不同的磁盘序列号

c# - 即使文件位于 Bin 文件夹中,DLLImport 也不起作用

c# - 单声道(Linux、C#): DLL in same folder can not be found (System. DllNotFoundException)

c# - C# 和 VB.NET 中的文件路径(通过 WCF)

c# - Umbraco:使用 C# 在内容中创建子节点

c - 我需要帮助来使用 DeleteDirectory 和 DeleteFile API 函数删除目录