如何从非托管代码中调用托管代码? [英] How to call managed code from unmanaged code?

查看:86
本文介绍了如何从非托管代码中调用托管代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从非托管C ++调用.NET代码。我的流程入口点是基于.NET的,因此我不必担心托管CLR。我知道可以使用.NET对象的COM包装器来完成此操作,但是我想访问托管类的各个静态方法,因此COM不是我最短/最简单的方法。

I want to call my .NET code from unmanaged C++. My process entrypoint is .NET based, so I don't have to worry about hosting the CLR. I know it can be done using COM wrappers for .NET objects, but I would like to access individual static methods of managed classes, so COM isn't my shortest/easiest route.

推荐答案

假定您正在谈论真正的非托管代码-不仅仅是运行在使用/ clr编译的混合模式程序集中的本机C ++-最简单的方法是为.NET创建包装器C ++ / CLI中的代码。然后,您可以通过仅用__declspec(dllexport)标记C ++ / CLI方法来导出它们。

Assuming you are talking about real unmanaged code - not just native C++ running in a mixed-mode assembly compiled with /clr - the easiest way is to create a wrapper to your .NET code in C++/CLI. You can then export the C++/CLI methods by just marking them with __declspec(dllexport).

或者,如果您可以控制非托管代码的调用,则可以将功能指针编组到您的.NET方法,并将它们传递给非托管代码。

Alternatively, if you have control over the invocation of the unmanaged code, you can marshal function-pointers to your .NET methods and pass them to the unmanaged code.

这篇关于如何从非托管代码中调用托管代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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