Search code examples
c#visual-studiox86dependencies64-bit

C# Switch dependencies depending on x86 or x64


We´ve got a third party dll which is either x86 or x64, now we have to change the x64 dll with x86 dependent on the TargetPlatform. May someone tell how?

The first try was to override the dll with a post build script.


Solution

  • With Configuration Manager we could brand the build with x86, x64 and with the macro $(PlatformName) we could name the dll path relative to the platform (x64/x86)

    Important to know is, that the macro has to be set in *.csproj file directly e.g.:

    <Reference Include="DLLNAME, Version=4.9.0.0, Culture=neutral, PublicKeyToken=TOKEN, 
          processorArchitecture=$(PlatformName)">
          <HintPath>Lib\$(PlatformName)\DLLNAME.dll</HintPath>
        </Reference>
    

    Also if there is a Plugin which should be compiled into the main-programm folder, the build path has to be changed in *.csproj file directly. E.g:

     <OutputPath>..\..\bin\$(PlatformName)\Debug\Features\</OutputPath>
    

    Thats because VS escapes the '$(' and ')'

    EDIT: Too fast. The <OutputPath> doesn´t understand macros. Therefore we had to change the outputpath for every Build-Configuration. Eg:

      <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'">
        <DebugSymbols>true</DebugSymbols>
        <OutputPath>..\..\bin\x86\Debug\Features\</OutputPath>
        <DefineConstants>DEBUG;TRACE</DefineConstants>
        <DebugType>full</DebugType>
        <PlatformTarget>x86</PlatformTarget>
        <LangVersion>7.3</LangVersion>
        <ErrorReport>prompt</ErrorReport>
        <CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
        <Prefer32Bit>true</Prefer32Bit>
      </PropertyGroup>
      <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x86'">
        <OutputPath>..\..\bin\x86\Release\Features\</OutputPath>
        <DefineConstants>TRACE</DefineConstants>
        <Optimize>true</Optimize>
        <DebugType>pdbonly</DebugType>
        <PlatformTarget>x86</PlatformTarget>
        <LangVersion>7.3</LangVersion>
        <ErrorReport>prompt</ErrorReport>
        <CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
        <Prefer32Bit>true</Prefer32Bit>
      </PropertyGroup>
    

    EDIT 2: For only x64 or x86 dependencies there is the Condition-Attribute which can seperate some cs-Files from x86 or x64 eg:

    <Reference Condition="'$(Platform)'=='x64'" Include="STPadLibNet">
      <HintPath>Lib\x64\DLLNAME.dll</HintPath>
    </Reference>
    

    and for some Clases u can use it like:

    <Compile Condition="'$(Platform)'=='x64'" Include="MyClass.cs" />