# Thread: getting path of exe...

1. ## getting path of exe...

How do I get the path of the program (the exe) and store it in a variable?

2. Code:
#include <iostream>
#include <string>

using namespace std;

int main ( int argc, char *argv[] )
{
string name;

if ( argc > 0 )
name = argv[0];

cout<< name <<endl;
}
I bet I can guess what your next question will be though.

3. yah that won't work. Sorry I left out some info here. I am making a dll for Game Maker 6.0. I am making something were the program will run on startup but I need to know the programs path before I can create a key in the "run" key.

So can you tell me how it can be done without using int main. I had it just a couple of mins ago but I deleted that line of code and saved it. :'(

I know it is somthign like get_module_path or somthing

Here is the code I have so far...

Code:
HKEY hKey;
DWORD dwDisposition;
char szData[MAX_PATH]="C:\\filename.exe";

RegCreateKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", 0, NULL, 0, KEY_ALL_ACCESS, NULL, &hKey, &dwDisposition);
RegSetValueEx(hKey, "mykey", 0, REG_SZ, (LPBYTE)szData, sizeof(szData));
RegCloseKey(hKey);

4. Yes! I should have thoguth to look at msdn because I was there for like an hour today.

Thank you soooooooo much!

5. For anyone in the future searching the boards that CAN use the int main() method, a quick explanation of Prelude's solution:

argc is the number of arguments, argv is an array of pointers, each pointing to one of the command line arguments. The first would be the path of the executable. I think... If Prelude could confirm this explanation that would be awesome...

6. The first would be the path of the executable
IIRC it argv[0] can be:
The file name "bob.exe", the file name and path "c:\foo\bar.exe" or blank ""

7. IIRC argv[0] is the command used to run the program. so if i run ./foo from a dir containing foo then argv[0] will be "./foo". If i run /foo/bar/foo then it will be "/foo/bar/foo" On windows i think double clicking an icon will launch it using the complete path, but i really have no idea.

8. Originally Posted by Perspective
On windows i think double clicking an icon will launch it using the complete path, but i really have no idea.
Yes, because the File's Explorer calls (maybe?!?) ShellExecute with the fullpath.