2

I have a project which configured for "Any CPU". Now I have to refer a third party dll which has x86 and x64 compiled versions seperately(I cant get AnyCPU version of third party dll). I have changed the configuration file of my project to refer particular dll based on the platform as shown below.(This is a sample project config file)

<PropertyGroup>
<CurrentPlatform>x86</CurrentPlatform>
<CurrentPlatform Condition="'$(PROCESSOR_ARCHITECTURE)'=='AMD64' or '$(PROCESSOR_ARCHITEW6432)'=='AMD64'">x64</CurrentPlatform>
</PropertyGroup>
<ItemGroup Condition=" '$(Platform)' == 'x86' ">
<!--Compiled as ClassLibrary target platform is x86-->
 <Reference Include="ClassLibrary">
    <HintPath>..\..\ClassLibrary_x86\ClassLibrary_x86\bin\Debug\ClassLibrary.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup Condition=" '$(Platform)' == 'x64' ">
 <!--Compiled as ClassLibrary target platform is x64-->
 <Reference Include="ClassLibrary">
      <HintPath>..\..\ClassLibrary_x64\ClassLibrary_x64\bin\Debug\ClassLibrary.dll</HintPath>
</Reference>
</ItemGroup>

when I run this project in Visual studio it is not working. throwing BadImageException. When I change My project target platform to x86 and run the application it will work. If I change it to x64 then throw the same BadImageException.

What is the wrong here? I don't want to create two projects(x86 and x64) just because of one dll reference. Is there any other way to proceed if the above way is wrong?

My Dev environment is VS2010 and .NET4.0 and Win7 64bit OS.

David
  • 15,894
  • 22
  • 55
  • 66
PSR
  • 875
  • 2
  • 13
  • 37

1 Answers1

2

Change the platform option to be X86 and only reference the 32-bit dll. This way, the executable will run both on 32-bit and 64-bit OS. Application built with x86 option will run as 32-bit process on 64-bit OS under WOW64.

Also the next link can help - Conditionally use 32/64 bit reference when building in Visual Studio

The next link show how to load the right dll which is what you are looking for

Community
  • 1
  • 1
Mzf
  • 5,210
  • 2
  • 24
  • 37
  • I have to reference 64 bit dll for 64bit OS. I have seen your link but not solved my problem. – PSR May 27 '13 at 06:53
  • Is there any reason that you want to use the x64 rather than always using the x86 ? – Mzf May 27 '13 at 06:56
  • yes 32bit dll wont work, I mean it wont give expected results on 64 bit machine. – PSR May 27 '13 at 07:00
  • OK, maybe you can use the post build event to copy the right dll version after build. I have update the answer with relevant link – Mzf May 27 '13 at 07:19
  • @Sree did it answer your question ? – Mzf Jun 09 '13 at 18:29