0

I want to get the current executable application name of my windows console application. According to this answer, the solution should be to use GetModuleFileNameA function.

The problem is the function doesn't fill the name of the executable but fill the address :

#include <iostream>
#include <tchar.h>
#include <windows.h>

int main()
{
    TCHAR szFileName[MAX_PATH];
    GetModuleFileName(NULL, szFileName, MAX_PATH);
    std::cout << "My executable name is : " << szFileName << std::endl;
    system("pause");
}

Output : My executable name is : 0077F9DC

Some one could fix my code ?

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • What is `TCHAR`? Is it `wchar_t` or `char`? In other words, are you compiling for Unicode or not? You also need to include error checking when you call winapi functions. You ignore the return value of `GetModuleFileName`. Don't do that. – David Heffernan Apr 15 '20 at 08:18
  • 1
    your problem in `std::cout << szFileName`. `GetModuleFileName` work correct. but your display not correct. instead display content of `szFileName` you display address of `szFileName` – RbMm Apr 15 '20 at 08:19
  • 1
    Change `TCHAR` to `char` and `GetModuleFileName` to `GetModuleFileNameA`. The code is mixing Unicode and non-Unicode in the output line and therefore just outputting the address of the buffer. – Richard Critten Apr 15 '20 at 08:26
  • 1
    when TCHAR is defined as WCHAR you should use std::wcout (instead of std::cout) for output. – engf-010 Apr 15 '20 at 08:38
  • 1
    @RichardCritten No. Change `TCHAR` to `wchar_t` and use `std::wcout`. – David Heffernan Apr 15 '20 at 08:45
  • @ric: The application location is outside the control of the application. Falling back to ANSI character encoding *will* fail at some point, when a user decides to place it into a directory, whose name cannot be represented in any one given ANSI codepage. – IInspectable Apr 15 '20 at 13:09

1 Answers1

0

This is the complete code of the extraction current executable name :

#include <iostream>
#include <string>
#include <windows.h>

int main()
{
    wchar_t szFileName[MAX_PATH];
    // don't forget to check return values when calling winapi functions
    GetModuleFileNameW(NULL, szFileName, MAX_PATH); 
    std::wstring filePath(szFileName);
    std::size_t indexLastCharSlash = filePath.find_last_of(L"/\\");
    std::wstring fileName = filePath.substr(indexLastCharSlash + 1);
    std::wcout << fileName << std::endl;
    system("pause");
}
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
Anonymous
  • 468
  • 5
  • 26
  • Please don't encourage use of ANSI text. Please don't encourage neglecting error checking on winapi calls. – David Heffernan Apr 15 '20 at 08:46
  • I am not understand very well where is the problem. If you think is necessary, please edit my answer but it responds to the question. Is my code incompatible with UTF-8 ? I think we keep in mind that it print name of file so characters are limited. – Anonymous Apr 15 '20 at 09:01
  • The problem is that your code can't handle characters outside the prevailing ANSI codepage. You should use Unicode, which with winapi means `wchar_t` and then using `std::wcout` to output the value. – David Heffernan Apr 15 '20 at 09:06
  • OK I understand, thank you for the feed back but some function like `find_last_of` and `substr` are not compatible with `wstring` so I don't know how to adapt it. – Anonymous Apr 15 '20 at 09:08
  • They are compatible with `wstring` – David Heffernan Apr 15 '20 at 09:13
  • I edited my code. Thanks for your feed back. I will be careful with winapi functions which ends with `A` in the future. – Anonymous Apr 15 '20 at 09:26
  • I fixed a couple of minor issues – David Heffernan Apr 15 '20 at 13:17
  • A helpful answer needs to at least identify the issue, and explain how the answer addresses it. *"Try this"* type of answers aren't useful. – IInspectable Apr 15 '20 at 13:29