title: Custom Is64BitOperatingSystem preprocessor directive
body: <p>We can add custom preprocessor directives for <a href="https://blog.magnusmontin.net/2018/11/05/platform-conditional-compilation-in-net-core/" rel="nofollow noreferrer">Platform Conditional Compilation in .NET Core</a> like this</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;Project Sdk="Microsoft.NET.Sdk"&gt;
    &lt;PropertyGroup&gt;
      &lt;OutputType&gt;Exe&lt;/OutputType&gt;
      &lt;TargetFramework&gt;netcoreapp2.0&lt;/TargetFramework&gt;
      &lt;IsWindows Condition="'$([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform($([System.Runtime.InteropServices.OSPlatform]::Windows)))' == 'true'"&gt;true&lt;/IsWindows&gt;
      &lt;IsOSX Condition="'$([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform($([System.Runtime.InteropServices.OSPlatform]::OSX)))' == 'true'"&gt;true&lt;/IsOSX&gt;
      &lt;IsLinux Condition="'$([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform($([System.Runtime.InteropServices.OSPlatform]::Linux)))' == 'true'"&gt;true&lt;/IsLinux&gt;
    &lt;/PropertyGroup&gt;
    &lt;PropertyGroup Condition="'$(IsWindows)'=='true'"&gt;
      &lt;DefineConstants&gt;Windows&lt;/DefineConstants&gt;
    &lt;/PropertyGroup&gt;
    &lt;PropertyGroup Condition="'$(IsOSX)'=='true'"&gt;
      &lt;DefineConstants&gt;OSX&lt;/DefineConstants&gt;
    &lt;/PropertyGroup&gt;
    &lt;PropertyGroup Condition="'$(IsLinux)'=='true'"&gt;
      &lt;DefineConstants&gt;Linux&lt;/DefineConstants&gt;
    &lt;/PropertyGroup&gt;
  &lt;/Project&gt;
  </code></pre>
  
  <p>I've tested, it's working fine. </p>
  
  <p>Now I want to detect whether or not I'm on a 64 bits operating system. Here is my <code>.csproj</code></p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;Project Sdk="Microsoft.NET.Sdk"&gt;
  
    &lt;PropertyGroup&gt;
      &lt;TargetFramework&gt;netstandard2.1&lt;/TargetFramework&gt;
      &lt;Is64BitOperatingSystem Condition="'$([System.Environment]::Is64BitOperatingSystem)' == 'true'"&gt;true&lt;/Is64BitOperatingSystem&gt;
    &lt;/PropertyGroup&gt;
  
    &lt;PropertyGroup Condition="'$(Is64BitOperatingSystem)'=='true'"&gt;
      &lt;DefineConstants&gt;Is64BitOperatingSystem&lt;/DefineConstants&gt;
    &lt;/PropertyGroup&gt;
  &lt;/Project&gt;
  </code></pre>
  
  <p>However when I run this code, my first <code>if...else</code> is working as expected but not my <code>Is64BitOperatingSystem</code> preprocessor directive</p>
  
  <pre class="lang-cs prettyprint-override"><code>if (System.Environment.Is64BitOperatingSystem)
      Console.WriteLine(64);
  else
      Console.WriteLine(32);
  
  #if Is64BitOperatingSystem
      Console.WriteLine(64);
  #else
     Console.WriteLine(32);
  #endif
  </code></pre>
  
  <p>What am I doing wrong? I can't spot where's the mistake in my code.</p>
  
  <p>Thank you</p>
  
  <h1>EDIT</h1>
  
  <p>To add more details about this, I included this code in a .NET Standard library that is called by a .NET Core project.</p>
  
  <p>I want my library to detect the current architecture it's running on (<strong>or has been compiled for</strong>) so I can do something like this</p>
  
  <pre class="lang-cs prettyprint-override"><code>#if Is64BitOperatingSystem
      [DllImport(@"Resources/HIDAPI/x64/hidapi")]
  #else
      [DllImport(@"Resources/HIDAPI/x32/hidapi")]
  #endif
  </code></pre>
  
  <p>Before debugging, Visual Studio obviously compiles my application so at this stage checking the architecture using <code>System.Environment.Is64BitOperatingSystem</code> or a preprocessor directive should give the same results but it's not. I'm on a 64 bits machine and my preprocessor directive tells me I'm on a 32 bits architecture even if I change <code>AnyCPU</code> to <code>x64</code> in Visual Studio Configuration Manager</p>
  
  <p>Note that <a href="https://stackoverflow.com/a/1313450/1203116">this answer</a> is Windows specific and <a href="https://stackoverflow.com/a/8861895/1203116">that one</a> too because the solution is to call the <code>SetDllDirectory</code> function from <code>kernel32.dll</code> </p>
  
  <p>But I want my code to be able to run on Linux.</p>
  
  <h1>EDIT 2:</h1>
  
  <p>For the sake of sharing a minimal sample here I actually removed the faulty part of my code.</p>
  
  <p>It looks like this gives the expected result:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;PropertyGroup&gt;
      &lt;TargetFramework&gt;netstandard2.1&lt;/TargetFramework&gt;
      &lt;Is64BitOperatingSystem Condition="'$([System.Environment]::Is64BitOperatingSystem)' == 'true'"&gt;true&lt;/Is64BitOperatingSystem&gt;
  &lt;/PropertyGroup&gt;
  
  &lt;PropertyGroup Condition="'$(Is64BitOperatingSystem)'=='true'"&gt;
      &lt;DefineConstants&gt;Is64BitOperatingSystem&lt;/DefineConstants&gt;
  &lt;/PropertyGroup&gt;
  </code></pre>
  
  <p>But this gives a faulty behavior:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;PropertyGroup&gt;
      &lt;TargetFramework&gt;netstandard2.1&lt;/TargetFramework&gt;
      &lt;Is64BitOperatingSystem Condition="'$([System.Environment]::Is64BitOperatingSystem)' == 'true'"&gt;true&lt;/Is64BitOperatingSystem&gt;
      &lt;IsWindows Condition="'$([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform($([System.Runtime.InteropServices.OSPlatform]::Windows)))' == 'true'"&gt;true&lt;/IsWindows&gt;
  &lt;/PropertyGroup&gt;
  
  &lt;PropertyGroup Condition="'$(Is64BitOperatingSystem)'=='true'"&gt;
      &lt;DefineConstants&gt;Is64BitOperatingSystem&lt;/DefineConstants&gt;
  &lt;/PropertyGroup&gt;
  
  &lt;PropertyGroup Condition="'$(IsWindows)'=='true'"&gt;
      &lt;DefineConstants&gt;Windows&lt;/DefineConstants&gt;
  &lt;/PropertyGroup&gt;
  
  </code></pre>
  
  <p>If anybody could explain me that? 
  I don't understand why adding the <code>IsWindows</code> condition is responsible for a different behavior on the <code>Is64BitOperatingSystem</code> preprocessor directive</p>