简体   繁体   中英

Assembly.LoadFrom works, Assembly.Load does not

I am trying to load the file using Assembly.Load but I get a BadImageException, but if I try and load the exact same assembly using Assembly.LoadFrom it works.

// this works.
var asm1 = Assembly.LoadFrom(path);

// this throws an exception
var ba = File.ReadAllBytes(path);
var asm2 = Assembly.Load(ba);

As you can see from the code above, the assembly is exactly the same, but it works for Assembly.LoadFrom but not Assembly.Load

What could be the reason for it? How can I get Assembly.Load to work?

Assmebly.Load might throw BadImageException while reflecting on C++ executable files might, unlike Assembly.LoadFrom(string) . For fixing that issue you have to use /fixed:no while linking. Here is the paragraph from Msdn:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM