46

Looking for a preprocessor directive in c# for importing dll based on whether the executable is 64bit or 32 bit:

#if WIN64
[DllImport("ZLIB64.dll", CallingConvention=CallingConvention.Cdecl)]
#else
[DllImport("ZLIB32.dll", CallingConvention=CallingConvention.Cdecl)]
jww
  • 97,681
  • 90
  • 411
  • 885
user85917
  • 925
  • 3
  • 12
  • 21

4 Answers4

60

Here's what you need to do.

First, go into Project-><project name> Properties... and go to the Build tab.

In there, in the textbox labelled "Conditional compilation symbols", add WIN32 for your x86 platform (selectable at the top of the dialog) and WIN64 for your x64 platform. Then save.

Note that if you have one for "AnyCPU", you probably want to remove that platform altogether, as it won't be safe.

Then, go into the source, and write this:

#if WIN64
    [DllImport("ZLIB64.dll", CallingConvention=CallingConvention.Cdecl)]
#else
    [DllImport("ZLIB32.dll", CallingConvention=CallingConvention.Cdecl)]
#endif

Note that when you view the source, one of the lines will look like it has been commented out, in that the entire line is in a gray font. This line is the one for the "other platform". If you select the platform in the toolbar, you'll notice that the syntax coloring follows suit.

Of course, after re-reading my answer I notice that you don't actually need to put WIN32 into the conditional symbols list as it isn't used, but it might be useful other places to do an #if on WIN32 instead of 64.

Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825
  • And you know that by "platform", one usually means two different operating systems, not two minuscule different CPU architectures ? That absolutely doesn't work if you have to support x64 Windows and x64 Linux and MacOS... – Stefan Steiger Sep 11 '18 at 05:27
  • 2
    @StefanSteiger That may be but it is the answer I left back in 2009. If you're loading a dll as an dll-import into .NET back in 2009, you were on Windows. As for "Platform", if that was the wrong word to use back then for 32-bit vs. 64-bit I was hardly the only one. – Lasse V. Karlsen Sep 11 '18 at 06:29
  • Agree, but I was using mono back then, so you hardly had to be on Windows ;) – Stefan Steiger Sep 11 '18 at 06:36
  • I agree, I edited the question and comment several times, my conclusion was that since the question detailed about dll-import of dll's, I landed on Windows only. I know cross-platform, though not officially supported, has been with us for some time. Did/does DllImport handle link-libraries on other platforms in mono? – Lasse V. Karlsen Sep 11 '18 at 07:02
5

You'll have to add a conditional compilation symbol for each target platform in your project's properties, in the Build tab. Simply add a symbol for the given Platform as determined by the Platform drop-down at the top of the Build form. Changing Platform will allow you do add different symbols that apply only to a build for that platform.

Ed S.
  • 122,712
  • 22
  • 185
  • 265
4

unload and edit the .csproj file, add:

  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x64' ">
    <DefineConstants>WIN64;$(DefineConstants)</DefineConstants>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x64' ">
    <DefineConstants>WIN64;$(DefineConstants)</DefineConstants>
  </PropertyGroup>

use:

#if WIN64
...
#endif

Regards

Martin.Martinsson
  • 1,894
  • 21
  • 25
  • If for example I have 25 csproj files and build configurations, half of which are x64. Is there a way to define WIN64 or something similar without manually copying DefineConstants to all 25 build configs (prone to error) – rollsch Mar 14 '23 at 03:34
  • No, AFAIK. I would code a small console program and use XmlDocument and change the elements. Takes max. 30 mins. – Martin.Martinsson Mar 14 '23 at 13:11
2

There is nothing builtin that I am aware of. However, it is simple to define a custom compilation constant. If you are using Visual Studio create different build configurations for 32bit and 64bit versions using the Configuration Manager. Then open the project properties and go to the Build tab and enter a descriptive name in the conditional compilation symbols textbox for each build configuration. Then you can reference the compilation constants in code.

Brian Gideon
  • 47,849
  • 13
  • 107
  • 150