-
June 1st, 2014, 10:08 AM
#1
MFC/ATL: How to get the application directory?
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.
Code:
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
::PathRemoveFileSpec(CStrBuf(strAppDir, MAX_PATH));
}
else
{
// see what's going wrong
dwRet = ::GetLastError();
}
return dwRet;
}
Note
- CStrBuf is an MFC/ATL shared class that helps to simplify code when need to pass CString buffer as output parameter.
See also
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|