[英]How to check if '32-bit prefered' is toggled using visual studio variables
I've made a NuGet package that has managed and unmanaged code.我制作了一个包含托管和非托管代码的 NuGet 包。 The managed code is written in C# and supports
AnyCPU
while the unmanaged code is in C++ and has a 32-bit and 64-bit version.托管代码使用 C# 编写并支持
AnyCPU
而非托管代码使用 C++ 编写并具有 32 位和 64 位版本。 The managed code in this package is dependent on the unmanaged code.此包中的托管代码依赖于非托管代码。 The structure of my package looks like this:
我的包的结构如下所示:
\build
\x86
unmanaged.dll
\x64
unmanaged.dll
package.targets
\lib
\net472
managed.dll
Within my '.targets' file, I'm checking the $(Platform)
variable to see if the user has selected x86
, x64
, or AnyCPU
and copy the correct unmanaged dll to the output directory:在我的“.targets”文件中,我正在检查
$(Platform)
变量以查看用户是否选择了x86
、 x64
或AnyCPU
并将正确的非托管 dll 复制到输出目录:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Condition=" '$(Platform)' == 'x64' ">
<Content Include="$(MSBuildThisFileDirectory)x64\unmanaged.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Link>unmanaged.dll</Link>
</Content>
</ItemGroup>
<ItemGroup Condition=" '$(Platform)' == 'x86' OR '$(Platform)' == 'AnyCPU' ">
<Content Include="$(MSBuildThisFileDirectory)x86\unmanaged.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Link>unmanaged.dll</Link>
</Content>
</ItemGroup>
</Project>
The expected behavior is when a user builds their program, the managed dll is copied over to the ouput directory with the correct umanaged dll according to the selected architecture type.预期的行为是当用户构建他们的程序时,托管 dll 被复制到输出目录,并根据所选的体系结构类型使用正确的 umanaged dll。
Everything works as intended when the user selects x86
or x64
, but not when AnyCPU
is chosen.当用户选择
x86
或x64
,一切都按预期工作,但在选择AnyCPU
时则AnyCPU
。 This is because if the user has '32-bit preferred' off and the 32-bit unmanaged dll is copied to the output directory, an exception is raised saying that the format of the 32-bit unmanaged dll doesn't match the process' architecture, which is obvious.这是因为如果用户关闭了“32 位首选”并且将 32 位非托管 dll 复制到输出目录,则会引发异常,说明 32 位非托管 dll 的格式与进程不匹配”架构,这是显而易见的。 I want to modify my '.targets' file so it can recognize if '32-bit preferred' is toggled on or off and copy the correct unmanaged dll to the output directory when the program is being built.
我想修改我的“.targets”文件,以便它可以识别“32 位首选”是打开还是关闭,并在构建程序时将正确的非托管 dll 复制到输出目录。 Is there a predefined Visual Studio variable that checks if '32-bit preferred' is toggled?
是否有预定义的 Visual Studio 变量来检查是否切换了“32 位首选”? If not, is there another way of checking the '32-bit preferred' option within the '.targets' file?
如果没有,是否有另一种方法可以检查“.targets”文件中的“32 位首选”选项?
After some personal experimentation I found out the solution to my problem.经过一些个人实验,我找到了解决问题的方法。 I unloaded my project to see if there were any lines pertaining to '32-bit preferred' and I found a line saying
<Prefer32Bit>false</Prefer32Bit>
, meaning I could add this to my '.targets' file:我卸载了我的项目以查看是否有任何与“32 位首选”相关的行,我发现一行说
<Prefer32Bit>false</Prefer32Bit>
,这意味着我可以将其添加到我的“.targets”文件中:
<ItemGroup Condition=" '$(Platform)' == 'AnyCPU' AND '$(Prefer32Bit)' == 'false' ">
<Content Include="$(MSBuildThisFileDirectory)x64\unmanaged.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Link>unmanaged.dll</Link>
</Content>
</ItemGroup>
<ItemGroup Condition=" '$(Platform)' == 'AnyCPU' AND '$(Prefer32Bit)' == 'true' ">
<Content Include="$(MSBuildThisFileDirectory)x86\unmanaged.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Link>unmanaged.dll</Link>
</Content>
</ItemGroup>
I tested this with my program to see if it would work and it was a success.我用我的程序对此进行了测试,看看它是否可行并且成功了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.