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. Detect the path where is running an application VC++

Detect the path where is running an application VC++

Scheduled Pinned Locked Moved C / C++ / MFC
c++question
6 Posts 6 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.
  • N Offline
    N Offline
    nencini
    wrote on last edited by
    #1

    I'd like to detect the folder (path) where a my application is running and put the data in a string. - Can somebody give me some suggestion (without using the system() funcion)? - Thanks - giovanni.

    G R P 3 Replies Last reply
    0
    • N nencini

      I'd like to detect the folder (path) where a my application is running and put the data in a string. - Can somebody give me some suggestion (without using the system() funcion)? - Thanks - giovanni.

      G Offline
      G Offline
      geo_m
      wrote on last edited by
      #2

      If you mean the path where your .exe is located, you can use a function: GetModuleFileName(...), where you put your exe module instance handle and retrieve the whole path including the exe module ("c:\windows\notepad.exe"). Then you have to simply strip the exe name and voila you have path. If you mean the current directory for process, use GetCurrentDirectory()

      R 1 Reply Last reply
      0
      • G geo_m

        If you mean the path where your .exe is located, you can use a function: GetModuleFileName(...), where you put your exe module instance handle and retrieve the whole path including the exe module ("c:\windows\notepad.exe"). Then you have to simply strip the exe name and voila you have path. If you mean the current directory for process, use GetCurrentDirectory()

        R Offline
        R Offline
        Roman Fadeyev
        wrote on last edited by
        #3

        geo_m is completely right. But I would add little remark: you can pass NULL to GetModuleFileName() to retrieve your app path. It's easier to use. Especially it is important to pass NULL when you get AppPath from DLL. How to cut filedir from path? All functions for manipulating with filenames are placed in shlwapi.h. Their names are beginning from "Path" word. For example: PathFindFileName (Searches a path for a file name). Also you can use C++ function _splitpath

        G 1 Reply Last reply
        0
        • R Roman Fadeyev

          geo_m is completely right. But I would add little remark: you can pass NULL to GetModuleFileName() to retrieve your app path. It's easier to use. Especially it is important to pass NULL when you get AppPath from DLL. How to cut filedir from path? All functions for manipulating with filenames are placed in shlwapi.h. Their names are beginning from "Path" word. For example: PathFindFileName (Searches a path for a file name). Also you can use C++ function _splitpath

          G Offline
          G Offline
          Gaul
          wrote on last edited by
          #4

          An example: char szDrive[_MAX_DRIVE]; char szDir[_MAX_DIR]; char szFileName[_MAX_FNAME]; char szExt[_MAX_EXT]; char szBuffer[_MAX_PATH]; // get path of executable GetModuleFileName(NULL, szBuffer, _MAX_PATH); // split into parts, to get the Path const char* pFilePath = szBuffer; _splitpath(pFilePath, szDrive, szDir, szFileName, szExt); CString strPath; strPath.Format("%s%s", szDrive, szDir); TRACE("File Path is %s\n", strPath); Gaulles

          1 Reply Last reply
          0
          • N nencini

            I'd like to detect the folder (path) where a my application is running and put the data in a string. - Can somebody give me some suggestion (without using the system() funcion)? - Thanks - giovanni.

            R Offline
            R Offline
            Ravi Bhavnani
            wrote on last edited by
            #5

            /*
            * Gets the name of the application's directory.
            */
            void getAppDir
            (CString& strAppDir) // buffer to receive application's directory
            {
            char* pSlash = NULL; // location of last backslash in filespec
            char szFilespec [_MAX_PATH]; // filespec

            // Get name of .exe file sans name
            ::GetModuleFileName (AfxGetInstanceHandle(), szFilespec, _MAX_PATH);
            pSlash = strrchr (szFilespec, '\\');
            ASSERT (pSlash != NULL);
            pSlash++;
            *pSlash = '\0';
            strAppDir = szFilespec;
            }

            /ravi Let's put "civil" back in "civilization" http://www.ravib.com ravib@ravib.com

            1 Reply Last reply
            0
            • N nencini

              I'd like to detect the folder (path) where a my application is running and put the data in a string. - Can somebody give me some suggestion (without using the system() funcion)? - Thanks - giovanni.

              P Offline
              P Offline
              pranavamhari
              wrote on last edited by
              #6

              use GetModuleFileName() Hari Krishnan

              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