-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?

TDS
  • 21
  • 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. – underscore_d Jun 30 '20 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 `/` – 463035818_is_not_an_ai Jun 30 '20 at 10:33
  • Getting the name from the path should be easy - https://stackoverflow.com/questions/1528298/get-path-of-executable – Mircea Ispas Jun 30 '20 at 11:09
  • i dont want to get the path. only the name of the exe – TDS Jun 30 '20 at 11:32
  • **This is operating system or implementation specific**. For Linux, see [proc(5)](https://man7.org/linux/man-pages/man5/proc.5.html). **Some C++ implementations don't even have files** (read some C++ standard such as [n3337](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3337.pdf) to check). If you use [Qt](http://qt.io/) there is a way.... – Basile Starynkevitch Jul 02 '20 at 11:21

1 Answers1

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;

    }
}
Mestkon
  • 3,532
  • 7
  • 18
  • 1
    This a nice easy solution for console applications. This will not work if it is a GUI application – Sisir Jun 30 '20 at 10:40
  • 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. – Jesper Juhl Jun 30 '20 at 10:49
  • According to https://learn.microsoft.com/en-us/cpp/cpp/main-function-command-line-args?redirectedfrom=MSDN&view=vs-2019 the first argument is "the command with which the program is invoked" if it exists. – Mestkon Jun 30 '20 at 10:51
  • @Mestkon See https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/execl-wexecl?view=vs-2019 - 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. – Jesper Juhl Jun 30 '20 at 11:23
  • none of em worked :( – TDS Jun 30 '20 at 11:31
  • Yes it does https://godbolt.org/z/Lock6d . Had to add std:: in front of the cout and add MAX_PATH parameter for it to compile. – Mestkon Jun 30 '20 at 11:47
  • i did already but it did not work lol – TDS Jul 01 '20 at 09:51