将本机C ++ dll和c#程序集组合成一个dll/程序集 [英] Combining native C++ dll and c# assembly into a single dll/assembly

查看:98
本文介绍了将本机C ++ dll和c#程序集组合成一个dll/程序集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个简单的本机C ++ dll(根本没有clr/托管代码),它创建一个AppDomain并使用ICorRuntimeHost提供的COM接口将C#程序集加载到其中.到目前为止,该方法没有任何问题.

问题:是否可以将C ++ dll和C#程序集链接到一个文件中?这意味着,该文件必须可由clr-wareware应用程序通过Win32的LoadLibrary调用,并且必须同时为有效的.net程序集.

CP

附注:为澄清起见,我不想做IJW之类的事情,例如#pragma(un)managed或COM-Interop.通过CorBindToRuntimeEx-> CreateDomain-> CreateInstance调用程序集的方式保持不变.应该更改的是,我希望有一个dll文件或汇编文件,而不是两个单独的文件.

Hi,

I have a simple native C++ dll (no clr/managed code at all) creating an AppDomain and loading an C#-assembly into it using COM-Interfaces supplied by ICorRuntimeHost. This works so far without any problem.

The question: Is it possible to link the C++ dll and the C# assembly into one single file? It would mean, this file must be callable by a clr-unaware app via Win32''s LoadLibrary and must be a valid .net assembly at the same time.

CP

P.S.: For clarification, I do not want to do IJW-things like #pragma (un)managed or do COM-Interop. The way of invoking the assembly via CorBindToRuntimeEx->CreateDomain->CreateInstance stays the same. What should be changed is that I want to have a single dll- or assembly-file instead of two separate files.

推荐答案

找到了解决方案:

-创建程序集的netmodule版本
-只需将此netmodule添加到dll项目文件中链接器的其他依赖项",然后进行编译
-将.NET2.0-Assembly与VS2010链接时,在常规属性"中设置正确的平台工具集".

用PEBrowse检查dll会发现有一个新的".NET MetaData(2.0)"部分.
您可以在正常导出(带有depends.exe的dll的)旁边看到带有.NET的类,以及带有Redgate .NET Reflector的类.

dll的本机代码部分将引导自定义的应用程序域,并现在以该程序域内部的程序集本身(以前已经完成)加载.没有更多的程序集或版本不匹配,可以进行故障安全部署了……
Found the solution:

- Create a netmodule version of the assembly
- Simply add this netmodule to linker''s "Additional Dependencies" in your dll project file and compile
- Set correct "Platform Toolset" in "General Properties" when linking .NET2.0-Assembly with VS2010.

Examining the dll with PEBrowse tells that there is a new ".NET MetaData (2.0)"-section.
You can see beside the normal exports (of the dll with depends.exe) the .NET classes etc. with Redgate .NET Reflector.

The native-code part of the dll bootstraps a customized appdomain and loads now itself (which was already done by before) as assembly inside that appdomain. No more missing assemblies or mismatched versions, garantueed failsafe deployment...


可能吗?我认为是(但不确定).
我想我看过一篇有关从C#导出本机方法的文章,但您一步之不来.您必须先编译,然后反汇编,以某种方式编辑MSIL代码,然后重新编译...我不记得确切了,我甚至不确定它!
但是,即使有可能,这也不容易.

进行混合代码汇编的最佳方法是使用C ++/CLI.
使用C ++/CLI,您可以导出本机代码和托管代码.

您可以做一个引用本机C ++ dll和C#程序集的包装器.但是您将拥有3个dll.
如果只想拥有1个DLL.然后,您需要将两个项目中的代码转换为C ++/CLI代码.
Possible? I think it is (but not sure).
I think I saw a post about exporting native methods from C# but you can''t do it in one step. You must first compile, then disassamble, edit the MSIL code in some way, then recompile... I don''t remember exactly and I am not even sure of it!!
But even though it would be possible, it is not easy.

The best way to do a mixed code assembly is using C++/CLI.
With C++/CLI you can export both native and managed code.

You can do a wrapper that references the native C++ dll and the C# assembly. But you would then have 3 dlls.
If you want to have only 1 DLL. Then you would need to convert the codes from both projects into C++/CLI code.


这篇关于将本机C ++ dll和c#程序集组合成一个dll/程序集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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