Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to get Folder where the application is installed

How to get Folder where the application is installed

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorial
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    Francis B
    wrote on last edited by
    #1

    I would like to know, how to get the directory where the application is installed. I tried to use _getcmd but this doesn't works correctly. Thanks for your help!

    M Q 2 Replies Last reply
    0
    • F Francis B

      I would like to know, how to get the directory where the application is installed. I tried to use _getcmd but this doesn't works correctly. Thanks for your help!

      M Offline
      M Offline
      Matt Gullett
      wrote on last edited by
      #2

      char szAppName[2000]; char szDrive[200]; char szDir[200]; memset(szAppName, 0, 2000); memset(szDrive, 0, 200); memset(szDir, 0, 200); ::GetModuleFileName(AfxGetInstanceHandle(), szAppName, 2000); _splitpath(szAppName, szDrive, szDir, NULL, NULL);

      1 Reply Last reply
      0
      • F Francis B

        I would like to know, how to get the directory where the application is installed. I tried to use _getcmd but this doesn't works correctly. Thanks for your help!

        Q Offline
        Q Offline
        Quint
        wrote on last edited by
        #3

        The best way to do this is to get the folder from the application .EXE path. To do that, you need to use the Win32 API GetModuleFileName(). That API will return the complete pathname of the calling process' EXE. Unfortunately, it can return an 8.3 style filename and can mangle your directory names. I've included a Win95 safe short->long pathname conversion function ( Win95 doesn't support this ). Finally, once you have a nice looking path, you chop off the .EXE name path off with another funciton I've included GetPathOnlyFromPathname. CString MyPath = GetPathOnlyFromPathname(GetLongPathname(GetAppPathname())); where you have the following 3 utility functions: CString GetAppPathname(void); CString GetLongPathname(const CString & ShortPathname); CString GetPathOnlyFromPathname(const CString & PathName); // returns the full pathname of the application CString CVChatApp::GetAppPathname(void) { char szAppPath[2048]; GetModuleFileName(NULL, szAppPath, sizeof(szAppPath)); return (GetLongPathname(szAppPath)); } The GetModuleFileName call will return an 8.3 style pathname to the EXE. You may want to convert that into a long filename so the directory name doesn't get mangled: // convert a short DOS type 8.3 pathname to a Long Filename is the // version of Windows support the export of the "GetLongPathName" API ( Win95 doesn't ) CString GetLongPathname(const CString & ShortPathname) { CString FullPath; // Win95 doesn't have the GetLongPathName function, // so we need to check the dll to see if it exists to avoid // a fatal run-time error. // Also, note that the name of the function as specified in the DLL // is different than the called name, due to UNICODE macro preprocessors // AND, I think GetLongPathName is a pascal-style called function, so // have to add __stdcall (__cdecl is the default, which is buggy) HMODULE hm; hm = GetModuleHandle("kernel32.dll"); typedef unsigned long (__stdcall *PFI)(LPCTSTR lpszShortPath, LPTSTR lpszLongPath, DWORD cchBuffer); PFI pGetLongPathName; FARPROC pTemp; pTemp = GetProcAddress(hm, "GetLongPathNameA"); char strLongName[MAX_PATH]; strcpy(strLongName, ""); if (pTemp != NULL) { DWORD dw; pGetLongPathName = (PFI)pTemp; dw = (*pGetLongPathName)(ShortPathname, strLongName, MAX_PATH); } else { strcpy(strLongName, ShortPathname); } FullPath = strLongName; return FullPath; } and you'll need one other function: CString GetPathOnlyFromPathname(const CString & PathName

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups