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.
  2.  
  3. dllName.Append( ".exe" ); // Now has "MyExe.exe" (or "MyDll.dll").
  4.  
  5. HMODULE hmod = GetModuleHandle(dllName);
  6.  
  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.

URL: http://www.codeguru.com/forum/showthread.php?t=312468

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.