简体   繁体   English

如何检查是否使用 Visual Studio 变量切换了“32 位首选”

[英]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)变量以查看用户是否选择了x86x64AnyCPU并将正确的非托管 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.当用户选择x86x64 ,一切都按预期工作,但在选择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.

相关问题 如何使用 Visual Studio 2022 注册 32 位 COM 互操作 - How to register for 32-Bit COM interop using Visual Studio 2022 为什么在Visual Studio 2012中禁用了“首选32位”复选框? - Why is the checkbox 'Prefer 32-bit' disabled in Visual Studio 2012? Visual Studio 中“首选 32 位”设置的目的是什么?它实际上是如何工作的? - What is the purpose of the "Prefer 32-bit" setting in Visual Studio and how does it actually work? Visual Studio 中的“首选 32 位”在控制台应用程序 (.NET Core) 下生成 64 位代码 - 'Prefer 32-bit' in Visual Studio Generates 64-bit Code Under Console App (.NET Core) 使用Visual Studio 2010时,32位和64位操作系统之间的性能差异? - Performance difference between 32-bit and 64-bit OS when i use Visual Studio 2010? “首选 32 位”编译器标志对 Visual Studio(C#、VB)意味着什么? - What does the “Prefer 32-bit” compiler flag mean for Visual Studio (C#, VB)? Microsoft.VisualStudio.Web.Host.exe使用Visual Studio 2012 / WIn7锁定32位进程 - Microsoft.VisualStudio.Web.Host.exe locking 32-bit processes with Visual Studio 2012/WIn7 如何检查客户端计算机是否正在运行32位或64位OS - How to check if client computer is running a 32-bit or 64-bit OS 如何检查给定地址是 32 位还是 64 位? - How can I check if a given address is 32-bit or 64-bit? 结构大小,检查是64位还是32位 - Struct Size, check if 64-bit or 32-bit
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM