在不使用COM的情况下从C ++调用C#方法

时间:2021-11-23 04:19:22

Is there a way to create C# objects and call methods from unmanaged C++, but without using COM Iterop? I am looking for something like JNI (but for .Net), where you can manually create the VM, create objects, etc.

有没有办法创建C#对象并从非托管C ++调用方法,但没有使用COM Iterop?我正在寻找像JNI(但对于.Net)这样的东西,你可以手动创建VM,创建对象等。

4 个解决方案

#1


6  

If you are using C++/CLI then you can interact directly with both the managed world and unmanaged code, so interop is trivial.

如果您使用的是C ++ / CLI,那么您可以直接与托管世界和非托管代码进行交互,因此互操作是微不足道的。

You can also host the CLR yourself, and whilst the hosting API is COM based, you can then create any managed object. The process isn't a difficult as it sounds as a few API calls encapsulate a lot of functionality. There is a lot of info online, for example the MSDN documentation on "Hosting the Common Language Runtime".

您也可以自己托管CLR,虽然托管API是基于COM的,但您可以创建任何托管对象。这个过程并不困难,因为它听起来像一些API调用封装了很多功能。网上有很多信息,例如关于“托管公共语言运行时”的MSDN文档。

#2


3  

There is a somewhat "undocumented" way of exporting C style API from a .NET class / method.

从.NET类/方法导出C风格API有一种“无法记录”的方式。

This ultimately leads to a situation where a .NET dll has exported APIs that can be called from C/C++ or anything that can consume .DLLs for that matter.

这最终会导致.NET dll导出可以从C / C ++调用的API或任何可以使用.DLL的API。

If you are into "reading" (beh ;) you can get a book called: Inside Microsoft® .NET IL Assembler where you'll find this technique in chapter 15: "Managed Methods as Unmanaged Exports"

如果您正在“阅读”(beh;),您可以获得一本名为:InsideMicrosoft®.NETIL Assembler的书,您将在第15章中找到这种技术:“托管方法作为非托管导出”

There's also a nice example project on code-project you can use as a starting point for 32-bit environments: http://www.codeproject.com/KB/dotnet/DllExport.aspx

还有一个关于代码项目的很好的示例项目,您可以将其用作32位环境的起点:http://www.codeproject.com/KB/dotnet/DllExport.aspx

#3


1  

You can decide file-by-file in your C++ project whether or not to use managed C++. Try changing the settings a file in your project so that it compiles as managed. Put the calls there to your C# object.

您可以在C ++项目中逐个文件地决定是否使用托管C ++。尝试更改项目中的文件设置,以便将其编译为托管。将调用放在C#对象上。

There's a cost to crossing the C++/C# border, so you should analyse where to do it. Like, you wouldn't want to do it inside a loop.

跨越C ++ / C#边界需要花费成本,因此您应该分析在哪里进行操作。就像,你不想在循环中做到这一点。

#4


1  

I am author of jni4net, open source intraprocess bridge between JVM and CLR. It's build on top of JNI and PInvoke. No C/C++ code needed. I hope it will help you.

我是jni4net的作者,jVM和CLR之间的开源内部进程桥。它建立在JNI和PInvoke之上。不需要C / C ++代码。我希望它会对你有所帮助。

#1


6  

If you are using C++/CLI then you can interact directly with both the managed world and unmanaged code, so interop is trivial.

如果您使用的是C ++ / CLI,那么您可以直接与托管世界和非托管代码进行交互,因此互操作是微不足道的。

You can also host the CLR yourself, and whilst the hosting API is COM based, you can then create any managed object. The process isn't a difficult as it sounds as a few API calls encapsulate a lot of functionality. There is a lot of info online, for example the MSDN documentation on "Hosting the Common Language Runtime".

您也可以自己托管CLR,虽然托管API是基于COM的,但您可以创建任何托管对象。这个过程并不困难,因为它听起来像一些API调用封装了很多功能。网上有很多信息,例如关于“托管公共语言运行时”的MSDN文档。

#2


3  

There is a somewhat "undocumented" way of exporting C style API from a .NET class / method.

从.NET类/方法导出C风格API有一种“无法记录”的方式。

This ultimately leads to a situation where a .NET dll has exported APIs that can be called from C/C++ or anything that can consume .DLLs for that matter.

这最终会导致.NET dll导出可以从C / C ++调用的API或任何可以使用.DLL的API。

If you are into "reading" (beh ;) you can get a book called: Inside Microsoft® .NET IL Assembler where you'll find this technique in chapter 15: "Managed Methods as Unmanaged Exports"

如果您正在“阅读”(beh;),您可以获得一本名为:InsideMicrosoft®.NETIL Assembler的书,您将在第15章中找到这种技术:“托管方法作为非托管导出”

There's also a nice example project on code-project you can use as a starting point for 32-bit environments: http://www.codeproject.com/KB/dotnet/DllExport.aspx

还有一个关于代码项目的很好的示例项目,您可以将其用作32位环境的起点:http://www.codeproject.com/KB/dotnet/DllExport.aspx

#3


1  

You can decide file-by-file in your C++ project whether or not to use managed C++. Try changing the settings a file in your project so that it compiles as managed. Put the calls there to your C# object.

您可以在C ++项目中逐个文件地决定是否使用托管C ++。尝试更改项目中的文件设置,以便将其编译为托管。将调用放在C#对象上。

There's a cost to crossing the C++/C# border, so you should analyse where to do it. Like, you wouldn't want to do it inside a loop.

跨越C ++ / C#边界需要花费成本,因此您应该分析在哪里进行操作。就像,你不想在循环中做到这一点。

#4


1  

I am author of jni4net, open source intraprocess bridge between JVM and CLR. It's build on top of JNI and PInvoke. No C/C++ code needed. I hope it will help you.

我是jni4net的作者,jVM和CLR之间的开源内部进程桥。它建立在JNI和PInvoke之上。不需要C / C ++代码。我希望它会对你有所帮助。