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