GetModuleFileName to get executable

I am working on linux, but this is crossplatform application and i never worked with windows API, so i have little problem with this.


1
2
3
4
5
6
7
8
9
10
#ifdef _WIN32
#include <windows.h>

std::string getExePath()
  {
  char result[ MAX_PATH ];
  return std::string( result, GetModuleFileName( NULL, result, MAX_PATH ) );
  asd = getExePath()  // dont mind the variable name, it will be changed
  }
#endif 

std::cout << asd; latter in program to check the path.
As the documentation says this function should return me the absolute path where program is executed. [Let say D:/MyStuff/Programs/ThisProgram.exe]
So i am in confusion how to get path "D:/MyStuff/Programs/" without "ThisProgram.exe"

If anyone is willing to write me function that removes programName.exe from path i get by calling getExePath()?
And please no boost or mindling with argv[0] :)

thank you in advance
Last edited on
closed account (Dy7SLyTq)
string has find_last_of so you would find the last \ (windows uses \ and linux uses / fyi) so you would have a new string thats constructed with string::substr(find_last_of("\\"));
Are you asking the right question?

If you are wanting where the program is being executed, you should use GetCurrentDirectory

GetCurrentDirectory function
http://msdn.microsoft.com/en-us/library/windows/desktop/aa364934%28v=vs.85%29.aspx

or the equivalent CRT call, _getcwd

_getcwd, _wgetcwd
http://msdn.microsoft.com/en-us/library/sf98bd4y%28v=vs.100%29.aspx

But the current (or working) directory is not always the same as the directory where the excutable file is located.

The approach you are using is the directory where the executable file for the current process is located. If this is the directory you want, then after you have got the full module path using GetModuleFileName you can then use either:
1. std::string functions (as DTSCode suggested)
2. equivalent C library calls (e.g. strrchr)
3. _splitpath and _makepath CRT calls
4. PathRemoveFileSpec from the Path API

Where 4 is prob the most up to date Windows way to do it (or PathCchRemoveFileSpec, if you want to be secure.)

By the way, why do you need to know this folder? For example, if it's to do with storing data, then you should prob. be using a different approach on Windows (which starts off with SHGetKnownFolderPath or SHGetFolderPath.)

Andy

strrchr
http://www.cplusplus.com/reference/cstring/strrchr/

_splitpath, _wsplitpath
http://msdn.microsoft.com/en-us/library/e737s6tf%28v=vs.100%29.aspx

_makepath, _wmakepath
http://msdn.microsoft.com/en-us/library/sh8yw82b%28v=vs.100%29.aspx

PathRemoveFileSpec function
http://msdn.microsoft.com/en-us/library/windows/desktop/bb773748%28v=vs.85%29.aspx
Last edited on
4. PathRemoveFileSpec from the Path API
-Thats what i need.

Are you asking the right question?
If you are wanting where the program is being executed, you should use GetCurrentDirectory
- Will check that one too :)

By the way, why do you need to know this folder?
- Its for loading data, i cant know where the user will start application. Since folder with data will be near executable and getExePath() will tell me where .exe is, and will use that path to find data folder. Thank for your help guys
Play with filenames:
http://www.cplusplus.com/forum/beginner/1962/#msg7180

You should check the return value of GetModuleFileName(). Here's the code I wrote some time ago:
http://v2.cplusplus.com/forum/general/28684/
(scroll down for the Windows stuff)

Anyway...
Topic archived. No new replies allowed.