任何依赖于c++/ CLI依赖于本机C dll的CPU(任何用于c++/ CLI的CPU)

时间:2021-09-03 05:01:21

Here's my problem. I am wrapping a C dll in C#. To do this, I am first writing a C++/CLI wrapper. The native C library is linked to the C++/CLI wrapper. (Linker properties in C++/cli project).

这是我的问题。我正在用c#包装一个cdll。为此,我首先编写一个c++ /CLI包装器。本机C库链接到c++ /CLI包装器。(c++ /cli项目中的链接器属性)。

Here's how it is all organised now: - The native C .lib: both x86 and 64bit.

以下是它现在的组织方式:-本机C .lib: x86和64位。

  • 1 solution containing 2 projects:
    • C++/CLI wrapper project to which is linked native C .lib
    • C+ /CLI包装器项目,链接到本机C .lib
    • C# project referencing C++/CLI project
    • c#项目引用c++ /CLI项目
  • 一个包含两个项目的解决方案:c++ /CLI包装项目,它链接到本机C .lib c#项目,引用c++ /CLI项目

My problem comes from the fact that I need C# to target "Any CPU". But this option is not available in C++/CLI since it compiles directly to native code.

我的问题来自这样一个事实:我需要c#来锁定“任何CPU”。但是这个选项在c++ /CLI中不可用,因为它直接编译为本地代码。

My idea to solve this is: - Compile C++/CLI wrapper in x86 and then change the config and compile to 64 bit. When it compiles, I would like to tell it which dll to take based on the platform. ie: if compiling in 64bit, link 64 bit native C dll, else if x86, link x86 native C. - With this done, I should then be able to have Any CPU target in my C# platform. Here again, instead of referencing my C++/CLI wrapper project, I would reference the required dll based on the target platform.

我解决这个问题的想法是:-在x86中编译c++ /CLI包装器,然后更改配置并编译为64位。当它编译时,我想告诉它基于平台要采用哪个dll。如果编译在64位,链接64位本机C dll,如果x86,链接x86本机C -这样做,我应该能够在我的c#平台上有任何CPU目标。在这里,我将根据目标平台引用所需的dll,而不是引用我的c++ /CLI包装器项目。

My questions are:

我的问题是:

  • How to I tell the C++/CLI project which .lib to link to based on the target platform?
  • 如何告诉c++ /CLI项目基于目标平台链接到哪个.lib ?
  • How to I tell the C# project which C++/CLI dll to reference based on the target platform?
  • 如何告诉c#项目在目标平台上要引用哪个c++ /CLI dll ?

Let me add that the C# project a CLASS LIBRARY to be used by an x86 or x64 client.

让我添加c#项目一个类库,供x86或x64客户机使用。

I hope my question is clear enough. Any helps would be appreciated !

我希望我的问题足够清楚。如有任何帮助,我们将不胜感激!

UPDATE based on:Conditional references in .NET project, possible to get rid of warning?...

基于:.NET项目中的条件引用,是否可以消除警告?

So now I've edited my .csproj file using a condition to reference the dll as follows:

现在我已经编辑了我的。csproj文件,使用一个条件引用dll:

<ItemGroup>
    <Reference Include="AlibCppWrapper, Version=1.0.4303.21410, Culture=neutral, PublicKeyToken=c0c17a53adc44091, processorArchitecture=AMD64"
               Condition="$(Platform) == 'x64'">
      <SpecificVersion>False</SpecificVersion>
      <HintPath>..\x64\Debug\AlibCppWrapper.dll</HintPath>
    </Reference>
    <Reference Include="AlibCppWrapper, Version=1.0.4303.21410, Culture=neutral, PublicKeyToken=c0c17a53adc44091, processorArchitecture=x86"
               Condition="$(Platform) == 'x86'">
      <SpecificVersion>False</SpecificVersion>
      <HintPath>..\Debug\AlibCppWrapper.dll</HintPath>
    </Reference>
  </ItemGroup>

Unfortunately this doesn't work as the $(Platform) is set to AnyCPU...

不幸的是,这并不起作用,因为$(平台)被设置为任何cpu……

1 个解决方案

#1


22  

What you describe is known as "side-by-side assembly" (two versions of the same assembly, one 32 and the other 64 bit)... I think you will find these helpful:

您所描述的是“并行程序集”(同一程序集的两个版本,一个32位,另一个64位)……我想你会发现这些有用的:

EDIT - as per comment:

编辑-根据评论:

Here you can find a walkthrough for exactly your scenario (.NET DLL wrapping C++/CLI DLL referencing a native DLL) http://scottbilas.com/blog/automatically-choose-32-or-64-bit-mixed-mode-dlls/

在这里,您可以找到一个完整的场景演练(。NET DLL包装C++/CLI DLL引用一个本地DLL) http://scottbilas.com/blog/automatically-choos-32or -64位-混合模式-dlls/

#1


22  

What you describe is known as "side-by-side assembly" (two versions of the same assembly, one 32 and the other 64 bit)... I think you will find these helpful:

您所描述的是“并行程序集”(同一程序集的两个版本,一个32位,另一个64位)……我想你会发现这些有用的:

EDIT - as per comment:

编辑-根据评论:

Here you can find a walkthrough for exactly your scenario (.NET DLL wrapping C++/CLI DLL referencing a native DLL) http://scottbilas.com/blog/automatically-choose-32-or-64-bit-mixed-mode-dlls/

在这里,您可以找到一个完整的场景演练(。NET DLL包装C++/CLI DLL引用一个本地DLL) http://scottbilas.com/blog/automatically-choos-32or -64位-混合模式-dlls/