首页 > 解决方案 > 通过线程设置DllDirectory

问题描述

为了在不同的线程中使用不可重入的 DLL,我将它复制到 4 个不同的文件夹中。这很丑陋但强制性...这个 DLL 由我的应用程序手动加载并使用另一个子库(隐式链接到另一个子库)。所以我有这样的事情:

folder1/
   lib1.dll
   sublib.dll
folder2/
   lib2.dll
   sublib.dll

我设法加载了不同版本的 lib.dll(lib1 和 lib2),但加载的 sublib.dll 对于两者都保持不变。我尝试了 SetDllDirectory 和 AddDllDirectory,但它没有加载多个 sublib.dll。

 for (size_t i { 1 }; i <= threadsCount; ++i)
 {
        SetDllDirectoryA(nullptr); // Reset.
        //SetDllDirectoryA(""); // Plug "binary planting" security hole. `
        SetDllDirectoryA(dirPath(i).c_str());   // folderX

        HMODULE module = LoadLibraryExA(libPath(i).c_str(), nullptr, LOAD_LIBRARY_SEARCH_USER_DIRS);
        if (!module )
        {
            std::cout << "error loading dll" << std::endl;
            return false;
        }

        // Used after
        // FooPrototype foo = (FooPrototype) GetProcAddress(module, "foo");
}

// Run threads with the different 'foo'

是否可以加载 2 个 sublib.dll 实例?

谢谢 !

标签: c++windowsmultithreadingdll

解决方案


推荐阅读