Current Filename and File path

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mike C#

    #1

    Current Filename and File path

    Hi all,

    Probably a really simple answer to this, but so far I'm having trouble
    locating the answer. I'm writing an unmanaged VC++ app and I need to grab
    the currently executing filename and file path from inside the application.
    So if I am in the directory C:\Program Files\Test_Dir and I run TestApp
    (.exe file), I need to grab the current directory ("C:\Program
    Files\Test_Dir" ) and the application's full filename ("TestApp.exe") . Any
    ideas are appreciated.

    Thanks


  • William DePalo [MVP VC++]

    #2
    Re: Current Filename and File path

    "Mike C#" <xyz@xyz.com> wrote in message
    news:urH3SqmiGH A.1600@TK2MSFTN GP04.phx.gbl...[color=blue]
    > I'm writing an unmanaged VC++ app and I need to grab the currently
    > executing filename and file path from inside the application.[/color]

    Check the docs for GetModuleFileNa me(0, ...);

    Regards,
    Will



    Comment

    • Mike C#

      #3
      Re: Current Filename and File path

      Thanks, I'll look into it. I was afraid I would have to do something funky
      with the process id for a minute :) Thanks

      "William DePalo [MVP VC++]" <willd.no.spam@ mvps.org> wrote in message
      news:uix5IzmiGH A.4368@TK2MSFTN GP03.phx.gbl...[color=blue]
      > "Mike C#" <xyz@xyz.com> wrote in message
      > news:urH3SqmiGH A.1600@TK2MSFTN GP04.phx.gbl...[color=green]
      >> I'm writing an unmanaged VC++ app and I need to grab the currently
      >> executing filename and file path from inside the application.[/color]
      >
      > Check the docs for GetModuleFileNa me(0, ...);
      >
      > Regards,
      > Will
      >
      >
      >[/color]


      Comment

      • Mike C#

        #4
        Re: Current Filename and File path

        "Mike C#" <xxx@yyy.com> wrote in message news:0zLhg.205$ Nb7.84@fe12.lga ...[color=blue][color=green]
        >> Check the docs for GetModuleFileNa me(0, ...);
        >>
        >> Regards,
        >> Will[/color][/color]

        Thanks Will, it works great!

        By the way, is there a method for grabbing resources (i.e., version number,
        copyright, etc.) from within the current process? Currently I'm having to
        open the file again (hence the GetModuleFileNa me call) to read the
        resources. It seems like there should be a way to grab the resources from
        memory, since the file is already loaded and executing. Thanks again!


        Comment

        • William DePalo [MVP VC++]

          #5
          Re: Current Filename and File path

          "Mike C#" <xyz@xyz.com> wrote in message
          news:%23a2i4Mxi GHA.2456@TK2MSF TNGP04.phx.gbl. ..[color=blue]
          > Thanks Will,[/color]

          You are welcome.
          [color=blue]
          > it works great![/color]

          I try hard not to give bogus information. :-)
          [color=blue]
          > By the way, is there a method for grabbing resources (i.e., version
          > number, copyright, etc.) from within the current process? Currently I'm
          > having to open the file again (hence the GetModuleFileNa me call) to read
          > the resources.[/color]

          How are you opening it? If it is with GetFileVersionI nfo() then I think that
          that is the canonical way.

          Regards,
          Will


          Comment

          • Mike C#

            #6
            Re: Current Filename and File path


            "William DePalo [MVP VC++]" <willd.no.spam@ mvps.org> wrote in message
            news:u$SGfaxiGH A.412@TK2MSFTNG P05.phx.gbl...[color=blue][color=green]
            >> Thanks Will,[/color]
            >
            > You are welcome.
            >[color=green]
            >> it works great![/color]
            >
            > I try hard not to give bogus information. :-)
            >[color=green]
            >> By the way, is there a method for grabbing resources (i.e., version
            >> number, copyright, etc.) from within the current process? Currently I'm
            >> having to open the file again (hence the GetModuleFileNa me call) to read
            >> the resources.[/color]
            >
            > How are you opening it? If it is with GetFileVersionI nfo() then I think
            > that that is the canonical way.
            >[/color]

            Yeah I'm using GetFileVersionI nfo(), but it just seems odd that I have to
            grab the file by pathname to get the resource information of itself! :) I
            thought there might be a method out there to grab the resources from memory
            without all the hocus-pocus, but hey this method works!

            Thanks again!


            Comment

            • William DePalo [MVP VC++]

              #7
              Re: Current Filename and File path

              "Mike C#" <xyz@xyz.com> wrote in message
              news:urXjIixiGH A.1272@TK2MSFTN GP03.phx.gbl...[color=blue]
              > Yeah I'm using GetFileVersionI nfo(), but it just seems odd that I have to
              > grab the file by pathname to get the resource information of itself! :)[/color]

              Well, it is a resource so there just may be, but being the lazy &^%$# that I
              am, I have never researched an alternative. :-)

              Seriously, with the application running, at least sone of its image file
              must be loaded in memory. I am happy to let the cache manager sort it out.

              Are you building a resource editor or something?

              Regards,
              Will


              Comment

              • Mike C#

                #8
                Re: Current Filename and File path

                "William DePalo [MVP VC++]" <willd.no.spam@ mvps.org> wrote in message
                news:uliHLvxiGH A.2188@TK2MSFTN GP04.phx.gbl...[color=blue]
                > Well, it is a resource so there just may be, but being the lazy &^%$# that
                > I am, I have never researched an alternative. :-)
                >
                > Seriously, with the application running, at least sone of its image file
                > must be loaded in memory. I am happy to let the cache manager sort it out.
                >
                > Are you building a resource editor or something?
                >[/color]

                Nah, nothing that fancy :) I'm re-writing some applications here at work,
                and I don't like the fact that all the "metadata" is hard-coded into the
                apps. The previous designer hard-coded app name, version, etc. using
                #define style constants. Some of the app names and versions changed, but
                the hard-coded info wasn't updated along the way in many cases -- so I
                figured I'd use resources to add some flexibility and as a better method of
                keeping all this metadata in sync, since I'm rewriting these things from
                scratch anyway. :)


                Comment

                • William DePalo [MVP VC++]

                  #9
                  Re: Current Filename and File path

                  "Mike C#" <xyz@xyz.com> wrote in message
                  news:%23mXfTpyi GHA.1320@TK2MSF TNGP04.phx.gbl. ..[color=blue]
                  > so I figured I'd use resources to add some flexibility and as a better
                  > method of keeping all this metadata in sync, since I'm rewriting these
                  > things from scratch anyway. :)[/color]

                  I see. As I said, I've always used GetFileVersionI nfo...() functions.

                  But I just tried this quick and awful hack

                  HRSRC hrsrc = FindResource(hI nst, VS_VERSION_INFO , VERSIONINFO);
                  HGLOBAL hglbl = LoadResource(hI nst, hrsrc);
                  LPVOID pv = LockResource(hg lbl);

                  UINT u;
                  LPCSTR p;
                  VerQueryValue(p v, "\\StringFileIn fo\\040904B0\\F ileDescription" , (LPVOID
                  *)&p, &u);

                  and it seems to do the right thing.

                  Regards,
                  Will


                  Comment

                  • Mike C#

                    #10
                    Re: Current Filename and File path

                    "William DePalo [MVP VC++]" <willd.no.spam@ mvps.org> wrote in message
                    news:uwoXRxziGH A.3848@TK2MSFTN GP04.phx.gbl...[color=blue]
                    > "Mike C#" <xyz@xyz.com> wrote in message
                    > news:%23mXfTpyi GHA.1320@TK2MSF TNGP04.phx.gbl. ..[color=green]
                    >> so I figured I'd use resources to add some flexibility and as a better
                    >> method of keeping all this metadata in sync, since I'm rewriting these
                    >> things from scratch anyway. :)[/color]
                    >
                    > I see. As I said, I've always used GetFileVersionI nfo...() functions.
                    >
                    > But I just tried this quick and awful hack
                    >
                    > HRSRC hrsrc = FindResource(hI nst, VS_VERSION_INFO , VERSIONINFO);
                    > HGLOBAL hglbl = LoadResource(hI nst, hrsrc);
                    > LPVOID pv = LockResource(hg lbl);
                    >
                    > UINT u;
                    > LPCSTR p;
                    > VerQueryValue(p v, "\\StringFileIn fo\\040904B0\\F ileDescription" ,
                    > (LPVOID *)&p, &u);[/color]

                    Thanks William! I ran your sample, and ran into a problem or two. Finally
                    got it to work but I think you're right - reading the resources from the
                    file is probably my best bet right now :) Now I'm running into another
                    problem with a possible memory leak... will the fun never end?


                    Comment

                    • William DePalo [MVP VC++]

                      #11
                      Re: Current Filename and File path

                      "Mike C#" <xyz@xyz.com> wrote in message
                      news:%23mod6Cxj GHA.4588@TK2MSF TNGP05.phx.gbl. ..[color=blue]
                      > Thanks William![/color]

                      You are welcome.
                      [color=blue]
                      > I ran your sample, and ran into a problem or two.[/color]

                      It was an awful hack. I did work here. Just by the way, the IDE assigns an
                      ID of 1 (VS_VERSION_INF O) to the resource. I'm not sure if that is a
                      convention or a requirement.
                      [color=blue]
                      > Now I'm running into another problem with a possible memory leak... will
                      > the fun never end?[/color]

                      :-)

                      Regards,
                      Will


                      Comment

                      • Mike C#

                        #12
                        Re: Current Filename and File path

                        "William DePalo [MVP VC++]" <willd.no.spam@ mvps.org> wrote in message
                        news:et%23AYuyj GHA.4584@TK2MSF TNGP05.phx.gbl. ..[color=blue][color=green]
                        >> I ran your sample, and ran into a problem or two.[/color]
                        >
                        > It was an awful hack. I did work here. Just by the way, the IDE assigns an
                        > ID of 1 (VS_VERSION_INF O) to the resource. I'm not sure if that is a
                        > convention or a requirement.
                        >[/color]

                        Yeah to get it to work here I had to use a string identifier "#1". It did
                        work, but I have more confidence in your original suggestion :) Thanks!


                        Comment

                        Working...