获取当前目录
- 1、GetCurrentDirectory()获取:
函数原型:
DWORD GetCurrentDirectory(
DWORD nBufferLength, // buffer长度
LPTSTR lpBuffer // buffer
);
函数调用成功会返回目录装到buffer中的长度。
- 2、使用GetModuleFileName()获取完整路径:
原型
DWORD WINAPI GetModuleFileName(
HMODULE hModule, //null表示当前模块,也可以是LoadLiberary得到的模块句柄
LPTSTR lpFileName,//buffer
DWORD nSize//长度
);
获取模块(dll,exe等)的运行目录,包括程序文件名。
代码:
WCHAR path[MAX_PATH + 1] = { 0 };
GetCurrentDirectory(MAX_PATH,path);
WCHAR m_path[MAX_PATH + 1] = { 0 };
GetModuleFileName(NULL, m_path, MAX_PATH);
CString m_dic(m_path);
m_dic = m_dic.Left(m_dic.ReverseFind('\\'));
MessageBox(CString("path:")+path+CString("\r\nmodule_path:")+m_path+CString("\r\nmodule_dic:")+m_dic);
效果:
小结:
- 1、GetCurrentDirectory()得到的路径是可修改的,不一定是想要的,可能得到的不是预期的结果
- 2、GetModuleFileName()能得到模块包括文件名在内的完整路径,要获得目录的话去掉文件名即可