如何查明 .NET 程序集是否包含非托管代码?

发布于 2024-08-15 21:06:43 字数 100 浏览 2 评论 0原文

包含托管和非托管代码混合的 .NET 程序集不能与其他程序集进行 ILMerged。

如何验证给定的 .NET 程序集是否包含纯托管代码,或者托管代码和非托管代码的混合?

.NET assemblies that contain a mixture of managed and unmanaged code cannot be ILMerged with other assemblies.

How can I verify if a given .NET assembly contains purely managed code, or a mix of managed and unmanaged code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

笑忘罢 2024-08-22 21:06:43

正如 nobugz 所建议的,查看 CLR 标志的一种更简单的方法是使用 corflags 实用程序,它是 .NET 2.0 SDK 的一部分。

如果未指定选项,则显示给定图像的标志:

C:\>corflags Foo.dll
Version   : v2.0.50727
CLR Header: 2.5
PE        : PE32
CorFlags  : 9
ILONLY    : 1
32BIT     : 0
Signed    : 1

“ILONLY”位指示这是纯托管程序集还是混合程序集。

请注意,用户“nobugz”的评论表明这些标志不能保证正确,因此此方法可能并非万无一失。

As suggested by nobugz, an easier way to see the CLR Flags is using the corflags utility, which is part of the .NET 2.0 SDK.

If no options are specified, the flags for the given image are displayed:

C:\>corflags Foo.dll
Version   : v2.0.50727
CLR Header: 2.5
PE        : PE32
CorFlags  : 9
ILONLY    : 1
32BIT     : 0
Signed    : 1

The "ILONLY" bit indicates whether this is a pure managed assemby, or a mixed assembly.

Note that the comment from user 'nobugz' suggests these flags are not guaranteed to be correct, so this method may not be foolproof.

滥情哥ㄟ 2024-08-22 21:06:43

针对程序集运行 PEVerify 工具。

PEVerify.exe 与 Visual Studio 一起安装,例如,该程序随 Visual Studio 2012 一起安装:

C:\Program Files (x86)\Microsoft SDKs\Windows\v8.0A\bin\NETFX 4.0 Tools\PEVerify.exe< /代码>

Run the PEVerify tool against your assembly.

PEVerify.exe is installed along with Visual Studio, e.g. this one comes with Visual Studio 2012:

C:\Program Files (x86)\Microsoft SDKs\Windows\v8.0A\bin\NETFX 4.0 Tools\PEVerify.exe

⊕婉儿 2024-08-22 21:06:43

从 Visual Studio 命令提示符运行 ildasm,如下所示:

ildasm file.exe /headers /noil /text

在输出末尾,您将看到以下内容:

// ----- CLR 标头:
// 标头大小:...
// 主要运行时版本:...
// 次要运行时版本: ...
// ...
// 标志:0x00000000

如果 Flags 设置了最低位(例如 0x00000001),那么该程序集是纯 CLR 的;如果不是(例如 0x00000000),则该程序集为混合模式。请注意,可能还存在其他标志,因此它只是您感兴趣的最低位(因此,如果最后一位数字是 1、3、5、7、9、b、d 或 f,那么它是纯 CLR)。

(编辑:您还可以以图形方式运行 ildasm,打开有问题的可执行文件,然后从“视图”菜单中选择“标头”以查看相同的信息。)

Run ildasm from a Visual Studio Command Prompt as follows:

ildasm file.exe /headers /noil /text

Towards the end of the output you will see the following:

// ----- CLR Header:
// Header size: ...
// Major runtime version: ...
// Minor runtime version: ...
// ...
// Flags: 0x00000000

If Flags has the lowest bit set (e.g. 0x00000001) then the assembly is pure CLR; if not (e.g. 0x00000000) then the assembly is mixed mode. Note that other flags may be present, so it's only the lowest bit you're interested in (so if the last digit is 1, 3, 5, 7, 9, b, d or f then it's pure CLR).

(Edit: You can also run ildasm graphically, open the executable file in question, and choose Headers from the View menu to see the same information.)

定格我的天空 2024-08-22 21:06:43

改进 Wim 上面提供的答案...

  1. 找到你的“PEVerify.exe” - 如果你安装了 VS,你就拥有它。- 将完整路径复制到 PEVerify.exe 文件,你的路径将会不同 - - 这里是一个例子:C:\Program Files (x86)\Microsoft SDKs\Windows\v8.0A\bin\NETFX 4.0 Tools\PEVerify.exe

  2. 打开 Visual Studio 命令提示符(不要“以管理员身份运行”)

  3. 输入:cd C: \Program Files (x86)\Microsoft SDKs\Windows\v8.0A\bin\NETFX 4.0 Tools

  4. 你的命令提示符现在应该是这么长的文本,而不仅仅是 C:\ 或其他东西......它将指向 PEVerify.exe 所在的路径.

  5. 现在输入:peverify“您要检查的 dll 的完整路径 - 然后按 Enter - 这是我的示例:peverify“G:\TestFolder\My_Managed_OR_Unmanagement.dll”

  6. 之后按 Enter 键,您会收到如下消息,其 100% 托管 dll - My_Managed_OR_Unmanagement.dll 中的所有类和方法已验证。"

Improving on the answer provided above Wim...

  1. Locate your "PEVerify.exe" - you have it if you have VS installed.- COPY THE FULL PATH TO THE PEVerify.exe file, your path will be different - - Here's an example: C:\Program Files (x86)\Microsoft SDKs\Windows\v8.0A\bin\NETFX 4.0 Tools\PEVerify.exe

  2. Open Visual Studio Command prompt (don't "Run as Administrator")

  3. Type in: cd C:\Program Files (x86)\Microsoft SDKs\Windows\v8.0A\bin\NETFX 4.0 Tools

  4. your command prompt should now be this long text instead of just C:\ or something....it will point to the path of where PEVerify.exe is located.

  5. Now type in: peverify "your full path to your dll you want to check - THen Press Enter - Here is my example : peverify "G:\TestFolder\My_Managed_OR_Unmanaged.dll"

  6. After you press Enter and you get a message as below, its 100% managed dll - All Classes and Methods in My_Managed_OR_Unmanaged.dll Verified."

痴者 2024-08-22 21:06:43

要从 C# 获取 PE 标志,请使用 System.Reflection API:http://www. example8.com/category/view/id/6027

...

Assembly a = Assembly.GetExecutingAssembly();
Module m = a.ManifestModule;

PortableExecutableKinds peKinds;
ImageFileMachine imageFileMachine;
m.GetPEKind(out peKinds, out imageFileMachine);

if ((peKinds & PortableExecutableKinds.ILOnly) != 0)

...

To get the PE flags from C#, use the System.Reflection API: http://www.example8.com/category/view/id/6027

...

Assembly a = Assembly.GetExecutingAssembly();
Module m = a.ManifestModule;

PortableExecutableKinds peKinds;
ImageFileMachine imageFileMachine;
m.GetPEKind(out peKinds, out imageFileMachine);

if ((peKinds & PortableExecutableKinds.ILOnly) != 0)

...

紧拥背影 2024-08-22 21:06:43

我必须仔细检查这一点,但我很确定您可以使用 redgate 的 Reflector 找到这一点。

I'll have to double check this but I am pretty sure you can find that out with Reflector by redgate.

眼泪都笑了 2024-08-22 21:06:43

ILMerge 仅合并托管程序集,这里,引用他们的下载页面,“ILMerge 是一个用于将多个 .NET 程序集合并为单个 .NET 程序集的实用程序”。

我还没有看到托管程序集与本机二进制文件合并。从技术上讲,您可以通过将非托管二进制文件作为嵌入式资源包含进来,将它们本身合并,但是将嵌入式资源作为二进制代码加载到内存中 - 我以前没有见过这种情况。我尝试过使用内存映射的技术,但失败了。

另一种检查方法是查看二进制文件本身,如果它在数据目录中具有第 15 个条目,并且非零,那么它是 .NET 二进制文件,本机二进制文件没有这个。请参阅此处,我在其中发布了类似问题的答案。

希望这有帮助,
此致,
汤姆.

ILMerge only merges managed assemblies, here, to quote from their download page, 'ILMerge is a utility for merging multiple .NET assemblies into a single .NET assembly'.

I have not seen an managed assembly merged with a native binary. Technically, you could have them merged per se, by including the unmanaged binary as a embedded resource, but the loading of the embedded resource into memory as a binary code - I have not seen this before. I have tried that technique using memory maps but failed.

The other way of checking is to look in the binary itself, if it has the 15th entry in the data directory, and is non-zero then it is a .NET binary, native binaries do not have this. See here where I posted this answer to a similar question.

Hope this helps,
Best regards,
Tom.

诠释孤独 2024-08-22 21:06:43

我认为您应该使用 .NET 反射来遍历程序集中的所有类型和方法。

I think you should use .NET reflection to go through all types and methods in assembly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文