如何告诉 QPluginLoader 检查包含文件夹而不是 exe 文件夹中的 dll 依赖项
How to tell QPluginLoader to check for dll dependencies in containing folder instead of exe folder
我有一个 dll,它是某种插件,我打算将它部署为一个包,dll 及其所有依赖项(也是 dll)在一个包中。我遇到的问题是 dll 在 exe 文件夹而不是它自己的文件夹中检查它的依赖项,它的依赖项在它旁边。
有没有办法告诉它依赖项在哪里?
编辑:
在我的例子中,插件是由 QPluginLoader 加载的,答案暗示这是相关的,因为加载程序决定在哪里寻找依赖项。
Note that the standard search strategy and the alternate search strategy specified by LoadLibraryEx
with LOAD_WITH_ALTERED_SEARCH_PATH
differ in just one way: The standard search begins in the calling application's directory, and the alternate search begins in the directory of the executable module that LoadLibraryEx
is loading.
这正是您所描述的行为。您不能从构建系统更改此行为(关于 [cmake] 标记),但需要修改应用程序的 plugin-loading 逻辑。
正如@Alex Reinking 指出的,加载器负责查找 dll,在我的例子中,加载是由 QPluginLoader 完成的,解决方案是将当前目录设置为插件目录,因为加载器在当前目录中查找对于 dll:
QDir pluginsDir(QLatin1String("../src/"));
QDir::setCurrent(pluginsDir.path());
QPluginLoader pluginLoader(pluginsDir.absoluteFilePath(fileName));
我有一个 dll,它是某种插件,我打算将它部署为一个包,dll 及其所有依赖项(也是 dll)在一个包中。我遇到的问题是 dll 在 exe 文件夹而不是它自己的文件夹中检查它的依赖项,它的依赖项在它旁边。
有没有办法告诉它依赖项在哪里?
编辑: 在我的例子中,插件是由 QPluginLoader 加载的,答案暗示这是相关的,因为加载程序决定在哪里寻找依赖项。
Note that the standard search strategy and the alternate search strategy specified by
LoadLibraryEx
withLOAD_WITH_ALTERED_SEARCH_PATH
differ in just one way: The standard search begins in the calling application's directory, and the alternate search begins in the directory of the executable module thatLoadLibraryEx
is loading.
这正是您所描述的行为。您不能从构建系统更改此行为(关于 [cmake] 标记),但需要修改应用程序的 plugin-loading 逻辑。
正如@Alex Reinking 指出的,加载器负责查找 dll,在我的例子中,加载是由 QPluginLoader 完成的,解决方案是将当前目录设置为插件目录,因为加载器在当前目录中查找对于 dll:
QDir pluginsDir(QLatin1String("../src/"));
QDir::setCurrent(pluginsDir.path());
QPluginLoader pluginLoader(pluginsDir.absoluteFilePath(fileName));