tags:

views:

107

answers:

3

I have two versions of System.Data.SQLite.DLL - for x86 and x64 platform. The x86 version keeps in application folder and x64 version keeps in appFolder\x64 folder. The application compiled as AnyCPU. How can i load needed version of SQLite according to windows platform?

+1  A: 

You could use Environment.Is64BitProcess to identify the process as 64 bit. (I would try to avoid catching exceptions as flow-control wherever possible.)

Paul Ruane
It's good proposition but it don't solve my problem.
Rover
@Rover: How so? Do you definitely have 64 bit .NET installed?
Paul Ruane
I use .NET 3.5 that not have this variable. I think i can check the version of platform but it only avoid catching some exceptions but needed assembly is not loaded.
Rover
@Rover: Ah hah, I am with you now. You are saying that the 64 bit assembly fails to load on 64 bit platform, rather than that you don't know how to detect the local platform?
Paul Ruane
The problem is loading of needed assembly :)
Rover
@Rover: OK, would it not be possible to check the Environment.Is64BitProcess and then, depending upon the result, load either the 32 bit library (Environment.Is64BitProcess = false) or the 64 bit library (Environment.Is64BitProcess = true)?
Paul Ruane
@paul-ruane: How can i load assembly to AppDomain?
Rover
A: 

Couldn't you just use the source of SQLite as a separate project in your solution instead of a precompiled assembly? Using AnyCPU the system itself will take care of everything and you don't have to do it in code...

Tim Robbin
It's one of dlls that i use which divided on x86 and x64. I don't have source code of all used dlls.
Rover
+1  A: 

I'm surprised that this works at all. It should find the x86 version first and fail. A failed assembly bind doesn't produce another attempt through AssemblyResolve.

Clearly, the CLR cannot actually find the x86 version or this would fail in x64 mode as well. In other words, when you fix the problem, you'll break the 64-bit code. Chase the x86 problem first, use Fuslogvw.exe to see which folders are being probed for the assembly.

A real fix should include moving the x86 assembly into a separate folder as well and adjusting your event handler accordingly. You can test IntPtr.Size to find out if you're running in 64-bit mode (Size == 8). Also be sure to generate a full path name, using the relative path like you do now can cause failure when the app's working directory isn't set where you hope it is. Assembly.GetEntryAssembly().Location gets you the path of the EXE.

Hans Passant
I removed code for avoiding of confuses and leave only question.
Rover
Hmm, I don't think that changes my answer. The last paragraph tells you how to do it right.
Hans Passant
Ok, i checked platform, found needed assembly. What should i do for loading of assembly to AppDomain?
Rover
You mean you want to load it in a non-default AppDomain? Better start a new question for that.
Hans Passant