如果 exe 没有清单,如何使用反射加载 exe?

德本森

我有一个可执行文件的转储(运行时:v2.0.50727)。它运行良好,没有任何错误。我可以将它加载到 DnSpy 进行调试或加载到 ILSpy。他们都告诉我们所有的引用都成功解析了。

但是,我无法使用以下代码加载它:

try
{
    var second_module = System.Reflection.Assembly.LoadFrom("myprog.bin");
}
catch (Exception e)
{
    Console.WriteLine(e.ToString());
}

它给了我以下错误:

System.BadImageFormatException: Could not load file or assembly 'file:///myprog.bin' or one of its dependencies. The module was expected to contain an assembly manifest.
File name: 'file:///myprog.bin'
   at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
   at System.Reflection.RuntimeAssembly.nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
   at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
   at System.Reflection.RuntimeAssembly.InternalLoadFrom(String assemblyFile, Evidence securityEvidence, Byte[] hashValue, AssemblyHashAlgorithm hashAlgorithm, Boolean forIntrospection, Boolean suppressSecurityChecks, StackCrawlMark& stackMark)
   at System.Reflection.Assembly.LoadFrom(String assemblyFile)
   at myproggg.Program.Main(String[] args) in c:\Sources\My\myproggg\Program.cs:line 11

=== Pre-bind state information ===
LOG: Where-ref bind. Location = myprog.bin
LOG: Appbase = file:///c:/Sources/My/myproggg/bin/x86/Debug/
LOG: Initial PrivatePath = NULL
Calling assembly : (Unknown).
===
LOG: This bind starts in LoadFrom load context.
WRN: Native image will not be probed in LoadFrom context. Native image will only be probed in default load context, like with Assembly.Load().
LOG: Using application configuration file: c:\Sources\My\myproggg\bin\x86\Debug\myproggg.exe.Config
LOG: Using host configuration file:
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: Attempting download of new URL file:///c:/Sources/My/MPCExtractor/examples/MP/myprog.bin.
ERR: Failed to complete setup of assembly (hr = 0x80131018). Probing terminated.

我尝试执行以下步骤:

  1. 将编译选项从“Any CPU”更改为“x86”。没有不同。

  2. 将框架更改为 2.0 和 4.5。无论我使用什么框架,它都会向我询问清单。

以上都没有帮助我。

我还能做些什么来加载这个转储的可执行文件?

成套工具

.bin您创建文件可追溯到 .NET 2.0 运行时使用的 COFF。您还可以使用dumpbin来获取.bin文件。文档指出

Microsoft COFF 二进制文件转储程序 (DUMPBIN.EXE) 显示有关通用对象文件格式 (COFF) 二进制文件的信息。

因此,要正确加载它,您需要使用Assembly.Load(byte[], ...)该文档指出其参数接受原始COFF字节数组:

一个字节数组,它是一个基于 COFF 的图像,包含一个发出的程序集。

来自上述来源的这部分也可能与您有关

反映 C++ 可执行文件可能会抛出BadImageFormatException这很可能是由 C++ 编译器.reloc从可执行文件中剥离重定位地址或部分造成的要保留.relocC++ 可执行文件地址,请指定/fixed:no链接时间。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章