如何在C#应用程序中DllExport一个C ++类 [英] How do I DllExport a C++ Class for use in a C# Application

查看:189
本文介绍了如何在C#应用程序中DllExport一个C ++类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个包含一个类myCppClass的C ++ Dll项目,并尝试使用以下代码导出Dll:
http://msdn.microsoft.com/en-us/library/a90k134d(v = vs80).aspx

  class __declspec(dllexport)CExampleExport:// public CObject 
{... class definition .. 。};

我省略了public CObject,因为这需要afx.h,意味着它是一个MFC Dll 。我不知道这是否是一件好事,但它与DLL项目的默认设置不同。



从上述链接的文档我被导致相信所有公共功能和成员变量可供导入。如何在C#中实现?可以简单地实例化课程吗?



编辑:我只是意识到帖子的标题可能会产生误导。重点在于C#上的DllImport,并确保我在C ++中正确遵循了文档

解决方案

C#无法直接导入C ++类(它们实际上是名称错误的C接口)。



您的选项是通过COM暴露类,使用C ++ / CLI创建一个托管包装器或暴露C风格接口。我会推荐托管包装,因为这是最简单的,并将提供最好的类型安全。



C风格的界面看起来像这样(警告:未经测试的代码) :

  externC__declspec(dllexport)
void * CExampleExport_New(int param1,double param2)
{
return new CExampleExport(param1,param2);


externC__declspec(dllexport)
int CExampleExport_ReadValue(void * this,int param)
{
return((CExampleExport *)这个) - > ReadValue(param)
}

C ++ / CLI风格的包装看起来像这样(警告:未经测试的代码):

  ref class ExampleExport 
{
private:
CExampleExport * impl;
public:
ExampleExport(int param1,double param2)
{
impl = new CExampleExport(param1,param2);
}

int ReadValue(int param)
{
return impl-> ReadValue(param);
}

〜ExampleExport()
{
delete impl;
}
};


I have created a C++ Dll project which contains a class "myCppClass" and tried to Dll export it using the following code as described by: http://msdn.microsoft.com/en-us/library/a90k134d(v=vs.80).aspx

class __declspec(dllexport) CExampleExport : //public CObject
{ ... class definition ... };

I have omitted the "public CObject" as that requires afx.h and implies it is an MFC Dll. I am not sure if this is a good thing or not but it differed from the DLL project default settings.

From the above linked documentation I am led to believe that all "public functions and member variables" are available for import. How do I accomplish this in C#? Can simply instantiate the class?

Edit: I just realized that the Title of the post may be misleading. The emphasis should be on DllImport-ing from C# and ensuring that I followed the documentation properly in C++

解决方案

C# cannot directly import C++ classes (which are effectively name-mangled C interfaces).

Your options are exposing the class via COM, creating a managed wrapper using C++/CLI or exposing a C-style interface. I would recommend the managed wrapper, since this is easiest and will give the best type safety.

A C-style interface would look something like this (warning: untested code):

extern "C" __declspec(dllexport)
void* CExampleExport_New(int param1, double param2)
{
    return new CExampleExport(param1, param2);
}

extern "C" __declspec(dllexport)
int CExampleExport_ReadValue(void* this, int param)
{
    return ((CExampleExport*)this)->ReadValue(param)
}

A C++/CLI-style wrapper would look like this (warning: untested code):

ref class ExampleExport
{
private:
    CExampleExport* impl;
public:
    ExampleExport(int param1, double param2)
    {
        impl = new CExampleExport(param1, param2);
    }

    int ReadValue(int param)
    {
        return impl->ReadValue(param);
    }

    ~ExampleExport()
    {
        delete impl;
    }
};

这篇关于如何在C#应用程序中DllExport一个C ++类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆