简体   繁体   English

如何根据CPU架构使用正确的非托管DLL文件? (32/64位)

[英]How to use the correct unmanaged DLL file according CPU architecture? (32 / 64 bits)

I have to use a C++ DLL file from an ASP.NET site. 我必须使用ASP.NET站点的C ++ DLL文件。 The site will be hosted on both 32 and 64 bits environments. 该站点将托管在32位和64位环境中。

I have a 32 and 64 bits version of the unmanaged DLL file. 我有一个32位和64位版本的非托管DLL文件。 How do I import the methods from the correct one according the current server architecture? 如何根据当前服务器体系结构从正确的方法导入方法?

Like someone posted, this could be handled as a deployment problem. 就像有人发布的那样,这可以作为部署问题来处理。 My concern is: the web application consuming this DLL file is huge, and we don't have a deployment document. 我担心的是:使用此DLL文件的Web应用程序非常庞大,而且我们没有部署文档。 No one will remember to deploy the correct DLL file, so I'm trying to remove the human factor from the solution :) 没有人会记得部署正确的DLL文件,所以我试图从解决方案中删除人为因素:)

Simplest approach would be to give the two native libraries the same filename in two different directories, then adjust your application DLL search path depending on the bitness. 最简单的方法是在两个不同的目录中为两个本机库提供相同的文件名,然后根据位数调整应用程序DLL搜索路径

http://www.pinvoke.net/default.aspx/kernel32.setdlldirectory http://www.pinvoke.net/default.aspx/kernel32.setdlldirectory

To get the compiler/framework to do most of the work you need to 让编译器/框架完成您需要完成的大部分工作

  1. have multiple build 'platforms' (typically x86, x64 - remove AnyCPU) 有多个构建'平台'(通常是x86,x64 - 删除AnyCPU)
  2. set each "platform target" configuration for each build config 为每个构建配置设置每个“平台目标”配置
  3. we added conditional compilation symbols __WIN32 & __X64 我们添加了条件编译符号__WIN32和__X64

List the different implementations for your functions according to platform, including different dll names if you need to have both installed at once. 根据平台列出功能的不同实现,包括不同的dll名称,如果您需要同时安装两者。

#if __WIN32
        public delegate int Move(int target);
        [DllImport("my.dll", SetLastError = true, CharSet = CharSet.Auto)]
#elif __X64
        public delegate int Move(int target);
        [DllImport("my64.dll", SetLastError = true, CharSet = CharSet.Auto)]
#endif

Otherwise you can use loadlib and manage the marshalling yourself. 否则,您可以使用loadlib并自行管理编组。

As we know that we cannot add unmanaged DLL files using add reference. 我们知道我们无法使用添加引用添加非托管DLL文件。 So we need an alternative way to handle unmanaged DLLS files according to CPU architecture that is add DLL files in your project directories. 因此,根据在项目目录中添加DLL文件的CPU架构,我们需要一种替代方法来处理非托管DLLS文件。

  1. Create new folders into your project named x32 and x64. 在名为x32和x64的项目中创建新文件夹。
  2. Copy your DLL files into these folders according to architecture. 根据体系结构将DLL文件复制到这些文件夹中。
  3. Select the files and open properties, Change "Copy to output directory: Copy always" 选择文件并打开属性,更改“复制到输出目录:始终复制”
  4. Repeat this on all files in both folders 对两个文件夹中的所有文件重复此操作

Now build your project in any platform when you will build the project both x86 and x64 folder will be copyed in bin folder. 现在,在构建项目的任何平台上构建项目时,x86和x64文件夹都将被复制到bin文件夹中。 When this will run on server it will use unmanaged DLL files according to CPU architecture. 当它将在服务器上运行时,它将根据CPU架构使用非托管DLL文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM