差异CLR和CLI和如何调用那些纯C差异、CLR、CLI

2023-09-04 05:48:00 作者:风烛人家

有人能告诉我是什么CLR和CLI之间的区别是它的基本functionallity,但最重要的是哪一个更好呢?

Could somebody tell me what the difference between CLR and CLI is in it's basic functionallity but most important would be which one of them is better?

所有我走到这一步,是使用 ICLRRuntimeHost 接口不允许我回去别的比 INT 而只允许参数是 LPCWSTR (见的 ExecuteInDefaultAppDomain )

All I got so far is that using the ICLRRuntimeHost Interface does not allow me to return anything else than an int and the only allowed parameter is a LPCWSTR (see ExecuteInDefaultAppDomain)

在这一点上我想知道,如果一个人能/会分配内存,例如为结构在他的C程序,给一个指针作为 LPCWSTR字符串 ExecuteInDefaultAppDomain 键,转换这个指针回到该结构在对方上班的结构。我不明白,怎么可以用这种signator每个功能的限制工作: INT fncname(字符串参数);

At this point I am wondering if one could/would allocate memory e.g. for a struct in his C program, give a pointer as a LPCWSTR string to ExecuteInDefaultAppDomain and translates this pointer back into that struct on the other side to work on that struct. I don't get it how one can work with the limitation of this signator for each function: int fncname(string param);

我接过一看的这个的教程,但我不明白它是如何从一个C程序的作品调用C ++ / CLI,我怎么可以返回复杂的对象或任何东西比一个更复杂的 INT

I took a look at this tutorial but I don't get it how calling C++/CLI from a C program works and how I could return complex objects or anything more complex than an int.

和可能有人请告诉我如何,我可以使用 CLI C之内?我似乎无法找到code一些线路,将展示如何将工作。

And could somebody please show me HOW I can use CLI within C? I can't seem to find some lines of code that would show how that would work.

推荐答案

您可以通过在命令行返回复杂的数据。 CLI是一个中间层。当您从管理code(.NET)到本机code传递数据(C,C ++),你需要一个中间层,将采取管理,以原生对象,反之亦然谈话的责任,因为管理对象由管理垃圾收集器和本地对象由程序员管理(需要删除创建时)。

You can pass and return complex data in CLI. CLI is an intermediate layer. when you pass data from a managed code(.NET) to native code (c,c++) you need a intermediate layer which will take responsibility of managed to native object and vice verse conversation because managed objects are managed by Garbage collector and native objects are managed by programmer (need to delete when created).

在C ++ / CLI有两种类型的类。 1)Manged类2)原生类。托管类的定义为:

In C++/CLI there are two type of class. 1) Manged class 2) Native class. A managed class is defined as :

public ref class ManagedClass
{
NativeObject* native;
ManagedObject^ mObject;
}

这可以包含管理和本地对象。因此,在这个类创建托管/本机类对象,它是原生/管理对象的副本(包含数据和方法方面)。中的任何对象的基本数据是另一对象或基本的原始数据,可以很容易地转换。你只取对象的照顾。

This can contain managed and native object. So in this class you create a managed/native class object which is replica of native/managed object (in terms of containing data and method). The basic data in any object is either another object or basic primitive data which can be easily converted. You have to take care of objects only.

假设你想要执行的C ++方法的形式.NET。你的C ++ code有一个名为NativeClass一个对象,你的C ++方法返回这个原生对象。你不能直接把这个对象传递到.NET层。所以,你会存储对象实例中的 ManagedClass 的上述类变量本地。请注意,您可以访问本地以及在CLI层管理对象。现在,您创建的CLI一manged对象类,这将是本机类中包含的数据而言翻版。现在你管理的对象实例复制的原生对象为管理对象的所有日期,并分配给变量 mObject 。现在,你可以通过这个变量的 mObject 以.NET层,因为这是Manged对象。

Suppose you want to execute C++ method form .NET. Your c++ code has an object called NativeClass and your c++ method return this native object. You can't pass this object directly to .NET layer. So you will store that object instance in variable native of above class ManagedClass. Note that you can access native as well as managed object in CLI layer. Now you create a manged object class in CLI which will be exact replica of native class in term of containing data. Now you copy all date from Native object to managed object and assign that managed object instance to variable mObject. Now you can pass this variable mObject to .NET layer because this is Manged object.

当你通过管理对象到本机的方法类似的方法可以使用​​。

Similar approach can be used when you pass Managed object to Native method.

您可以通过这个PDF了解更多关于C ++ / CLI。

You can go through this PDF to know more about C++/CLI.

http://asawicki.info/Download/Productions/Publications/CPP_CLI_tutorial.pdf

更新:

我会建议你先通过PDF。下面是一个简单的例子,这是如何发生转换。 NativeObject 是C ++对象(你也可以有ç结构 instaed类)和 ManagedObject 是C#对象和 CLIInterface 用于提供接口

I will recommend you to first go through the PDF. Below is a simple example how this conversion happens. NativeObject is C++ object (You can also have C struct instaed of Class) and ManagedObject is C# object and CLIInterface is used to provide interface.

public ref class CLIInterface
{
private:
    NativeObject* native;
    ManagedObject^ mObject;
public:
    CLIInterface(NativeObject* nativeObj){
        native = nativeObj;
        mObject = gcnew ManagedObject(nativeObj);
    }
    string getNativeMessage();
    int getNaiveID();
    String^ getManagedMessage();
    int getmanagedID();
}

本机类:(C ++对象或C结构,可以在命令行中使用)

Native class:(C++ Object or C struct and can be used in CLI)

public class NativeObject
{
private:
    int id;
    string msg;
public:
    string getMessage(){return msg;}
    int getID(){return id;}
}

管理类:(C#对象,你可以在命令行使用它)

Managed class: (C# object and you can use it in CLI)

public ref class ManagedObject
{
private:
    NativeObject* native;
public:
    ManagedObject(NativeObject* obj){
        native = obj;
    }
    String^ getMessage(){
        convertNativeToCLI(native->getMessage()); //you can use marshaling to implement convertNativeToCLI method. 
    }
    int getID(){
        return native->getID();
    }
}

您可以使用托管类从C#code的对象和CLIInterface,你可以在C ++ code。使用本机对象。我希望这会有所帮助。您也可以到微软的C ++ / CLI文档。

You can use Managed class object and CLIInterface from C# code and you can use Native Object in C++ code. I hope this will help. You can also go to Microsoft's C++/CLI documentation.