我们有两个版本的托管C++程序集,一个用于x86,另一个用于x64.此程序集由符合AnyCPU的.net应用程序调用.我们正在通过文件复制安装部署我们的代码,并希望继续这样做.
当应用程序动态选择其处理器体系结构时,是否可以使用并排程序集清单分别加载x86或x64程序集?或者是否有另一种方法可以在文件复制部署中完成此操作(例如,不使用GAC)?
这是情况,我在我的dot.net应用程序中使用基于C的dll.有2个dll,一个是32位称为MyDll32.dll,另一个是64位版本,名为MyDll64.dll.
有一个保存DLL文件名的静态变量:string DLL_FILE_NAME.
它以下列方式使用:
[DllImport(DLL_FILE_NAME, CallingConvention=CallingConvention.Cdecl, EntryPoint=Func1")]
private static extern int is_Func1(int var1, int var2);
Run Code Online (Sandbox Code Playgroud)
到目前为止简单.
可以想象,该软件是在打开"Any CPU"的情况下编译的.
我还有以下代码来确定系统是否应该使用64位文件或32位文件.
#if WIN64
public const string DLL_FILE_NAME = "MyDll64.dll";
#else
public const string DLL_FILE_NAME = "MyDll32.dll";
#endif
Run Code Online (Sandbox Code Playgroud)
到目前为止,您应该看到问题.. DLL_FILE_NAME是在编译时定义的,而不是在执行时间中定义的,因此根据执行上下文不会加载右dll.
处理这个问题的正确方法是什么?我不想要两个执行文件(一个用于32位,另一个用于64位)?如何在DllImport语句中使用DLL_FILE_NAME 之前设置它?
我有一个SQLite程序集的引用,一个用于32位,一个用于64位,看起来像这样(这是一个试图摆脱警告的测试项目,不要挂在路径上) :
<Reference Condition=" '$(Platform)' == 'x64' " Include="System.Data.SQLite, Version=1.0.61.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139, processorArchitecture=AMD64">
<SpecificVersion>True</SpecificVersion>
<HintPath>..\..\LVK Libraries\SQLite3\version_1.0.65.0\64-bit\System.Data.SQLite.DLL</HintPath>
</Reference>
<Reference Condition=" '$(Platform)' == 'x86' " Include="System.Data.SQLite, Version=1.0.65.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139, processorArchitecture=x86">
<SpecificVersion>True</SpecificVersion>
<HintPath>..\..\LVK Libraries\SQLite3\version_1.0.65.0\32-bit\System.Data.SQLite.DLL</HintPath>
</Reference>
Run Code Online (Sandbox Code Playgroud)
这会产生以下警告:
Warning 1 The referenced component 'System.Data.SQLite' could not be found.
Run Code Online (Sandbox Code Playgroud)
我有可能摆脱这个警告吗?
我开发时只考虑将项目配置为32位的一种方法,让构建机器在构建64位时修复引用,但这看起来有点尴尬,可能容易出错.
还有其他选择吗?
我想摆脱它的原因是警告显然被TeamCity收集并定期标记为我需要调查的东西,所以我想完全摆脱它.
编辑:根据答案,我试过这个:
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
...
<SqlitePath>..\..\LVK Libraries\SQLite3\version_1.0.65.0\32-bit</SqlitePath>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
...
<SqlitePath>..\..\LVK Libraries\SQLite3\version_1.0.65.0\32-bit</SqlitePath>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x64' ">
...
<SqlitePath>..\..\LVK Libraries\SQLite3\version_1.0.65.0\64-bit</SqlitePath>
</PropertyGroup>
<PropertyGroup …
Run Code Online (Sandbox Code Playgroud) c# ×3
.net ×2
32bit-64bit ×1
64-bit ×1
conditional ×1
dllimport ×1
pinvoke ×1
project ×1
reference ×1