添加自定义DLL搜索路径@应用程序启动 [英] add custom DLL search path @ application startup

查看:369
本文介绍了添加自定义DLL搜索路径@应用程序启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的大脑提供一个优化的DLL加载问题解决方案。我有一个应用程序静态链接到其他加载DLL的lib文件。我没有直接加载DLL。我想在另一个文件夹中有一些DLL,而不是可执行文件所在的文件夹。像%working_folder%\dlls这样的东西 - 我宁可在我的%working_folder中没有几十个(是...几十个)的DLL %。

I'm racking my brain trying to come up with an elegant solution to a DLL load problem. I have an application that statically links to other lib files which load DLLs. I'm not loading the DLLs directly. I'd like to have some DLLs in another folder other than the folder that the executable is in. Something like %working_folder%\dlls - I'd rather not have dozens (yes ... dozens) of DLLs in my %working_folder%.

我正在尝试开发一些可以调整搜索路径@启动的应用程序的一部分。我遇到的问题是这个新的自定义DLL路径不在系统搜索路径中。当我启动应用程序崩溃(STATUS_DLL_NOT_FOUND),因为必要的DLL不在适当的地方。我想做的是检查@启动,如果这个新的自定义DLL文件夹在进程环境变量搜索路径,如果不添加。问题是,应用程序会尝试在应用程序执行一行代码之前加载所有这些DLL。

I'm trying to develop something that is part of the main app that will adjust the search path @ startup. The problem I'm running into is that this new custom DLL path isn't in the system search path. When I start the app it crashes (STATUS_DLL_NOT_FOUND) because the necessary DLLs are not in the appropriate places. What I'd like to do is to check @ startup if this new custom DLL folder is in the process environment variable search path and if not add it. Problem is, the application attempts to load all these DLLs before the app executes one line of code.

如何解决这个问题?我考虑编写一个首先启动的帮助应用程序,适当调整环境变量,并通过CreateProcess启动主应用程序。这将是我确定的,但它使事情困难的开发人员。当他们调试主应用程序时,他们不会首先启动一个帮助应用程序 - 而不是他们甚至可以这样做。

How do I fix this? I've considered writing a help app that starts first, adjusts the environment variables appropriately and the launches the main app via CreateProcess. This will work I'm sure of it but it makes things difficult on the developers. When they debug the main app they're not going to launch a helper app first - not that they could even do that.

我尝试了注册表应用程序路径功能没有成功。同样的鸡蛋和鸡蛋的问题与以前一样。

I've tried the registry app path feature with no success. Same chicken and egg problem as before.

我可以在这里做什么?

推荐答案

为我工作。

在visual studio 2012中,转到您的项目属性和
配置属性 - >链接器 - >输入 - >延迟加载的数据
添加您想要加载的每个dll文件,直到需要。

In visual studio 2012 goto your project properties and in Configuration Properties->Linker->Input->Delay Loaded Dlls add each dll file that you want to not load until needed.

尽管它不再需要在main之前运行,这是我的代码设置新的搜索路径



Although it no longer needs to run before main, this is my code to set the new search path

class RunBeforeMain
{
public:
    RunBeforeMain()
    {
        const TCHAR* dllPathEnvName= name of env variable to directory containing dlls
        const TCHAR* pathEnvName= TEXT("Path");


        TCHAR newSearchPath[4096];
        ::GetEnvironmentVariable(dllPathEnvName, newSearchPath, MAX_PATH);

             //append bin
        _tcscat_s(newSearchPath, MAX_PATH, TEXT("bin;"));
        size_t length = _tcslen(newSearchPath);

            //append existing Path
        ::GetEnvironmentVariable(pathEnvName, newSearchPath + length, 4096-length);
        ::SetEnvironmentVariable(pathEnvName, newSearchPath);

    }
};
static RunBeforeMain runBeforeMain; //constructor code will run before main.

这篇关于添加自定义DLL搜索路径@应用程序启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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