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

/ Published in: C++
Save to your folder(s)

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

Copy this code and paste it in your HTML
  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


RSS Icon Subscribe to comments

You need to login to post a comment.