在C#preprocessor指令导入基于平台 [英] Preprocessor directive in C# for importing based on platform

查看:182
本文介绍了在C#preprocessor指令导入基于平台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

寻找在C#preprocessor指令的基础上的可执行文件是否是64位或32进口的dll位:

 #如果WIN64
函数[DllImport(ZLIB64.dll,CallingConvention = CallingConvention.Cdecl)
#其他
函数[DllImport(ZLIB32.dll,CallingConvention = CallingConvention.Cdecl)


解决方案

下面是你需要做的事情。

首先,进入项目 - ><项目名称>属性... 并转到Build标签。

在那里,在文本框中标有条件编译符号,添加 WIN32 为您的x86平台(在对话框顶部的选择)和 WIN64 为您的x64平台。然后保存。

请注意,如果你有一个值为anycpu,你可能希望完全删除该平台,因为它不会是安全的。

然后,进入源,并写:

 #如果WIN64
    函数[DllImport(ZLIB64.dll,CallingConvention = CallingConvention.Cdecl)
#其他
    函数[DllImport(ZLIB32.dll,CallingConvention = CallingConvention.Cdecl)
#万一

请注意,当您查看源代码,其中一条线看起来像它已被注释掉,在整个行是在一个灰色的字体。这条线是一个为其他平台。如果您选择工具栏中的平台上,你会发现,语法着色跟风。

当然,在重新阅读我的答案,我注意到,你实际上并不需要把WIN32到有条件的符号列表,因为它不使用,但它可能是有用的其他地方做一个WIN32#如果而不是64。

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)]

解决方案

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.

这篇关于在C#preprocessor指令导入基于平台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆