How to use the correct unmanaged DLL file in accordance with the processor architecture? (32/64 bit)

I need to use a C ++ DLL file from an ASP.NET site. The site will be hosted in both 32 and 64-bit environments.

I have a 32 and 64 bit version of an unmanaged DLL file. How to import methods from the correct one according to the current server architecture?

Like someone, this could be due to a deployment problem. My concern is: the web application using this dll file is huge and we do not have a document to deploy. No one will remember to deploy the correct DLL file, so I'm trying to remove the human factor from the solution :)

+6
c ++ 32bit-64bit
source share
3 answers

The simplest approach would be to give the two native libraries the same file name in two different directories, then configure your application's DLL search path depending on the bit depth.

http://www.pinvoke.net/default.aspx/kernel32.setdlldirectory

+5
source share

To get the compiler / framework to do most of the work you need,

  • have several build platforms (usually x86, x64 - remove AnyCPU)
  • configure each platform configuration for each assembly configuration.
  • we added __WIN32 and __X64 compilation conditionals

List the various implementations of your functions according to the platform, including different dll names if you need to install both installed at the same time.

#if __WIN32 public delegate int Move(int target); [DllImport("my.dll", SetLastError = true, CharSet = CharSet.Auto)] #elif __X64 public delegate int Move(int target); [DllImport("my64.dll", SetLastError = true, CharSet = CharSet.Auto)] #endif 

Otherwise, you can use loadlib and manage the sort yourself.

+1
source share

As we know, we cannot add unmanaged DLL files using the add link. Therefore, we need an alternative way to handle unmanaged DLLS files according to the processor architecture, which adds DLL files to your project directories.

  • Create new folders in the project named x32 and x64.
  • Copy the DLL files to these folders according to the architecture.
  • Select files and open properties, change "Copy to output directory: Always copy"
  • Repeat this for all files in both folders.

Now create your project on any platform when you build the project. The x86 and x64 files will be copied to the bin folder. When this is done on the server, it will use unmanaged DLL files in accordance with the processor architecture.

+1
source share

All Articles