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

How to load exe with reflection if exe does not have a manifest?

我有一个可执行文件的转储(运行时: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。无论我用什么框架,它都会问我manifest。

以上都没有帮助我。

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

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

The Microsoft COFF Binary File Dumper (DUMPBIN.EXE) displays information about Common Object File Format (COFF) binary files.

因此,要正确加载它,您需要使用 Assembly.Load(byte[], ...). That documentation states that its parameter accepts a raw COFF 字节数组:

A byte array that is a COFF-based image containing an emitted assembly.

上述来源的这部分内容可能也与您相关

Reflecting on C++ executable files might throw a BadImageFormatException. This is most likely caused by the C++ compiler stripping the relocation addresses or the .reloc section from your executable file. To preserve the .reloc address for your C++ executable file, specify /fixed:no when you are linking.