任何CPU依赖于C ++ / CLI依赖于原生的C DLL(任何CPU的C ++ / CLI)依赖于、CPU、DLL、CLI

2023-09-08 10:15:18 作者:天会黑人会变

下面是我的问题。 我包裹在C#中的C DLL。要做到这一点,我第一次写C ++ / CLI包装。本机C库链接到C ++ / CLI包装。 (链接器的属性在C ++ / CLI项目)。

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的.lib:x86和64位

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

1的解决方案包含2个项目: 在C ++ / CLI包装项目,这是与本地的C的.lib 在C#项目中引用C ++ / CLI项目 1 solution containing 2 projects: C++/CLI wrapper project to which is linked native C .lib C# project referencing C++/CLI project

我的问题来自于一个事实,我需要C#的目标任何CPU。但这种选择是不是在C ++ / CLI中可用,因为它直接编译为本地code。

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 ++ / CLI包装在x86和再更改配置,并编译成64位。当它编译,我想告诉它哪一个DLL基于该平台上采取。即:如果编译在64位,链接64位本机C DLL,否则,如果86,86链接本地C. - 有了这个做,我应该再能有任何CPU的目标在我的C#平台。在这里,而不是引用我的C ++ / CLI包装项目,我会根据目标平台上引用需要的dll。

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.

我的问题是:

如何,我告诉了C ++ / CLI项目,该项目的.lib链接到基于目标平台上? 如何我告诉C#项目,C ++ / CLI DLL来引用基于目标平台上?

让我补充一点,要使用的x86或x64客户端在 C#项目类库。

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

我希望我的问题是清楚。任何帮助将AP preciated!

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

更新基于:Conditional在.NET项目,可以参考摆脱警告? ...

所以,现在我已经使用一个条件来引用的DLL如下编辑我的.csproj文件:

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

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

不幸的是,这并不作为$(平台)被设置为值为anycpu ...

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

推荐答案

你描述的被称为并排侧大会(两个版本相同的组件,一32和其他64位)...我想你会发现这些有用的:

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:

Using侧并排组件加载DLL 的64或X32版本 http://blogs.msdn.com/b/gauravseth/archive/2006/03/07/545104.aspx http://www.thescarms.com/dotnet/Assembly.aspx Using Side-by-Side assemblies to load the x64 or x32 version of a DLL http://blogs.msdn.com/b/gauravseth/archive/2006/03/07/545104.aspx http://www.thescarms.com/dotnet/Assembly.aspx

编辑 - 按注释:

在这里,你可以找到一个演练了整整您的方案(.NET的DLL包装C ++ / CLI中引用的DLL本机DLL)http://scottbilas.com/blog/automatically-choose-32-or-64-bit-mixed-mode-dlls/

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/