Posted By

jimfred on 04/17/09


Versions (?)

Get application's full path, MFC, using AfxGetApp()->m_pszExeName, GetModuleHandle and GetModuleFileName

 / Published in: C++


For C++ MFC applications, this code gets the full path of the application (even if the application is a DLL run with RunDll32).

  1. AfxGetApp()->m_pszExeName; // Get the "MyExe" portion of "MyExe.exe". Or, "MyDll" if RunDll32 is used.
  3. dllName.Append( ".exe" ); // Now has "MyExe.exe" (or "MyDll.dll").
  5. HMODULE hmod = GetModuleHandle(dllName);
  7. CString fullPath;
  8. DWORD pathLen = ::GetModuleFileName( hmod, fullPath.GetBufferSetLength(MAX_PATH+1), MAX_PATH); // hmod of zero gets the main EXE
  9. fullPath.ReleaseBuffer( pathLen ); // Note that ReleaseBuffer doesn't need a +1 for the null byte.

Report this snippet  

You need to login to post a comment.