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

查看:228
本文介绍了修改静态链接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环境变量。

  • 使用延迟加载机制延迟访问插件功能,设置自定义助手函数,可以使用提供的路径加载dll。

  • 将plugins文件夹转换为程序集(通过在其中创建一个.manifest文件plugin.dll)。将插件作为依赖程序集添加到应用程序中。

  • 将您的应用程序分为存根exe和动态加载的部分。在stub exe中调用SetDllDirectory以指向插件文件夹,然后调用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的名称不同, name是程序集名称,Windows开始查看其嵌入的清单文件以获取有关程序集的信息。

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天全站免登陆