使用SetEnvironmentVariable调整应用程序环境变量中的path设置
2012-11-15 15:26
811 查看
在开发软件时,碰到了有一大批的dll需要加载,且这些dll中有隐式链接到其它dll情况.由于某些原因,不能将dll放入系统目录中也不能将他们放置在应用程序同一目录中.
为集中管理,将其放置到应用程序目录下的字目录MyDllPath目录下.
当使用LoadLibrary加载dll时会由于dll中存在隐式链接,且被链接的dll不在当前路径下(在MyDllPath路径下)而导致加载失败的情况.
这时,可以使用GetEnvironmentVariable/SetEnvironmentVariable来调整本应用程序的路径设定.将MyDllPath加载到本应用程序的当前路径中.这样即可正常加载所需要的dll了.
如下是修改当前应用程序目录路径的方法:
根据MSDN.应用程序在加载dll时,所搜索的路径如下(Windows 2000/NT):
The directory from which the application loaded.
The current directory.
The system directory. Use the GetSystemDirectory function to get the path of this directory.
The 16-bit system directory. There is no function that obtains the path of this directory, but it is searched.
The Windows directory. Use the GetWindowsDirectory function to get the path of this directory.
The directories that are listed in the PATH environment variable.
更详细的信息可以参考msdn http://msdn.microsoft.com/en-us/library/windows/desktop/ms682586%28v=vs.85%29.aspx
为集中管理,将其放置到应用程序目录下的字目录MyDllPath目录下.
当使用LoadLibrary加载dll时会由于dll中存在隐式链接,且被链接的dll不在当前路径下(在MyDllPath路径下)而导致加载失败的情况.
这时,可以使用GetEnvironmentVariable/SetEnvironmentVariable来调整本应用程序的路径设定.将MyDllPath加载到本应用程序的当前路径中.这样即可正常加载所需要的dll了.
如下是修改当前应用程序目录路径的方法:
BOOL CDemoApp::SetCurrentEnvPath() { char chBuf[0x8000]={0}; DWORD dwSize =GetEnvironmentVariable("path",chBuf,0x10000); CString strEnvPaths(chBuf); // 将当前路径\dll路径添加到本进程的路径中 if(!::GetModuleFileName(NULL,chBuf,MAX_PATH)) return FALSE; CString strAppPath(chBuf); const int nPos = strAppPath.ReverseFind(_T('\\')); if(nPos>0){ // 路径中包含最后的'\\' strAppPath = strAppPath.Mid(0,nPos+1); } strEnvPaths.TrimRight(";"); strEnvPaths += ";" + strAppPath +"MyDllPath;"; BOOL bRet = SetEnvironmentVariable("path",strEnvPaths); return bRet; }
根据MSDN.应用程序在加载dll时,所搜索的路径如下(Windows 2000/NT):
The directory from which the application loaded.
The current directory.
The system directory. Use the GetSystemDirectory function to get the path of this directory.
The 16-bit system directory. There is no function that obtains the path of this directory, but it is searched.
The Windows directory. Use the GetWindowsDirectory function to get the path of this directory.
The directories that are listed in the PATH environment variable.
更详细的信息可以参考msdn http://msdn.microsoft.com/en-us/library/windows/desktop/ms682586%28v=vs.85%29.aspx
相关文章推荐
- Python环境变量PYTHONPATH设置和easy_install简单使用
- dos命令之 set (显示、设置或删除 cmd.exe 环境变量) 用法详解以及使用变量
- Python环境变量PYTHONPATH设置和easy_install简单使用
- eclipse下环境变量设置:eclipse导入工程出现 Unbound classpath variable Error
- 设置Path变量 - Batch Script To Manage Windows PATH Environment Variable
- 获取和更改环境变量的值SetEnvironmentVariable()
- 使用set命令设置环境变量
- 如何使用脚本set-env来设置环境变量[转]
- Python环境变量PYTHONPATH设置和easy_install简单使用
- 设置PATH环境变量,及CRegKey类的使用
- 修改环境变量(GetEnvironmentVariable、SetEnvironmentVariable、GetEnvironmentStrings)
- Linux下环境变量PATH设置错误,导致命令都找不到,无法使用
- eclipse中使用maven的jar包,但是提示unbound classpath variable,eclipse没装maven插件,在eclipse里new一个环境变量指向maven的repo
- 【转】在setclasspath.bat文件中设置tomcat环境变量
- eclipse下环境变量设置:eclipse导入工程出现 Unbound classpath variable Error
- u-boot环境变量的设置与使用
- redhat中设置环境变量PATH的方法
- 在Linux里设置环境变量的方法(export PATH)--路径
- Linux 环境变量PATH设置
- tomcat配置及使用(环境变量设置及测试,一个简单的web应用实例)