Using a 32-bit or 64-bit dll library depending on the degree of processing

I need to reference the DLL, which is available in two versions (one for 32-bit and one for 64-bit). My goal is to create a web application that runs on both 32 and 64-bit systems.

I was thinking of referencing the default 32-bit assembly and using the AssemblyResolve event to load the 64-bit version (if the 32-bit version failed to load):

static void Main(string[] args) { AppDomain.CurrentDomain.AssemblyResolve += _AssemblyResolve; // Try LoadAssembly ... } static System.Reflection.Assembly _AssemblyResolve(object sender, ResolveEventArgs args) { var path = string.Format(@"...\lib_x64\{0}.dll", args.Name); return Assembly.LoadFrom(path); } 

But even if a BadImageFormatException error occurs, the _AssemblyResolve handler will not be called. Is there any other way to achieve the proposed behavior?

+6
source share
2 answers

See the answers to solve this problem for System.Data.SQLite .

I think your proposed method should work, but you need to move the 32-bit version so that it is not found by default, so _AssemblyResolve always called for this DLL. This is just an assumption.

0
source

The easiest way, but less flexible from my point of view, is to explicitly reference the specific platform in the csproj file using Condition :

 <ItemGroup Condition=" '$(Platform)' == 'x86' "> <Reference Include="MyAssemblyx86"> 

You can also do this dynamically using the Assembly.Load (AssemblyName) overload method. The parameter is of type AssemblyName , which provides the AssemblyName.ProcessorArchitecture property, which can be set to None, MSIL, X86, X64, IA64, AMD64

One thing you can also learn is the Publisher Policy File parameter and the /platform:processorArchitecture command line argument

+1
source

Source: https://habr.com/ru/post/897963/


All Articles