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. The Lounge
  3. how to know the exact path from where a program starts ?

how to know the exact path from where a program starts ?

Scheduled Pinned Locked Moved The Lounge
c++tutorialquestion
16 Posts 10 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.
  • G Offline
    G Offline
    Guy Lecomte
    wrote on last edited by
    #1

    Hello I need in my project to know the file location of the runnig program, from within this program. In clear words, the path where the EXE files starts. How to do it in VC++ (if I remember there is something like app.path in VB) I've looked the CWinApp class without rigth answer.:(( Can anybody do something nice fo me ?:-D regards Guy LECOMTE

    T M J L L 5 Replies Last reply
    0
    • G Guy Lecomte

      Hello I need in my project to know the file location of the runnig program, from within this program. In clear words, the path where the EXE files starts. How to do it in VC++ (if I remember there is something like app.path in VB) I've looked the CWinApp class without rigth answer.:(( Can anybody do something nice fo me ?:-D regards Guy LECOMTE

      T Offline
      T Offline
      Tim Ranker
      wrote on last edited by
      #2

      One possibilty which I use is to create a member variable in your CWinApp derived class to store the path and add the following code to the top of InitInstance: ///////////////////////////////////////////////////////////////////////////// // CYourApp initialization BOOL CYourApp::InitInstance() { // Get current directory, which should be where the executable is // located, and save it. ::GetModuleFileName(NULL, m_strAppPath.GetBuffer(MAX_PATH), MAX_PATH); m_strAppPath.ReleaseBuffer(); ... Kind regards, Tim

      G 1 Reply Last reply
      0
      • G Guy Lecomte

        Hello I need in my project to know the file location of the runnig program, from within this program. In clear words, the path where the EXE files starts. How to do it in VC++ (if I remember there is something like app.path in VB) I've looked the CWinApp class without rigth answer.:(( Can anybody do something nice fo me ?:-D regards Guy LECOMTE

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

        char szAppName[2000]; memset(szAppName, 0, 2000); ::GetModuleFileName(AfxGetInstanceHandle(), szAppName, 2000);

        G J 2 Replies Last reply
        0
        • T Tim Ranker

          One possibilty which I use is to create a member variable in your CWinApp derived class to store the path and add the following code to the top of InitInstance: ///////////////////////////////////////////////////////////////////////////// // CYourApp initialization BOOL CYourApp::InitInstance() { // Get current directory, which should be where the executable is // located, and save it. ::GetModuleFileName(NULL, m_strAppPath.GetBuffer(MAX_PATH), MAX_PATH); m_strAppPath.ReleaseBuffer(); ... Kind regards, Tim

          G Offline
          G Offline
          Guy Lecomte
          wrote on last edited by
          #4

          I tried your solution, and it fails : I explain :the result is the same as the _getcwd. Try this : while programming, start the program, it will show the project root directory but the location of the exe is in the /debug subdirectory. :(( Try this now: make a shortcut in your desk, leaving the "starting from" entry to blank, the run from the shortcut, the program is still in the /debug subdir, but the message will be your /desktop dir ! :mad: So , has anybody already solve this problem ? hope so :-D So is there a solution ? here is the chalenge ! Regards Guy LECOMTE

          T 1 Reply Last reply
          0
          • G Guy Lecomte

            I tried your solution, and it fails : I explain :the result is the same as the _getcwd. Try this : while programming, start the program, it will show the project root directory but the location of the exe is in the /debug subdirectory. :(( Try this now: make a shortcut in your desk, leaving the "starting from" entry to blank, the run from the shortcut, the program is still in the /debug subdir, but the message will be your /desktop dir ! :mad: So , has anybody already solve this problem ? hope so :-D So is there a solution ? here is the chalenge ! Regards Guy LECOMTE

            T Offline
            T Offline
            Tim Ranker
            wrote on last edited by
            #5

            Matt is correct. I took a look at my code again and verified that I actually do use ::GetModuleFileName(NULL, m_strAppPath.GetBuffer(MAX_PATH), MAX_PATH); My original GetCurrentDirectory was wrong because if the "Start from" is left blank then your current directory would not be the same as the exe pathname. I tried your shortcut test and it worked fine with GetModuleFileName. Kind regards, Tim

            1 Reply Last reply
            0
            • M Matt Gullett

              char szAppName[2000]; memset(szAppName, 0, 2000); ::GetModuleFileName(AfxGetInstanceHandle(), szAppName, 2000);

              G Offline
              G Offline
              Guy Lecomte
              wrote on last edited by
              #6

              thank you very much Matt !:-D :-D :-D now is the time to have a cold drink in a parisian café ! What a hot shinnig sun !:cool: :cool: (it is the afternoon break) Best regards Guy LECOMTE

              1 Reply Last reply
              0
              • G Guy Lecomte

                Hello I need in my project to know the file location of the runnig program, from within this program. In clear words, the path where the EXE files starts. How to do it in VC++ (if I remember there is something like app.path in VB) I've looked the CWinApp class without rigth answer.:(( Can anybody do something nice fo me ?:-D regards Guy LECOMTE

                J Offline
                J Offline
                jerry0davis
                wrote on last edited by
                #7

                Err why not just use __argv[0] ??? Am I missing something?

                A 1 Reply Last reply
                0
                • J jerry0davis

                  Err why not just use __argv[0] ??? Am I missing something?

                  A Offline
                  A Offline
                  Alvaro Mendez
                  wrote on last edited by
                  #8

                  If I remember correctly, __argv[0] only gives you the program's whole path when you explicitly run it with it. For example, if I'm at the command prompt and say: c:\abc\def>c:\xyz\opq\myProg.exe __arg[0] will be equal to "c:\xyz\opq\myProg.exe" But if I just say c:\xyz\opt>myProg.exe __arg[0] will be equal to "myProg.exe" Regards, Alvaro

                  P 1 Reply Last reply
                  0
                  • M Matt Gullett

                    char szAppName[2000]; memset(szAppName, 0, 2000); ::GetModuleFileName(AfxGetInstanceHandle(), szAppName, 2000);

                    J Offline
                    J Offline
                    John Fisher
                    wrote on last edited by
                    #9

                    If you aren't using MFC, replace "AfxGetInstanceHandle()" with "GetModuleHandle(NULL)". John

                    1 Reply Last reply
                    0
                    • A Alvaro Mendez

                      If I remember correctly, __argv[0] only gives you the program's whole path when you explicitly run it with it. For example, if I'm at the command prompt and say: c:\abc\def>c:\xyz\opq\myProg.exe __arg[0] will be equal to "c:\xyz\opq\myProg.exe" But if I just say c:\xyz\opt>myProg.exe __arg[0] will be equal to "myProg.exe" Regards, Alvaro

                      P Offline
                      P Offline
                      Pavlos Touboulidis
                      wrote on last edited by
                      #10

                      Nop, Jeremy's right. Just create a new MFC Dialog app and try this: BOOL CMFCTestApp::InitInstance() { AfxMessageBox(__argv[0]); return(FALSE);

                      T 1 Reply Last reply
                      0
                      • P Pavlos Touboulidis

                        Nop, Jeremy's right. Just create a new MFC Dialog app and try this: BOOL CMFCTestApp::InitInstance() { AfxMessageBox(__argv[0]); return(FALSE);

                        T Offline
                        T Offline
                        Tim Ranker
                        wrote on last edited by
                        #11

                        Nope. Alvaro is 100% correct. argv[0] is the name by which the program was invoked. Need proof? 1. Bring up a command window. 2. Change directory to the directory where the app is located. 3. Launch the app by typing just <appname>. _argv[0] will only be <appname> and not the full app path. Kind regards, Tim

                        P 1 Reply Last reply
                        0
                        • T Tim Ranker

                          Nope. Alvaro is 100% correct. argv[0] is the name by which the program was invoked. Need proof? 1. Bring up a command window. 2. Change directory to the directory where the app is located. 3. Launch the app by typing just <appname>. _argv[0] will only be <appname> and not the full app path. Kind regards, Tim

                          P Offline
                          P Offline
                          Pavlos Touboulidis
                          wrote on last edited by
                          #12

                          OK! Something is wrong here! These links target to 3 small gif files, which open new browser windows. Image1 Image2 Image3 It seems to be working for me, as it always did. Including console apps, and DOS apps. :confused:

                          T 1 Reply Last reply
                          0
                          • P Pavlos Touboulidis

                            OK! Something is wrong here! These links target to 3 small gif files, which open new browser windows. Image1 Image2 Image3 It seems to be working for me, as it always did. Including console apps, and DOS apps. :confused:

                            T Offline
                            T Offline
                            Tim Ranker
                            wrote on last edited by
                            #13

                            Hello Pavlos, All my previous tests were done using Windows NT 4 or Windows 2000. After running the app on a 9X machine, I get the same results you did. Apparently 9X returns the whole pathname regardless of where it is launched, NT does not. FYI, Solaris behaves the same way as NT. Even the prestigious book "The C Programming Language" by Brian W Kernighan and Dennis M Ritchie state the following: "By convention, argv[0] is the name by which the program was invoked,..." Therefore, it looks like using GetModuleFileName() and stripping off the filename(_splitpath) is the safest way to get the location of the exe. Another way to do all of this assuming the application is using a logo compliant installer is to check the registry under HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\<YourAppName>.exe Value name: Path The "Path" value name should contain the path of where the app is installed. Kind regards, Tim

                            G 1 Reply Last reply
                            0
                            • T Tim Ranker

                              Hello Pavlos, All my previous tests were done using Windows NT 4 or Windows 2000. After running the app on a 9X machine, I get the same results you did. Apparently 9X returns the whole pathname regardless of where it is launched, NT does not. FYI, Solaris behaves the same way as NT. Even the prestigious book "The C Programming Language" by Brian W Kernighan and Dennis M Ritchie state the following: "By convention, argv[0] is the name by which the program was invoked,..." Therefore, it looks like using GetModuleFileName() and stripping off the filename(_splitpath) is the safest way to get the location of the exe. Another way to do all of this assuming the application is using a logo compliant installer is to check the registry under HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\<YourAppName>.exe Value name: Path The "Path" value name should contain the path of where the app is installed. Kind regards, Tim

                              G Offline
                              G Offline
                              Gennady Oster
                              wrote on last edited by
                              #14

                              Hi, Tim! Maybe you know also how to correctly split the UNC? _tsplitpath for UNC returns empty drive, interpreting the server and the share parts as directories. Regards, Gennady P.S. After posting this message I've found here on the CP site the article introducing simple CSplitPath class. The small addition enables you to split UNC too. Gennady

                              1 Reply Last reply
                              0
                              • G Guy Lecomte

                                Hello I need in my project to know the file location of the runnig program, from within this program. In clear words, the path where the EXE files starts. How to do it in VC++ (if I remember there is something like app.path in VB) I've looked the CWinApp class without rigth answer.:(( Can anybody do something nice fo me ?:-D regards Guy LECOMTE

                                L Offline
                                L Offline
                                leo 0
                                wrote on last edited by
                                #15

                                CWinApp::m_pszHelpFilePath will also give the application path, just remove the helpfile name. (i.e. unless you have specifically changed the default) e.g. BOOL CYourApp::InitInstance() { CString sHelpFile = m_pszHelpFilePath; int t = sHelpFile.ReverseFind('\\'); CString sAppPath= sHelpFile.Left(t+1); Regards Leo

                                1 Reply Last reply
                                0
                                • G Guy Lecomte

                                  Hello I need in my project to know the file location of the runnig program, from within this program. In clear words, the path where the EXE files starts. How to do it in VC++ (if I remember there is something like app.path in VB) I've looked the CWinApp class without rigth answer.:(( Can anybody do something nice fo me ?:-D regards Guy LECOMTE

                                  L Offline
                                  L Offline
                                  Lost User
                                  wrote on last edited by
                                  #16

                                  Use the API function - GetModuleFileName.

                                  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