Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get program path [duplicate]

Tags:

c++

gtk

Possible Duplicate:
how to find the location of the executable in C

I'm writting an multi-platform app in C++ using GTK+ and I have a problem. I must get program path. E.g., when program is in /home/user/program (or C:\Users\user\program.exe), i have /home/user/ (or C:\Users\user\).

Can and how I can do this?

like image 896
m4tx Avatar asked Dec 23 '10 09:12

m4tx


2 Answers

For Win32/MFC c++ programs:

char myPath[_MAX_PATH+1];
GetModuleFileName(NULL,myPath,_MAX_PATH);

Also observe the remarks at http://msdn.microsoft.com/en-us/library/windows/desktop/ms683156%28v=vs.85%29.aspx,

In essence: WinMain does not include the program name in lpCmdLine, main(), wmain() and _tmain() should have it at argv[0], but:

Note: The name of the executable in the command line that the operating system provides to a process is not necessarily identical to that in the command line that the calling process gives to the CreateProcess function. The operating system may prepend a fully qualified path to an executable name that is provided without a fully qualified path.

like image 58
Oliver Zendel Avatar answered Sep 23 '22 17:09

Oliver Zendel


argv[0] contains the program name with path. Am I missing something here?

like image 25
Jaywalker Avatar answered Sep 24 '22 17:09

Jaywalker