简体   繁体   English

如何从C#导入和使用非托管C ++类?

[英]How to import and use a unmanaged C++ class from C#?

I have an native C++ dll, some header files and the import library. 我有一个原生的C ++ DLL,一些头文件和导入库。 Is there a way how to instantiate an object within C# that is defined in the dll? 有没有办法如何在dll中定义的C#中实例化对象?

The two ways I'm aware of are: 我所知道的两种方式是:

  1. to wrap the C++ code into COM 将C ++代码包装到COM中
  2. to use DLLImport and external C functions 使用DLLImport和外部C函数

C++/CLI is your friend for this. C ++ / CLI是你的朋友。 You'll run into one problem though: it is not possible to store standard C++ objects inside C++/CLI ref or value classes (the ones for .NET). 但是你会遇到一个问题:无法在C ++ / CLI ref或value类(.NET的那些)中存储标准C ++对象。 So you'll have to resort to the following class (that you can modify) that I use in production code: 因此,您将不得不求助于我在生产代码中使用的以下类(您可以修改):

#pragma once
#include <boost/shared_ptr.hpp>

template <typename T>
ref class Handle
{
    boost::shared_ptr<T>* t;

    !Handle() 
    {
        if (t != nullptr)
        {
            delete t;
            t = nullptr;
        }
    }

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

public:
    Handle() : t(new boost::shared_ptr<T>((T*)0)) {}

    Handle% operator=(T* p)
    {
        if (p != t->get()) t->reset(p);
        return *this;
    }

    static T* operator&(Handle% h) { return h.t->get(); }
    static boost::shared_ptr<T> operator->(Handle% h) { return *h.t; }

    T& reference() { return *t->get(); }
    T const& const_reference() { return *t->get(); }
};

Usage: Handle<MyCppClass>^ handle; 用法: Handle<MyCppClass>^ handle; inside a C++/CLI class. 在C ++ / CLI类中。 You then implement stub methods, forwarding them to the handle member. 然后,您实现存根方法,将它们转发到handle成员。 Garbage collected objects will call destructors of the C++ class instance iff there is no more pointer to it: 垃圾收集对象将调用C ++类实例的析构函数iff没有更多指向它的指针:

public ref class Foo
{
    void bar() { handle->bar(); }

internal:
    Handle<CppNamespace::Foo>^ handle;
};

I think that your option is only build a C++/CLI class wrapper (so you can reference it like ac# class), otherwise you can't instantiate a c++ class (unmanaged) from c# code. 我认为您的选择只是构建一个C ++ / CLI类包装器(因此您可以像ac#类一样引用它),否则您无法从c#代码实例化c ++类(非托管)。

Alternative could be the "ugly" way: instantiate the c++ class through ac function, but you'll treat the class as a void pointer inside the c# code, so you basically will not do anything with it (except if you create other functions to interact with this class; only C functions) 替代方案可能是“丑陋”的方式:通过ac函数实例化c ++类,但是你会把类视为c#代码中的void指针,所以你基本上不会对它做任何事情(除非你创建其他函数到与这个类交互;只有C函数)

C# understands C, if you want make it understand C++ you have to use C++/CLI C#理解C,如果你想让它理解C ++,你必须使用C ++ / CLI

PS C# has some basic understanding of C++ classes, but it's only about converting class data (I mean bytes: fields) into some usable data in C# (there are some Attributes), but will not allow to work with methods and things like that (avoid totally is my suggestion). PS C#对C ++类有一些基本的了解,但它只是将类数据(我的意思是字节:字段)转换为C#中的一些可用数据(有一些属性),但不允许使用类似的方法和事物(完全避免是我的建议)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM