How can a program get the executable name of itself?

Possible duplicate:
Retrieve the current executable file name

I created a program that reads the configuration from an ini file, the name of this file should be identical to the name of the executable file, but, of course, with its extension. Therefore, if I name it myprogram.exe, there should be a config myprogram.ini, and if I changed the name exe after compilation, it should look like it matches its new name.

I know that you can get the program name from argv[0], but this only works if it starts from the command line, when it is clicked in Explorer, this array is empty.

As I read the answers here, I think he needs to do something with this function: https://stackoverflow.com/a/166269/2126161- But I can’t find a good usage example from this function, I really start with C + +, and the definitions of common functions (for example, presented on Microsoft pages) are too difficult to understand, but when I get a working example, it is accessible to me.

+5
source share
2 answers
#include <windows.h>
#include <Shlwapi.h>
// remember to link against shlwapi.lib
// in VC++ this can be done with
#pragma comment(lib, "Shlwapi.lib")

// ...

TCHAR buffer[MAX_PATH]={0};
TCHAR * out;
DWORD bufSize=sizeof(buffer)/sizeof(*buffer);
// Get the fully-qualified path of the executable
if(GetModuleFileName(NULL, buffer, bufSize)==bufSize)
{
    // the buffer is too small, handle the error somehow
}
// now buffer = "c:\whatever\yourexecutable.exe"

// Go to the beginning of the file name
out = PathFindFileName(buffer);
// now out = "yourexecutable.exe"

// Set the dot before the extension to 0 (terminate the string there)
*(PathFindExtension(out)) = 0;
// now out = "yourexecutable"

You now have a pointer to the "base name" of your executable; keep in mind that it points inside buffer, so when it buffergoes out of scope outit is no longer valid.

+5
source

GetModuleFileName(NULL, .....)

But I can not find a good example of using this function

. "" "GetModuleFileName" msdn

+4

All Articles