5

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.

Community
  • 1
  • 1
rsk82
  • 28,217
  • 50
  • 150
  • 240
  • 2
    I would propose that someone suggests you a good C++ book, rather that presenting a ready-to-copy-and-paste example, so that you wouldn't have any problem to call an API function. – Vlad May 30 '12 at 11:04
  • 1
    What's so difficult in `GetModuleFileName`? You pass `NULL` as first argument, as explained in that answer, the buffer where to store the output as second argument and the size of such buffer as third. Voilà, you have the path of your executable. – Matteo Italia May 30 '12 at 11:05
  • @Vlad: WinAPI has little to do with C++. If you don't like somebody's answer, write your own instead of criticizing. – SigTerm May 30 '12 at 11:32
  • @SigTerm: I don't see any use in providing the fish, instead of teaching the fishing. Teaching the proper C++ is tricky, and cannot be done inside a single answer. If a person sees a function reference but cannot use the function, the problem is not in the WinAPI understanding, but in the mastering the language basics, right? – Vlad May 30 '12 at 14:20
  • @Vlad: "I don't see any use in providing the fish" Then don't provide the fish. Teaching fishing takes longer and will be a waste of time if the person being taught doesn't need to become a fisherman. "Teaching the proper C++ is tricky" WinAPI has very little to do with C++. It is pure C. – SigTerm May 30 '12 at 14:50
  • @Vlad: "but in the mastering the language basics, right?" Wrong. It means person did not read documentation carefully. Also, you don't "master" language basics (as you don't "master" alphabet) - you either know them or don't. Trying to explain everything to everybody in detail is a waste of time, because most beginners will quit programming without ever achieving much and it all has been already documented many times. Replying precisely to the question is more efficient. – SigTerm May 30 '12 at 14:51

2 Answers2

11
#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.

Matteo Italia
  • 123,740
  • 17
  • 206
  • 299
  • 1
    Wow, this is what I needed, although by the example of SigTerm I slowly started to understand whole thing (his example is doing much more and using my need as a step in bigger accomplish), your answer is very clear. – rsk82 May 30 '12 at 11:15
  • 1
    @Vlad: the links to MSDN have already been posted, alongside with a link to a not-so-simple example; I just tried to provide some very simple, linear code that "puts together the bricks", explaining each step, to show how the function is actually used, which can be useful if you just started Win32 programming. Obviously you can't learn it with just examples, but you can't even learn it with just the reference documentation; a good comprehension of the matter will surely require a good Win32 book. – Matteo Italia May 30 '12 at 11:24
  • 1
    @Vlad: "No one learns from ready-to-go examples." That's incorrect. "You better ought to explain the OP how to find" That's not necessary. A programmer that does not know how to find information will either learn how to do it or eventually quit. Either outcome is good. Now if OP asked how to find information... – SigTerm May 30 '12 at 11:31
  • 1
    @SigTerm: "A programmer that does not know how to find information will either learn how to do it or eventually quit" the problem is the code that he "writes" in the meantime... :P – Matteo Italia May 30 '12 at 11:40
  • @MatteoItalia: " the problem is the code he writes" In order to learn how to write good code, you need to make many mistakes first. – SigTerm May 30 '12 at 11:45
4

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

SigTerm
  • 26,089
  • 6
  • 66
  • 115
  • you say the example is first post after Community Additions ? – rsk82 May 30 '12 at 11:06
  • Ok, your example works, others I found on the web were too complicated and gave me errors. Thank you. – rsk82 May 30 '12 at 11:10
  • @rsk82: "you say the example is first post" Open [GetModuleFileName](http://msdn.microsoft.com/en-us/library/windows/desktop/ms683197(v=vs.85).aspx) article in browser, press Ctrl+F (or whatever it is in your browser), type "Examples", press "Enter". – SigTerm May 30 '12 at 11:26