C#非托管DLL导出(工作方式) [英] C# unmanaged dll exporting (how it works)

查看:176
本文介绍了C#非托管DLL导出(工作方式)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现可以将托管代码导出为非托管的库,因此您可以通过非托管语言(例如C/C ++)使用它.但是我没有找到任何可以解释其完成方式的信息(这是我更感兴趣的)

I have found libraries that would export managed code as unmanaged so you can use it by unmanaged languages like C/C++. But I did not find anything that would explain how it's done (which is what I'm more interested in)

我正在寻找信息,教程,文章,代码源或任何可以帮助我了解其工作原理的东西

I'm looking for information, tutorials, articles, code sources or anything that could help me to understand how this works

在旁注中,如果您在书签中找到了一些弯钩/弯路资源,我也想阅读它们:)

On a side note, if you found some hooks/detours resources in your bookmarks I would love to read them too :)

预先感谢,祝您有美好的一天.

Thanks in advance and have a wonderful day.

推荐答案

我将发布答案,收集我写的评论.

I'll post an answer, collecting the comments I wrote.

最著名的图书馆(如今天)是 UnmanagedExports .其页面为 https://sites.google.com/site/robertgiesecke/Home/上传/不受管理的导出.遗憾的是,没有可用的源代码,但是它已获得MIT许可,因此可以使用IlSpy进行查看.

The most famous library for doing it is (as today) the UnmanagedExports. Its page is https://sites.google.com/site/robertgiesecke/Home/uploads/unmanagedexports . Sadly there is no source code available, but it is licensed under the MIT license, so it is probably ok to use IlSpy to take a look at it.

有一些有关其完成方式的参考.

There are a few references about how it is done.

关于Codeproject至少有两篇文章:

There are at least two articles on Codeproject: How to Automate Exporting .NET Function to Unmanaged Programs that seems to be for .NET 2.0 and Unmanaged code can wrap managed methods that sadly is about .NET 1.1.

在第384页附近的专家.NET 2.0 IL汇编器中有一些参考.

There is some reference in the book Expert .NET 2.0 IL Assembler around page 384.

您当然可以做另一件事:观察 UnmanagedExports 的作用: UnmanagedExports 最后由两部分组成":装配体 RGiesecke.DllExport.Metadata.dll 包含一个愚蠢"属性 DllExportAttribute 以及两个程序集(在我的计算机上,nuget安装在 packages \ UnmanagedExports.1.2.6 \ tools 中文件夹): RGiesecke.DllExport.dll RGiesecke.DllExport.MSBuild.dll . UnmanagedExports 的nuget安装程序向csproj添加了一些行,例如:

You can surely do another thing: observe what UnmanagedExports do: UnmanagedExports in the end is "composed" of two parts: an assembly RGiesecke.DllExport.Metadata.dll containing a "stupid" attribute DllExportAttribute plus two assemblies (that on my computer the nuget installs in the packages\UnmanagedExports.1.2.6\tools folder): RGiesecke.DllExport.dll and RGiesecke.DllExport.MSBuild.dll. The nuget installer of UnmanagedExports adds some lines to the csproj, like:

<Import Project="packages/UnmanagedExports.1.2.6/tools/RGiesecke.DllExport.targets" Condition="Exists('packages/UnmanagedExports.1.2.6/tools/RGiesecke.DllExport.targets')" />

导致执行程序集 RGiesecke.DllExport.MSBuild.dll 中包含的类 RGiesecke.DllExport.MSBuild.DllExportAppDomainIsolatedTask

.此类使用 Mono.Cecil 重写了程序集,进行了一些代码重组.此程序仅调用 ildasm 生成il代码源,修改il代码源,然后使用 ilasm 生成回原始" .dll/.EXE文件 .简而言之,生成两个程序集,一个程序集带有< Import/> ,另一个程序集带有< Import/> 被注释掉,然后执行

that cause the execution of the class RGiesecke.DllExport.MSBuild.DllExportAppDomainIsolatedTask contained in the assembly RGiesecke.DllExport.MSBuild.dll. This class, using Mono.Cecil, rewrites the assembly doing some code reweaving. This program simply calls ildasm to generate the il code source, modifies the il code source and then uses ilasm to generate back the "original" .dll/.exe . So simply put, generate two assemblies, one with the <Import /> and another with the <Import /> commented out, then do a

ildasm yourdll.dll /out=source.il

两个文件,然后将其与您喜欢的文件比较器进行比较.

of both files and compare it with your favourite file comparer.

另一个有趣的链接是

Another interesting link is here. There are some comments to how to make it work on x64.

如果我必须构建类似的东西,我可能会尝试将其集成到 Fody .这样,我将免费获得整个构建后任务(因为它是由Fody完成的)

If I had to build something similar, I would probably try to integrate it to Fody. In this way I would have the whole post-build Task for free (because it is done by Fody)

无法使用 Mono.Cecil ... Mono.Cecil 无法编写混合模式程序集(导出符号所需).您必须使用UnmanagedExports(以及其他各种示例)所使用的相同技巧" ...生成IL文件,对其进行修改(它是固定格式的文本文件...非常容易修改),重新-生成.dll/.exe.

Can't be done with Mono.Cecil... Mono.Cecil can't write mixed-mode assemblies (that are needed to export symbols). You have to use the same "trick" used by UnmanagedExports (and by the various other examples)... Generate the IL file, modify it (it is a text file in a fixed format... quite easy to modify), re-generate the .dll/.exe .

这篇关于C#非托管DLL导出(工作方式)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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