VC++ 中LoadLibrary的路径问题
来源:互联网 发布:阿儿法营编程学校 编辑:程序博客网 时间:2024/06/05 15:16
已有动态库A.dll, 创建动态库B.dll和C.exe
在B.dll中编写:LoadLibrary("A.dll")
在C.exe中编写:LoadLibrary("B.dll")
把A.dll和B.dll放到C.exe相同的目录,运行C.exe正常。
把A.dll和B.dll放到文件夹DLL中,在C.exe 中改为LoadLibrary(“DLL\B.dll”),运行失败。
原因在于LoadLibrary搜索dll的顺序为:
The directory from which the application loaded. (应用程序所在的目录)
The system directory. Use the GetSystemDirectory function to get the path of this directory. (system32目录)
The 16-bit system directory. There is no function that obtains the path of this directory, but it is searched. (System目录)
The Windows directory. Use the GetWindowsDirectory function to get the path of this directory. (Windows目录)
The current directory. (不清楚这个目录和应用程序所在的目录有什么区别)
The directories that are listed in the PATH environment variable. Note that this does not include the per-application path specified by the App Paths registry key. (PATH 路径)
The system directory. Use the GetSystemDirectory function to get the path of this directory. (system32目录)
The 16-bit system directory. There is no function that obtains the path of this directory, but it is searched. (System目录)
The Windows directory. Use the GetWindowsDirectory function to get the path of this directory. (Windows目录)
The current directory. (不清楚这个目录和应用程序所在的目录有什么区别)
The directories that are listed in the PATH environment variable. Note that this does not include the per-application path specified by the App Paths registry key. (PATH 路径)
加载B.dll时,在exe所在目录能够查找到“DLL\B.dll”,所以能够加载到B.dll,但是加载A.dll时,在所有目录中都找不到"A.dll",所以加载失败。 在B.dll中写成“A.dll”的目的就是使用A相对于B的相对路径来加载A.dll, 但是LoadLibrary并没有使用到B的路径。
解决方法:使用SetDllDirectory增加DLL搜索路径,
HMODULE hMod = GetModuleHandle(("B.dll"));
if (hMod != NULL)
{
TCHAR szBuffer[MAX_PATH] = { 0 };
GetModuleFileName(hMod, szBuffer, sizeof(szBuffer) / sizeof(TCHAR) - 1);
string str;
str.append(szBuffer);
size_t pos=str.find_last_of("\\");
str.erase(pos);
SetDllDirectory(str.c_str());
if (hMod != NULL)
{
TCHAR szBuffer[MAX_PATH] = { 0 };
GetModuleFileName(hMod, szBuffer, sizeof(szBuffer) / sizeof(TCHAR) - 1);
string str;
str.append(szBuffer);
size_t pos=str.find_last_of("\\");
str.erase(pos);
SetDllDirectory(str.c_str());
LoadLibrary("A.dll")
SetDllDirectory(null);
SetDllDirectory(null);
}
阅读全文
0 0
- VC++ 中LoadLibrary的路径问题
- LoadLibrary的路径问题
- java 中System.loadLibrary()调用c++Dll的路径问题
- Eclipse中java工程中引用dll库的路径设置 && System.loadLibrary()调用Dll路径问题
- JNA路径问题--Native.loadLibrary("NLPIR", CLibrary.class)的坑
- LoadLibrary文件路径及windows API相关的文件路径问题
- LoadLibrary文件路径及windows API相关的文件路径问题
- VC中关于路径中含有空格问题的解决办法
- System.loadLibrary("cocos2dcpp"); 的问题
- windows编程中Loadlibrary()动态数据库问题
- LoadLibrary加载dll失败的问题
- 我在做一个项目中碰到的loadLibrary的问题
- 在 Android 系统中直接调用 SO文件(包含System.loadLibrary加载so的路径解释)
- 在 Android 系统中直接调用 SO文件(包含System.loadLibrary加载so的路径解释)
- 在 Android 系统中直接调用 SO文件(包含System.loadLibrary加载so的路径解释)
- 在 Android 系统中直接调用 SO文件(包含System.loadLibrary加载so的路径解释)
- Java中System.loadLibrary() 的执行过程
- Java中System.loadLibrary() 的执行过程
- Libevent源码分析-----连接监听器evconnlistener
- Guava学习记录
- NodeVisitor
- js获取当前时间并存到数据中
- C#关于委托的那些事儿
- VC++ 中LoadLibrary的路径问题
- Comparing system calls and C libraries
- 棋盘问题
- BZOJ 3065 带插入区间K小值
- Shell 定时5分钟一次监控Tomcat状态,自动重启
- iOS tableView滑动卡顿的问题之一
- Android之xml解析和json解析
- 【线段树-区间更新求区间和】CDOJ 1057
- 《Java并发编程实战》笔记