更改静态链接DLL的DLL搜索路径 [英] Altering DLL search path for static linked DLL

查看:260
本文介绍了更改静态链接DLL的DLL搜索路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经搜索了任何提示,我可以如何做,但我发现是如何将SxS DLL重定向到本地应用程序文件夹。
这是我想要完成的:
(C ++)Application.exe链接到DLL,Plugin.DLL(依赖项目)。这个DLL没有放在应用程序目录中,而是放在一个名为plugins的子文件夹中。由于DLL是静态链接的,应用程序将尝试从应用程序文件夹中加载它。

I've searched for any hints how I can do this, but all I found was how to redirect a SxS DLL to the local application folder. Here is what I want to accomplish: The (C++) Application.exe is linked to a DLL, Plugin.DLL (dependant project). This DLL is not placed inside the application directory, but in a subfolder called "plugins". As the DLL is statically linked, the application would try to load it from the application folder.

有没有办法如何更改这个特定的DLL的搜索路径?通过清单或VS2008链接器配置?

Is there any way how I can change the search path for this particular DLL? Either through manifests or VS2008 linker configurations?

推荐答案

我的第一个想法是,如果你静态链接一个dll,它不是一个插件。只需将DLL放在EXE文件夹中即可完成。这就是Windows支持的部署配置,用于静态加载的DLL。

My first thought is, if you are statically linking a dll, it isnt a plugin. Just put the dll in the EXE folder and be done with it. That is the deployment configuration supported by windows for statically loaded DLLs.

那就是说,有办法实现你想要的。但是它们大多是愚蠢的,或者是复杂的,没有什么好的理由:你的选择是:

That said, there are ways to achieve what you want. But they are mostly stupid, or complicated for no good reason: Your options are:


  • 不要静态链接。使用LoadLibrary(plugins / Plugin.dll)& GetProcAddress访问插件内容。

  • 将插件文件夹的路径添加到系统PATH环境变量中。

  • 使用延迟加载机制来延迟访问插件功能,设置 custom helper function ,可以使用提供的路径加载dll。

  • 将plugins文件夹转换为程序集(通过创建一个.manifest文件中列出了plugin.dll)。将插件作为依赖程序集添加到您的应用程序。现在,它将在plugins文件夹中查找。

  • 将应用程序分解为存根exe和动态加载的部分。在stub exe调用SetDllDirectory来指向plugin文件夹,然后调用LoadLibrary将完整路径传递给appstub.dll。

  • Don't statically link. Use LoadLibrary("plugins/Plugin.dll") & GetProcAddress to access plugin content.
  • Add "the path to your plugins folder" to the systems PATH environment variable.
  • Use the delay load mechanism to delay accessing the plugins functionality, set a custom helper function that can load the dll(s) using a provided path.
  • Turn the plugins folder into an assembly (by creating a .manifest file in it that lists the plugin.dll). Add "plugins" as a dependent assembly to your app. Now it will look in the plugins folder.
  • Split your application into a stub exe and a dynamically loaded part. In the stub exe call SetDllDirectory to point to the plugin folder, then call LoadLibrary passing the full path to "appstub.dll".

要将一个或多个dll的文件夹转换为程序集,只需将文件添加到文件夹name.manifest的文件夹中。

To turn a folder, with one or more dll's into an "assembly", simply add a file to the folder with the folders name.manifest.

所以,plugins.manifest: -

So, plugins.manifest :-

<assembly manifestVersion="1.0">
  <assemblyIdentity type="Win32" name="Plugins" version="1.0.0.0" processorArchitecture="x86" />
  <file name="Plugin.dll"/>
</assembly>

确保文件夹和dll的名称不同,这是一个非常好的主意,就好像dll名称是组件名称窗口开始查看其嵌入式清单文件以获取有关程序集的信息。

It is a VERY good idea to ensure that the folder and the dll's name is different as if the dll name is the assembly name windows starts looking at its embedded manifest file for information about the assembly.

假设您正在使用Visual Studio 7或更高版本,将以下指令添加到项目中的.c / .cpp或.h文件将使您的应用程序尝试从程序集加载dll,而不仅仅是本地目录:

Assuming you are using Visual Studio 7 or later, the following directive added to a .c/.cpp or .h file in the project will then make your application attempt to load dlls from the assembly rather than just the local directory:

#pragma comment(linker, "/manifestdependency:\"name='Plugins' "\
                        "processorArchitecture='*' version='1.0.0.0' "\
                        "type='win32'\"")

这篇关于更改静态链接DLL的DLL搜索路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆