Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Getting an exe's directory.

PhrostBytePhrostByte Posts: 47Member
I'm trying to get the dir an exe resides in (not current working dir), and i am coding with win32 so there is no argv[]. Somebody, if there is a function out there please god tell me lol i've been looking for days.

------------------------------------------
The art of war is not to die for your country, but to make the other bastard die for his!
«1

Comments

  • PlanetCppPlanetCpp Posts: 30Member
    GetCurrentDirectory(bufferlength,buffer)
    as long as you dont change it with setcurrentdirectory itll be the processes directory it was started in,,the exe directory.
    if you do change it, well then get it before you do.

    : I'm trying to get the dir an exe resides in (not current working dir), and i am coding with win32 so there is no argv[]. Somebody, if there is a function out there please god tell me lol i've been looking for days.
    :
    : ------------------------------------------
    : The art of war is not to die for your country, but to make the other bastard die for his!
    :

  • PlanetCppPlanetCpp Posts: 30Member
    use MAX_PATH for buffer lenghts when delaing with file path names, its a constant of the maximum allowed path name lenght the system allows.
    char mydirectory[MAX_PATH] = {""};
    GetCurrentDirectory(MAX_PATH,mydirectory);

  • PhrostBytePhrostByte Posts: 47Member
    : GetCurrentDirectory(bufferlength,buffer)
    : as long as you dont change it with setcurrentdirectory itll be the processes directory it was started in,,the exe directory.
    : if you do change it, well then get it before you do.

    Actually that will get the current working directory, which is set to whatever folder you run the exe from. I found the function after hunting through MSDN some more.

    LPTSTR GetCommandLine(void); is the function I was looking for, it returns an unraveled argv[], with the exe's path in double-quotes in front.

    ------------------------------------------
    The art of war is not to die for your country, but to make the other bastard die for his!

  • AsmGuru62AsmGuru62 Posts: 6,519Member
    : : GetCurrentDirectory(bufferlength,buffer)
    : : as long as you dont change it with setcurrentdirectory itll be the processes directory it was started in,,the exe directory.
    : : if you do change it, well then get it before you do.
    :
    : Actually that will get the current working directory, which is set to whatever folder you run the exe from. I found the function after hunting through MSDN some more.
    :
    : LPTSTR GetCommandLine(void); is the function I was looking for, it returns an unraveled argv[], with the exe's path in double-quotes in front.
    :
    : ------------------------------------------
    : The art of war is not to die for your country, but to make the other bastard die for his!
    :
    :
    [code]
    char strExePath [MAX_PATH];
    GetModuleFileName (NULL, strExePath, MAX_PATH);
    [/code]
  • PlanetCppPlanetCpp Posts: 30Member
    the current working directory is the directory the exe is in unless you change it, ive never had a problem with it

    : : GetCurrentDirectory(bufferlength,buffer)
    : : as long as you dont change it with setcurrentdirectory itll be the processes directory it was started in,,the exe directory.
    : : if you do change it, well then get it before you do.
    :
    : Actually that will get the current working directory, which is set to whatever folder you run the exe from. I found the function after hunting through MSDN some more.
    :
    : LPTSTR GetCommandLine(void); is the function I was looking for, it returns an unraveled argv[], with the exe's path in double-quotes in front.
    :
    : ------------------------------------------
    : The art of war is not to die for your country, but to make the other bastard die for his!
    :
    :

  • RaizoRaizo Posts: 131Member
    : the current working directory is the directory the exe is in unless you change it, ive never had a problem with it
    :
    : : : GetCurrentDirectory(bufferlength,buffer)
    : : : as long as you dont change it with setcurrentdirectory itll be the processes directory it was started in,,the exe directory.
    : : : if you do change it, well then get it before you do.
    : :
    : : Actually that will get the current working directory, which is set to whatever folder you run the exe from. I found the function after hunting through MSDN some more.
    : :
    : : LPTSTR GetCommandLine(void); is the function I was looking for, it returns an unraveled argv[], with the exe's path in double-quotes in front.
    : :
    : : ------------------------------------------
    : : The art of war is not to die for your country, but to make the other bastard die for his!
    : :
    : :
    :
    :
    If this is a C++ question, then _ _FILE_ _ will give the entire directory, but it includes the filename.
  • tsagldtsagld Posts: 621Member
    : : the current working directory is the directory the exe is in unless you change it, ive never had a problem with it
    : :
    : : : : GetCurrentDirectory(bufferlength,buffer)
    : : : : as long as you dont change it with setcurrentdirectory itll be the processes directory it was started in,,the exe directory.
    : : : : if you do change it, well then get it before you do.
    : : :
    : : : Actually that will get the current working directory, which is set to whatever folder you run the exe from. I found the function after hunting through MSDN some more.
    : : :
    : : : LPTSTR GetCommandLine(void); is the function I was looking for, it returns an unraveled argv[], with the exe's path in double-quotes in front.
    : : :
    : : : ------------------------------------------
    : : : The art of war is not to die for your country, but to make the other bastard die for his!
    : : :
    : : :
    : :
    : :
    : If this is a C++ question, then _ _FILE_ _ will give the entire directory, but it includes the filename.
    :
    That's bull. __FILE__ gives the name of the source file, not the exe.

    Greets,
    Eric Goldstein
    www.gvh-maatwerk.nl

  • AsmGuru62AsmGuru62 Posts: 6,519Member
    : the current working directory is the directory the exe is in unless you change it, ive never had a problem with it
    [blue]---------------------------
    That depends on how you launch your EXE.
    1. Put your compiled program into C:TEMPMYEXE folder
    2. Go to DOS Prompt
    3. In DOS Prompt change your folder into C:TEMP
    4. Launch your EXE with '.MYEXEMYPROG.EXE'
    5. The working directory will be C:TEMP and not C:TEMPMYEXE

    The correct way is still Win32 API [b]GetModuleFileName()[/b].[/blue]
  • RaizoRaizo Posts: 131Member
    : : : the current working directory is the directory the exe is in unless you change it, ive never had a problem with it
    : : :
    : : : : : GetCurrentDirectory(bufferlength,buffer)
    : : : : : as long as you dont change it with setcurrentdirectory itll be the processes directory it was started in,,the exe directory.
    : : : : : if you do change it, well then get it before you do.
    : : : :
    : : : : Actually that will get the current working directory, which is set to whatever folder you run the exe from. I found the function after hunting through MSDN some more.
    : : : :
    : : : : LPTSTR GetCommandLine(void); is the function I was looking for, it returns an unraveled argv[], with the exe's path in double-quotes in front.
    : : : :
    : : : : ------------------------------------------
    : : : : The art of war is not to die for your country, but to make the other bastard die for his!
    : : : :
    : : : :
    : : :
    : : :
    : : If this is a C++ question, then _ _FILE_ _ will give the entire directory, but it includes the filename.
    : :
    : That's bull. __FILE__ gives the name of the source file, not the exe.
    sorry
    :
    : Greets,
    : Eric Goldstein
    : www.gvh-maatwerk.nl
    :
    :

  • dskvdskv Posts: 2Member
    U r right. Because if you have file associations set up on your system with the files that where created by your program and you saved them to the Desktop and then open them from the Desktop then your working directory will be the Desktop and not the Program_Files/Application/*.exe directory where your programs EXE is at.

    The Current Working Directory will be changed as soon as the program is Launched and so there is no way to capture it if you loaded the program from somewhere else, like a file from the Desktop.

    There is also an small issue with the program shortcuts, but that's another topic.
«1
Sign In or Register to comment.