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. GetCurrentDirectory vs GetModuleFileName

GetCurrentDirectory vs GetModuleFileName

Scheduled Pinned Locked Moved C / C++ / MFC
c++visual-studiolinuxquestion
5 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.
  • B Offline
    B Offline
    Bash
    wrote on last edited by
    #1

    Hi! I develop my own application using Visual C++. I need to get a directory where my application is launched. I've found in MSDN two function: GetCurrentDirectory and GetModuleFileName. It seems that both methods are shown the right results (except for GetModuleFileName I need to make ReverseFind the first slash - but it's not hard to do). What method is the best ? Yours sincerely, Alex Bash

    L 1 Reply Last reply
    0
    • B Bash

      Hi! I develop my own application using Visual C++. I need to get a directory where my application is launched. I've found in MSDN two function: GetCurrentDirectory and GetModuleFileName. It seems that both methods are shown the right results (except for GetModuleFileName I need to make ReverseFind the first slash - but it's not hard to do). What method is the best ? Yours sincerely, Alex Bash

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

      I strongly recommend you to use GetModuleFileName, this will give you the correct result. Be careful with GetCurrentDirectory, this will return the path your app is currently working in. E.g. if you create a file in "C:\Windows", this will be the current directory, regardless of whether your app is located there or not. regards Greg

      B 1 Reply Last reply
      0
      • L Lost User

        I strongly recommend you to use GetModuleFileName, this will give you the correct result. Be careful with GetCurrentDirectory, this will return the path your app is currently working in. E.g. if you create a file in "C:\Windows", this will be the current directory, regardless of whether your app is located there or not. regards Greg

        B Offline
        B Offline
        Bash
        wrote on last edited by
        #3

        At the first state of application (I don't perform any creating of files) - do GetModuleFileName and GetCurrentDirectory show the same results ? Yours sincerely, Alex Bash

        T L 2 Replies Last reply
        0
        • B Bash

          At the first state of application (I don't perform any creating of files) - do GetModuleFileName and GetCurrentDirectory show the same results ? Yours sincerely, Alex Bash

          T Offline
          T Offline
          Tim Smith
          wrote on last edited by
          #4

          They can, but need not. Example: Create a shortcut to your program. Modify the shortcut and specify a different directory for the working directory. Run the program using the shortcut. When you do this, those API routines will return two different things. Tim Smith I'm going to patent thought. I have yet to see any prior art.

          1 Reply Last reply
          0
          • B Bash

            At the first state of application (I don't perform any creating of files) - do GetModuleFileName and GetCurrentDirectory show the same results ? Yours sincerely, Alex Bash

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

            To be honest: I don't know. But I assume that if you call GetCurrentDirectory dierctly after startup it will return the path where your app is located. But I still think it is safer to use GetModuleFileName. /edit: Tim is right :-D regards

            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