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. Knowing path of MFC executable

Knowing path of MFC executable

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

    Hi All, I want to know the path of an MFC excutable. Whenever an MFC app is executed, I want to know the path of that executable from where it is executing?

    S S C 3 Replies Last reply
    0
    • S Sakhalean

      Hi All, I want to know the path of an MFC excutable. Whenever an MFC app is executed, I want to know the path of that executable from where it is executing?

      S Offline
      S Offline
      ShilpiP
      wrote on last edited by
      #2
          TCHAR Buffer\[MAX\_PATH\];
      DWORD dwRet;
      dwRet = GetModuleFileName(NULL, Buffer, MAX\_PATH);
      

      "Every Little Smile can touch Somebody's Heart... May we find Hundreds of Reasons to Smile Everyday... and May WE be the Reason for someone else to smile always!" (ICAN) "Your thoughts are the architects of your destiny."

      S 1 Reply Last reply
      0
      • S ShilpiP
            TCHAR Buffer\[MAX\_PATH\];
        DWORD dwRet;
        dwRet = GetModuleFileName(NULL, Buffer, MAX\_PATH);
        

        "Every Little Smile can touch Somebody's Heart... May we find Hundreds of Reasons to Smile Everyday... and May WE be the Reason for someone else to smile always!" (ICAN) "Your thoughts are the architects of your destiny."

        S Offline
        S Offline
        Sakhalean
        wrote on last edited by
        #3

        Ok, Thank you a lot.

        P C 2 Replies Last reply
        0
        • S Sakhalean

          Ok, Thank you a lot.

          P Offline
          P Offline
          pix_programmer
          wrote on last edited by
          #4

          GetModuleFileName() Sometimes returns wrong results when you have a deep directory structure. Use _getcwd() instead. I've used both and experienced the above problem with GetModuleFileName().

          L A 2 Replies Last reply
          0
          • S Sakhalean

            Hi All, I want to know the path of an MFC excutable. Whenever an MFC app is executed, I want to know the path of that executable from where it is executing?

            S Offline
            S Offline
            softwaremonkey
            wrote on last edited by
            #5

            Another way is...

            CString appPath = AfxGetApp()->m\_pszHelpFilePath;
            appPath = appPath.Left(appPath.ReverseFind('\\\\') + 1);
            

            :) Tony

            1 Reply Last reply
            0
            • P pix_programmer

              GetModuleFileName() Sometimes returns wrong results when you have a deep directory structure. Use _getcwd() instead. I've used both and experienced the above problem with GetModuleFileName().

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

              Hi, The getcwd function will "get the current working directory" which is not always the same as the directory the process executable is residing in. There are many standard API calls that will change the current working directory of your process. In fact... the CreateProcess function itself has the option of starting your process in an arbitrary working directory. Best Wishes, -David Delaune

              1 Reply Last reply
              0
              • P pix_programmer

                GetModuleFileName() Sometimes returns wrong results when you have a deep directory structure. Use _getcwd() instead. I've used both and experienced the above problem with GetModuleFileName().

                A Offline
                A Offline
                Albert Holguin
                wrote on last edited by
                #7

                getcwd() is not appropriate for this... it returns the current working directory, which can be changed by file open dialogs or any other code that wants to change its working directory.

                1 Reply Last reply
                0
                • S Sakhalean

                  Hi All, I want to know the path of an MFC excutable. Whenever an MFC app is executed, I want to know the path of that executable from where it is executing?

                  C Offline
                  C Offline
                  Chuck OToole
                  wrote on last edited by
                  #8

                  In InitInstance(), member m_pszExeName is your image name. So, use SearchPath() on the result of CString(m_pszExeName) + CString(".exe"). Use the lpFilePart parameter of the call to isolate the path from the file name.

                  1 Reply Last reply
                  0
                  • S Sakhalean

                    Ok, Thank you a lot.

                    C Offline
                    C Offline
                    Charles Oppermann
                    wrote on last edited by
                    #9

                    The C Runtime to the rescue:

                    // Get the full path to our process image
                    _TCHAR *pszExeFullPath;
                    int nReturn = ATL_CRT_ERRORCHECK(_get_tpgmptr(&pszExeFullPath));

                    I use the ATL_CRT_ERRORCHECK() macro to provide some runtime handling of possible CRT errors.

                    /* Charles Oppermann */ http://weblogs.asp.net/chuckop

                    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