windows动态库相互调用可能存在的问题,当可执行文件与动态库不在一个路径下时会存在找不到动态库的依赖库的情况,从而导致程序崩溃。
linux可能不存在这种问题是因为linux动态库文件本身的elf包含了其搜索依赖库路径的信息,编译时makefile有 -rpath选项。
解决办法是,使用这个动态库时设置当前工作路径,使用完恢复原路径。
需要包含头文件
#if _MSC_VER > 1000
#include <windows.h>
#endif
当程序为多字节程序时:
char szOldPath[512] = { 0 };
GetCurrentDirectoryA(512, szOldPath);
SetCurrentDirectoryA(str_init_path.c_str());
// do something
SetCurrentDirectoryA(szOldPath);
当程序使用unicode时,一般路径用wstring和wchar表示:
wchar_t szOldPath[512] = {0};
GetCurrentDirectory(512,szOldPath);
SetCurrentDirectory(DllPath);
// do something
SetCurrentDirectory(szOldPath);
参考:GetModuleFileNameA()与GetCurrentDirectoryA()