简体   繁体   English

C#中用于基于平台导入的预处理器指令

[英]Preprocessor directive in C# for importing based on platform

Looking for a preprocessor directive in c# for importing dll based on whether the executable is 64bit or 32 bit:根据可执行文件是 64 位还是 32 位,在 c# 中寻找用于导入 dll 的预处理器指令:

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

Here's what you need to do.这是您需要做的。

First, go into Project-><project name> Properties... and go to the Build tab.首先,进入Project-><project name> Properties...并进入 Build 选项卡。

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.在那里,在标记为“条件编译符号”的文本框中,为您的 x86 平台添加WIN32 (可在对话框顶部选择)和为您的 x64 平台添加WIN64 Then save.然后保存。

Note that if you have one for "AnyCPU", you probably want to remove that platform altogether, as it won't be safe.请注意,如果您有一个用于“AnyCPU”的平台,您可能希望完全删除该平台,因为它不安全。

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.当然,在重新阅读我的答案后,我注意到您实际上并不需要将 WIN32 放入条件符号列表中,因为它没有被使用,但是在其他地方在 WIN32 而不是 64 上执行 #if 可能会很有用.

You'll have to add a conditional compilation symbol for each target platform in your project's properties, in the Build tab.您必须在项目属性的 Build 选项卡中为每个目标平台添加条件编译符号。 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.更改平台将允许您添加仅适用于该平台构建的不同符号。

unload and edit the .csproj file, add:卸载并编辑 .csproj 文件,添加:

  <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问候

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.如果您使用 Visual Studio,请使用配置管理器为 32 位和 64 位版本创建不同的构建配置。 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.然后打开项目属性并转到 Build 选项卡并在每个构建配置的条件编译符号文本框中输入一个描述性名称。 Then you can reference the compilation constants in code.然后就可以在代码中引用编译常量了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM