简体   繁体   English

如何从C代码创建ac#对象?

[英]How can I create a c# object from C code?

I'm using a small C webserver. 我正在使用一个小型的C网络服务器。 I'm doing some of the actual request processing with C# libraries. 我正在使用C#库进行一些实际的请求处理。

I'm using a glue layer which appears to be written in something close to c++ to join the two worlds. 我正在使用胶水层,它似乎是用接近c ++的东西编写的,以加入这两个世界。 It provides a 'public ref class' which I can derive from in the C# world. 它提供了一个'公共参考类',我可以从C#世界中获得它。

If I want to derive several different classes from this in C#, how do I create instances of these derived classes from the C/C++? 如果我想在C#中从中派生出几个不同的类,我该如何从C / C ++创建这些派生类的实例?

What information (the class name? A factory function?) shoud I pass (and and in what form) from the C# to the C/C++ code? 什么信息(类名?工厂函数?)我从C#传递(以及以什么形式)到C / C ++代码?

I would like a solution compatible with .NET 2.0, and I'm using Visual Studio 2008 to create my code. 我想要一个与.NET 2.0兼容的解决方案,我正在使用Visual Studio 2008来创建我的代码。

I have realized several times that people are sometimes not completely aware of the difference between managed and unmanaged code, so I would just like to summarize: 我已经多次意识到人们有时并不完全了解托管代码和非托管代码之间的区别,所以我只想总结一下:

  1. You cannot simply call managed code from a native C++ application. 您不能简单地从本机C ++应用程序调用托管代码。 In order to call it, you will first have to expose your .Net code to COM, and then instantiate a COM object in C++. 为了调用它,首先必须将.Net代码暴露给COM,然后在C ++中实例化COM对象。 This way your native app thinks it is creating a COM object like any other, and .Net is doing all the interop in runtime. 这样,您的本机应用程序认为它正在创建一个COM对象,而.Net正在运行时执行所有的互操作。

  2. Second way is to use managed C++/CLI (which is not native C++, but supports both worlds). 第二种方法是使用托管C ++ / CLI(它不是本机C ++,但支持两个世界)。 This way both C++ and C# apps are managed and can communicate seamlessly. 这样,C ++和C#应用程序都可以进行管理,并且可以无缝通信。 So as long as the "glue layer" is written using managed C++/CLI, you can both work with native and managed data. 因此,只要使用托管C ++ / CLI编写“粘合层”,您就可以使用本机和托管数据。

As your originally mentioned unmanaged C++, then the answer would be to go for 1st solution: expose the managed object to the native world through COM interop. 正如您最初提到的非托管C ++,那么答案就是采用第一种解决方案:通过COM互操作将托管对象暴露给本地世界。 If you don't mind using managed C++/CLI, then you have a simpler solution - you can instantiate managed classes easily (with some changes in syntax you should quickly get used to). 如果您不介意使用托管C ++ / CLI,那么您可以使用更简单的解决方案 - 您可以轻松地实例化托管类(在语法上有一些变化,您应该很快习惯)。

See How to call a managed DLL from native Visual C++ code in Visual Studio.NET or in Visual Studio 2005 . 请参见如何从Visual Studio.NET或Visual Studio 2005中的本机Visual C ++代码调用托管DLL

http://support.microsoft.com/kb/828736 http://support.microsoft.com/kb/828736

Use the #using directive to import your C# assembly into the C++ code. 使用#using指令将C#程序集导入C ++代码。

#using "ThingLib.dll"
using namespace ThingLib;

If you only want the C++ code to be aware of the base class, you'll need to call some kind of factory method. 如果您只希望C ++代码知道基类,则需要调用某种工厂方法。

ThingBase^ thing = myThingFactory.MakeThing(aParameter);

If you want to actually instantiate the derived classes in C++, use the gcnew operator. 如果要在C ++中实际实例化派生类,请使用gcnew运算符。

ThingBase^ thing = gcnew DerivedThing(aParameter);

There's a good summary here of the new C++ language features for talking to managed code: http://msdn.microsoft.com/en-us/library/xey702bw.aspx 这里有一个很好的总结,用于与托管代码交谈的新C ++语言功能: http//msdn.microsoft.com/en-us/library/xey702bw.aspx

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

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