C# – Creating a managed wrapper for 32bit and 64bit unmanaged DLL

64-bitcunmanagedvisual studiox86-64

We are creating a C# wrapper around a unmanaged DLL. The unmanaged DLL comes in both a 32 and 64bit versions. We keep the managed wrapper in its own project so that we can build it as a separate component and reuse it across solutions.

However this leads to some problems. Since the unmanaged DLL has the same name for both the 32bit and 64bit versions we are having trouble moving the correct unmanaged DLL to the output (bin) directory. If the build configuration is x86 we want to copy the 32bit version and with x64 the 64bit. With just one processor architecture this is easy to achieve. We just include the unmanaged DLL in our project and set copy local to true on the file. But since we need to target both its more tricky.

We found this link Targeting both 32bit and 64bit with Visual Studio in same solution/project but this seems to reference some DLL that already exist on the machine. We want the correct version of the DLL to be copied to the output directory (bin).

Any tips or techniques on how to solve this are more than welcome.

Best Answer

I just went through this same issue with the .Net wrapper for the FreeImage library. What I did was create two build configurations, one for x86 and one for x64 for the project that references the managed wrapper. I added msbuild conditional copy sections in the AfterBuild target of the project file like so:

  <Target Name="AfterBuild">
    <Copy Condition="'$(Platform)' == 'X86'" SourceFiles="$(MSBuildProjectDirectory)\Resources\x86\FreeImage.dll" DestinationFolder="$(TargetDir)" />
    <Copy Condition="'$(Platform)' == 'X64'" SourceFiles="$(MSBuildProjectDirectory)\Resources\x64\FreeImage.dll" DestinationFolder="$(TargetDir)" />
  </Target>