Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to distribute both 32 and 64 bit versions of the library

I have a C# library that is called by various clients (both 32-bit and 64-bit). Up to now it was compiled as AnyCPU, so there was no issues.

Recently I added a dependency to SQLite .NET library which come in both 32 and 64-bit flavors (but not AnyCPU). So, now, I have to have 2 builds - for both bitnesses.

In the past, I've seen other libraries (MS SQL Compact comes to mind) that had a scheme where a single .NET assembly would have Private\amd64 and Private\x86 folders in the folders with the appropriate native libraries in them and it would call each one as necessary.

Is this approach viable for my situation? Is there documentation on how to implement it? Are there code changes required or is this a distribution technique?

like image 276
AngryHacker Avatar asked Feb 27 '12 17:02

AngryHacker


People also ask

Will 32-bit and 64-bit combination compatible and will work together?

In short: You can't link a 32-bit app to a 64-bit library. You can run a 32-bit application, using 32-bit shared libraries on a 64-bit OS (at least all the popular 32-/64-bit processors such as AMD, Intel and Sparc).

Can a 64-bit application use a 32-bit library?

32-bit applications must link with 32-bit libraries, and 64-bit applications must link with 64-bit libraries. It is not possible to create or execute a 32-bit application using 64-bit libraries. The 32-bit libraries continue to be located in /usr/lib and /usr/ccs/lib .


1 Answers

There are several ways you can handle this. Code changes (small) are required for the first three approaches:

A. You can modify the PATH to point to the platform specific folder during application start up. Then .NET will automatically load local DLLs from that folder.

B. You can subscribe to the AssemblyResolve event and then choose the assembly based on the platform.

Check out Scott Bilias's blog post on this http://scottbilas.com/blog/automatically-choose-32-or-64-bit-mixed-mode-dlls/. Note that he ends up preferring approach A.

"In a nutshell, the solution is to trick the loader! Reference a p4dn.dll that does not exist, and use the AssemblyResolve event to intercept the load and reroute it to the correct bit size assembly."

C. Use a platform-specific set of exe.configs and the codebase element to determine assembly locations. Your setup would install the correct one based on platform.

http://msdn.microsoft.com/en-us/library/4191fzwb.aspx

D. Write two setups one for 32-bit and one for 64-bit, then only install the appropriate files for the platform.

like image 138
Geoff Cox Avatar answered Oct 14 '22 14:10

Geoff Cox