从Win32环境获取.net二进制文件的强名称令牌 [英] Get strong name token of .net binary from win32 environment

查看:89
本文介绍了从Win32环境获取.net二进制文件的强名称令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个c ++ win32控制台应用程序(regsvr42),该应用程序生成清单文件并使用/MT进行编译.它添加到清单中的某些dll是.net,有些是win32.所有的dotnet都是强名称.我需要从.net dll中获取公共全名令牌,以便我可以在清单文件中正确引用它们.

sn -T mydllname.dll显示此令牌,但是我需要从可以挂接到Win32应用程序的api调用中获取它.我可以看到它可以在使用反射的.net环境中完成,但是我无法从我的应用程序/clr和/mt编译选项中选择.net与我们兼容

理想情况下,我希望有一种从win32 API模拟获取令牌的方法,也可以是我可以使用GetFileVersionInfo

I have a c++ win32 console application (regsvr42) that generates manifest files and is compiled with /MT. Some of the dlls it adds to the manifest are .net and some are win32. All the dotnet ones are strong named. I need to get the public strong name token from the .net dlls so that I can reference them correctly in the manifest file.

sn -T mydllname.dll displays this token, but I need to get it from an api call that can be hooked into the win32 application. I can see that it can be done in the .net environment using reflection but I cann''t us .net from my application /clr and /mt compile options are not compatable

Ideally I would like a way to get the token from win32 API simular to the way I can get the version information using GetFileVersionInfo

推荐答案

获取版本信息的方法,首先,我可以看到你''对于获取版本信息的.NET部分有些困惑.方法GetFileVersionInfo仅获取文件版本. .NET软件的版本控制系统通常基于程序集版本,该程序集完全独立于文件版本,并由程序集属性AssemblyVersion定义,而文件版本由程序集属性AssemblyFileVersion定义.
假设您从文件加载程序集.获取版本的方法如下:

First, I can see you''re a bit confused about .NET part of getting version information. The method GetFileVersionInfo only gets file version. The versioning system of .NET software is usually based on assembly version, which is totally independent from the file version and is defined by the assembly attribute AssemblyVersion and the file version is defined by the assembly attribute AssemblyFileVersion.

Suppose you load an assembly from file. Here is how you get the version:

System.Reflection.Assembly assembly = 
    System.Reflection.Assembly.LoadFrom(fileName);
System.Version version =
    assembly.GetName().Version;



现在,让我们介绍通过Win32非托管代码进行汇编的方法.我建议您解决此问题.首先,创建一个.NET程序集,以所需的方式从程序集中检索System.Version信息.它应该将此类型克隆为您的非托管代码可以理解的某种托管类型.

现在,问题是将结果传递给非托管代码.您将需要在库程序集中创建一些静态托管方法,并将其导出为非托管方法.并且非托管代码应该能够将该库作为常规非托管DLL加载并调用此方法.

许多人会说这是不可能的.这几乎是真的,但不是全部.这仅在C#或VB.NET中是不可能的,但使用IL汇编程序则完全有可能.这是CLR ECMA和ISO标准的一部分,不像任何肮脏的把戏.

因此,其想法是使用特殊属性将要导出的方法标记为非托管方法.该代码由VB.NET或C#编译,反汇编;然后根据属性修改IL代码,然后重新组装.这是使用MSBuild定制步骤自动完成的,因此不需要IL汇编程序知识.

您可以在以下CodeProject文章中找到解决方案:
非托管代码可以包装托管方法 [如何自动将.NET功能导出到非托管程序 [ http://msdn.microsoft.com/en-us/library/system.reflection.assemblyname.aspx [ ^ ].


—SA



Now, let''s approach getting assembly by the Win32 unmanaged code. I suggest you work around the problem. First, create a .NET assembly which retrieve the System.Version information from the assemblies the way you want. It should clone this type into some managed type understandable by your unmanaged code.

Now, the problem is to deliver the result to the unmanaged code. You will need to create some static managed method in you library assembly and export it as unmanaged; and the unmanaged code should be able to load this library as a regular unmanaged DLL and call this method.

Many would say this is impossible. This is almost true but not all the truth. It is only impossible in C# or VB.NET, but quite possible using IL assembler; and this is a part of CLR ECMA and ISO standards, not like any dirty trick.

So, the idea is to use a special attribute to mark the methods to be exported as unmanaged. The code is complied by VB.NET or C#, disassembled; then the IL code is modified according to the attributes and assembled again. This is automated using the MSBuild custom step, so no knowledge of IL assembler is required.

You can find the solution in the following CodeProject articles:
Unmanaged code can wrap managed methods[^],
How to Automate Exporting .NET Function to Unmanaged Programs[^].


Same approach goes for getting a public key token of assembly strong name. You can get it using your .NET library DLL. Use System.Reflection.Assembly.GetName().GetPublicKeyToken, see http://msdn.microsoft.com/en-us/library/system.reflection.assemblyname.aspx[^].


—SA


这篇关于从Win32环境获取.net二进制文件的强名称令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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