samedi 8 septembre 2018

Can't load a specific 32-bit assembly through Reflection

I'm using the library SharpShell to develop a simple shell extension (a property sheet) to display some information for .NET assemblies, see:

enter image description here

This shell extension and SharpShell itself is not relevant to this question, but to explain my scenario so you can understand better my question.


My project is compiled as Any CPU mode, then, to manage the loading of x86, x64 an Any CPU assemblies in my program I'm using the overload of the function Assembly.Load() that takes the bytes of a raw assembly as unique argument:

Assembly asm = Assembly.Load( File.ReadAllBytes(filepath) );

I tested it with dlls and executables of both x86 and x64 architectures, and Any CPU, it works fine... except for the problem I'll explain.

The problem is that for some reason that I ignore it is not working for some x86 assemblies, for example when I try to load the x86 assembly: System.Web.dll ( System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a ), the Assembly.Load() function throws an exception. It is NOT a BadImageException, its a FileLoadException with this error message:

Could not load file or assembly 'System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The given assembly name or codebase was invalid. (Exception from HRESULT: 0x80131047)

( note that Assembly.ReflectionOnlyLoad() will also throw the same exception. )

However, I can load the x64 version of the same assembly with success. Please note that as I said, I can load most of the 32-bit assemblies in my program.

If I compile my project as x86, then I can load this assembly with sucess. But my project must compile as Any CPU (to be able handle the loading of both x86 and x64 assemblies ...since I'm not aware of a better approach).

I would like to know why I can't load that specific assembly, and a viable approach to solve this issue.

Here you can download the dlls if you want to test it:

http://www.mediafire.com/file/8h9256w02b2j3dj/System.Web.dll.zip/file





Aucun commentaire:

Enregistrer un commentaire