有时候,我们想获取本模块或本动态库的路径,而不是运行路径,下面这个函数就可满足:
#include<Windows.h>
#include<string>
using namespace std;
const string& GetModulePath(const HMODULE hMod)
{
static string st_strModulePath;
if(st_strModulePath.size()<1)
{
HMODULE hLocalMoudle=NULL;
::GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,(LPCTSTR)(&GetModulePath),&hLocalMoudle);
char chArr[1024];
::GetModuleFileNameA(hLocalMoudle,(LPSTR)chArr,1024);
string strTmpPath;
strTmpPath=chArr;
int iNumPos=strTmpPath.rfind("\\");
st_strModulePath=strTmpPath.substr(0,iNumPos);
}
st_strModulePath+="\\";
return st_strModulePath;
}