Possible Duplicate:
Extracting the current executable name
I created a program that reads configuration from ini file, the name of that file should be identical to name of executable but of course with its extension. So If I name it myprogram.exe
the config should be myprogram.ini
, and if I change name of the exe after compilation it should look accorting to its new name.
I know that it is possible to get program name from argv[0]
but this works only if it starts from command line, when it is clicked in explorer this array is empty.
As I read through the answers here I think it has to do something with this function: https://stackoverflow.com/a/10572632/393087 - But I can't find any good example of usage of that function, I'm very beginner to c++ and general function definitions (like that presented on microsoft pages) are too hard for me to understand, but when I get a working example it is a snap for me to comprehend.
#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"
Now in out you have a pointer to the "base name" of your executable; keep in mind that it points inside buffer
, so when buffer
goes out of scope out
is not valid anymore.
GetModuleFileName(NULL, .....)
But I can't find any good example of usage of that function
You haven't tried hard enough. "Examples" section in "GetModuleFileName" article on msdn
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With