September 10, 2012

Get the Application Directory in C++


BOOL GetApplicationDirectory(CString& _sApplicationDirectory)
{
        TCHAR szPathName[_MAX_PATH];
        if (FAILED(::GetModuleFileName(NULL, szPathName, _MAX_PATH)))
        {
               return FALSE;
        }

        TCHAR szDrive[_MAX_DRIVE];
        TCHAR szDirectory[_MAX_DIR];
        TCHAR szFileName[_MAX_FNAME];
        TCHAR szExtension[_MAX_EXT];

        // Split path
        _tsplitpath_s(szPathName, szDrive, _MAX_DRIVE, szDirectory, _MAX_DIR,
               szFileName, _MAX_FNAME, szExtension, _MAX_EXT);

        // Make safe path
        _tmakepath_s(szPathName, _MAX_PATH, szDrive, szDirectory, _T(""), _T(""));

        _sApplicationDirectory = szPathName;

        return TRUE;
}

No comments: