Q: How to get the application directory?
A: Here is a simple example that calls GetModuleFileName to get the full path of the executable file of the current process, then PathRemoveFileSpec to remove the executable file name.
DWORD GetApplicationDirectory(CString& strAppDir)
DWORD dwRet = NO_ERROR;
// retrieve the full path of the executable file of the current process
if(0 != ::GetModuleFileName(NULL, CStrBuf(strAppDir, MAX_PATH), MAX_PATH))
// remove the executable file name
// see what's going wrong
dwRet = ::GetLastError();
- CStrBuf is an MFC/ATL shared class that helps to simplify code when need to pass CString buffer as output parameter.