负载64或取决于平台的x86 DLL?负载、平台、DLL

2023-09-02 01:55:02 作者:删除回忆录丶

我有建为任何CPU的应用程序,并有针对性地x86和x64相同的库两个第三方的DLL。我想包括在运行这些库,这取决于它可以运行在客户机上的平台之一。什么是去了解它的最佳方式?

I have an application built as 'Any CPU' and have two third party Dlls of the same library targeted to x86 and x64. I would like to include one of these libraries at runtime depending upon the platform it would run on the client machine. What would be the best way to go about it ?.

推荐答案

如果我们谈论的非托管的DLL,申报的P /调用是这样的:

If we are talking about unmanaged DLLs, declare the p/invokes like this:

[DllImport("DllName.dll")]
static extern foo();

请注意,我们没有指定的DLL,只是它的名字,这是我presume是相同的32位和64位版本的路径。

Note that we are not specifying a path to the DLL, just its name, which I presume is the same for both 32 and 64 bit versions.

然后,你叫你的任何P /调用之前,加载库到你的进程。做到这一点的P /调用到调用LoadLibrary API函数。在这一点上,你将决定你的进程是否是32位或64位,并建立了完整路径的DLL相应。这完整路径是什么,你传递给调用LoadLibrary

Then, before you call any of your p/invokes, load the library into your process. Do that by p/invoking to the LoadLibrary API function. At this point you will determine whether your process is 32 or 64 bit and build the full path to the DLL accordingly. That full path is what you pass to LoadLibrary.

现在,当你打电话给你的P /调用的库,他们会通过你刚才加载的模块来解决。

Now, when you call your p/invokes for the library, they will be resolved by the module that you have just loaded.

有关托管程序,那么你可以使用 Assembly.LoadFile 来指定程序集的路径。这可能是一个有点棘手编排,但这种优秀的文章将介绍如何:的自动选择32位或64位混合模式的DLL 。有很多有关混合模式的详细信息和本机DLL依赖性,可能与你无关。关键是 AppDomain.CurrentDomain.AssemblyResolve 事件处理程序。

For managed assemblies then you can use Assembly.LoadFile to specify the path of the assembly. This can be a little tricky to orchestrate, but this excellent article shows you how: Automatically Choose 32 or 64 Bit Mixed Mode DLLs. There are a lot of details relating to mixed mode and the native DLL dependencies that are probably not relevant to you. The key is the AppDomain.CurrentDomain.AssemblyResolve event handler.