-1

hey so i wanted to get name of the exe that is currently running here is what ive tried doing

#include <iostream> #include <windows.h> using namespace std; int main() { char filename[ MAX_PATH ]; DWORD size = GetModuleFileNameA( NULL, filename); if (size) cout << "EXE file name is: " << filename << "\n"; else cout << "Could not fine EXE file name.\n"; return 0; } 

but it gets the path of the exe too but i only need the exe name any help?

5
  • 4
    Cut at the last backslash and only keep the stuff after...? Btw, "i wanted to get path of the exe" and "it gets the path...but i only need the exe name" are mutually exclusive/confusing. Commented Jun 30, 2020 at 10:28
  • Please include output and expected output in the question. It seems like all you have to do is cut everything before and including the last / Commented Jun 30, 2020 at 10:33
  • Getting the name from the path should be easy - stackoverflow.com/questions/1528298/get-path-of-executable Commented Jun 30, 2020 at 11:09
  • i dont want to get the path. only the name of the exe Commented Jun 30, 2020 at 11:32
  • This is operating system or implementation specific. For Linux, see proc(5). Some C++ implementations don't even have files (read some C++ standard such as n3337 to check). If you use Qt there is a way.... Commented Jul 2, 2020 at 11:21

1 Answer 1

1

The first command line argument is the name of the current program

#include <iostream> #include <string> #include <windows.h> #include <algorithm> int main(int argc, char** argv) { if (argc > 0) std::cout << argv[0] << std::endl; else { //some other method has to be used, use OP's suggestion char filename[ MAX_PATH ]; DWORD size = GetModuleFileNameA( NULL, filename, MAX_PATH); if (!size) { std::cout << "Could not fine EXE file name.\n"; return -1; } //Remove everything before the last "\" std::string name = filename; auto it = std::find(name.rbegin(), name.rend(), '\\'); //escape the escape character if (it != name.rend()) { name.erase(name.begin(), it.base()); } std::cout << filename << std::endl; } } 
Sign up to request clarification or add additional context in comments.

7 Comments

This a nice easy solution for console applications. This will not work if it is a GUI application
There's no guarantee that argv[0] is the name of the application executable. If I use fork() + ones of the exec family of functions to launch your program I can make argv[0] be whatever I please. That argv[0] contains the executable name is only a convention, you cannot rely on it.
According to learn.microsoft.com/en-us/cpp/cpp/… the first argument is "the command with which the program is invoked" if it exists.
@Mestkon See learn.microsoft.com/en-us/cpp/c-runtime-library/reference/… - you can make argv[0] whatever you want. Convention says it should be the same as the program name, but nothing enforces that. Same deal on UNIX.
none of em worked :(
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.