从非托管代码加载混合模式程序集
正如标题所说,我想从非托管代码调用混合模式程序集。
更准确地说,我想动态加载混合模式程序集,然后执行一些静态非托管启动代码,这些代码为 C# 代码注册一些托管 C++ 包装器。
这可能吗(或者我需要嵌入 .Net 运行时或使用 COM?)?
有人已经这样做过并且可以分享一些经验吗?
PS:如果混合模式程序集包含WPF窗口,它会启动吗?
As the title says i want to call a mixed mode assembly from unmanaged code.
To be more precise, i want to load the mixed mode assembly dynamically and then execute some static unmanaged startup code that registers some Managed C++ Wrappers for C# Code.
Is this possible (or do i need to embed the .Net Runtime or use COM?) ?
Has anybody already done this and can share some experience?
PS: If the mixed mode assembly contains a WPF Window will it be started?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要加载并初始化 CLR。是的,创建一个托管类 [ComVisible] 或使用 CorBindToRuntimeEx() 自行托管 CLR 是实现此目的的一种方法。一种非常简单的方法是从 DLL 导出托管函数,C++/CLI 编译器在负责初始化 CLR 的代码中嵌入一个 thunk。非常容易做到,但是当托管代码的接口很胖时,它就不能很好地扩展。
您可以通过将函数指针传递到本机接口来进行修饰。使用 Marshal::GetDelegateForFunctionPointer() 将其转换为托管委托。如果执行此操作,请不要忘记使用 #pragma Managed 包装任何本机声明。
You need to get the CLR loaded and initialized. Yes, making a managed class [ComVisible] or hosting the CLR yourself with CorBindToRuntimeEx() is a way to do this. A very simple way is to export a managed function from your DLL, the C++/CLI compiler embeds a thunk in the code that takes care of initializing the CLR. Very easy to do but it does not scale well when the interface to your managed code is fat.
You could embellish by passing a function pointer to your native interface. Convert it to a managed delegate with Marshal::GetDelegateForFunctionPointer(). Don't forget to wrap any native declarations with #pragma managed if you do this.