简体   繁体   English

在c#中使用托管c ++ dll

[英]using managed c++ dll in c#

**unmanaged class**

this is the unmanaged class declaration 这是非托管类声明

#ifdef EXPORT_CLASS
#define DLL_EXPORT __declspec(dllexport)
#else
#define DLL_EXPORT __declspec(dllimport)
#endif
public class DLL_EXPORT cppclass
{
private:
string x;
public:
cppclass();
~cppclass();
string native();
};


**UNMANAGED CLASS DEFINITION**

this is the unmanaged class definition 这是非托管类定义

 cppclass::cppclass()
{
x="hello";
};
cppclass::~cppclass()
{
};
string cppclass::native()
{
return x;
};

**MANAGED CLASS**

this is the managed class declaration 这是托管类声明

public __gc class Mclass
{
//private:
public:
cppclass * obj;
public:
Mclass();
~Mclass();
string native();

}; };

**MANAGED CLASS DEFINITION**

//this is the managed class definition //这是托管类定义

#include"managed.h"
Mclass::Mclass()
{
    obj=new cppclass();
};
Mclass::~Mclass()
{
    delete obj;
};
string Mclass::native() 
{
return  obj->native();
};

Now all this is made into a dll and imported in a c# project


using managed;

 namespace ConsoleApplication1
 {
 class Program
 {
    static void Main(string[] args)
    {
        managed.Mclass first = new Mclass();
        String x=first.nativ();
        Console.Out.WriteLine(x);
    }

}

} }

error comes that Managed.Mclass.nativ() is not supported by the language 错误来自该语言不支持Managed.Mclass.nativ()

You are returning a native string from your C++/CLI wrapper class. 您正在从C ++ / CLI包装器类返回本机字符串。 You need to return a managed .net string instead. 您需要返回托管的.net字符串。 The wrapper class must translate parameters and return values of native classes to appropriate managed classes. 包装类必须转换参数并将本机类的值返回到适当的托管类。

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

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