How do .NET Framework classes reference native Windows DLLs without becoming specific?

I read a lot of questions and answers, indicating that if I want to link my C # project with my native libraries, I cannot use the target AnyCPU platform, but I have to make separate 32- and 64-bit assemblies, each of which is associated with a native DLL corresponding bitness.

This makes you wonder how the .NET Framework assemblies themselves or, at least, seem to be created for AnyCPU . That is, adding a link to my GUI application, why don’t I have to choose the 32-bit or 64-bit version of System.Windows.Forms ? I thought it might just be Visual Studio magic, which would allow the corresponding GAC subdirectory (GAC_32 or GAC_64), but I looked for System.Windows.Forms.dll in the GAC and found it in:

C: \ Windows \ Microsoft.NET \ assembly \ GAC_MSIL \ System.Windows.Forms \ v4.0_4.0.0.0__b77a5c561934e089 \ System.Windows.Forms.dll

Pay attention to the "GAC_MSIL". So, how does this DLL manage to wrap its own 32-bit API, but still remains a binder in a 64-bit application? And why can't I use a similar strategy to make a single C # DLL that references its own 32-bit library, but remains operational in 64-bit mode?

+6
source share
2 answers

Option 1: In the GAC, you can register 2 build versions of one 32 and one 64 bit with exactly the same names. The Oracle DB driver for .NET uses this strategy.

Option 2: with your assembly, which will be AnyCPU, deploys two versions of the embedded DLL and selects the desired DLL at runtime (SQLite works like this). As it turns out, the .NET Framework is smart enough to load the proper version of the embedded DLL through P / Invoke ( Using a 32-bit or 64-bit dll in C # DllImport )

0
source

I had the same problem and ended up using Fody Costura

DLL files will be delivered as embedded resources, and the library takes care of bitness.

You can find an example for SQLite here

The problem I ran into was that your application must have access to the Windows Temp folder to create assemblies from the resource. If you do not need this, you can disable it using the configuration option .

0
source

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


All Articles