.NET interop bitness
-
This is fairly low priority, as all the machines our code runs on are 64-bit so I don't actually have to support 32-bit, but I'd still like to get this correct.
I have a medium size (~25 projects) solution in Visual Studio .NET: 1 ASP.NET project and the rest are class libraries (.NET 4.0). All of them are AnyCPU, meaning they run in 64-bits if the host environment supports it, or 32-bits if it doesn't. I've recently introduced an unmanaged (native code) dll file dependency. I have 32-bit and 64-bit versions of this dll, but I can't figure out how I can reference the appropriate one depending on the bitness of the hosting environment.
All our dev machines and the web server are 64-bit, as I stated, so I'm just linking to the 64-bit dll and everything is fine. Is there a way to make it choose the right DLL for the current CPU architecture?
-
It's definitely doable, because the managed wrapper for sqlite3 does it. I might try to figure out how if no-one posts a solution first.
-
The short answer seems to be that you look at IntPtr.Size to decide whether you're on 32-bit or 64-bit, and then use an extern to LoadLibrary. The details can get quite messy if you're trying to support CF, Alpha processors, etc.
-
-
If nothing else, try loading one and if it doesn't work, try the other. This is probably the simplest solution, assuming you always ship both.
-
We do this for a few different BuildMaster Extensions -- one off hand, https://github.com/inedo/bmx-sqlanywhere -- specifically, https://github.com/Inedo/bmx-sqlanywhere/blob/master/SqlAnywhereDatabaseProvider.cs#L48
That's SqlAnywhere specific of course, but the idea is to look at processor (intPtr) then load appropriate dll. That's the only way to do it in .NET -- you have you dynamically load.