Thread: getting path of exe...

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    731

    getting path of exe...

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

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    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.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    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. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268

  5. #5
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    Yes! I should have thoguth to look at msdn because I was there for like an hour today.

    Thank you soooooooo much!

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    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...

  7. #7
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    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 ""

  8. #8
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    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.

  9. #9
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    Quote 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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Shortest Path Maze Solver (Breadth Search Help)
    By Raskalnikov in forum C Programming
    Replies: 5
    Last Post: 04-07-2009, 07:41 PM
  2. clipping path
    By stanlvw in forum Windows Programming
    Replies: 0
    Last Post: 07-23-2008, 11:47 PM
  3. Path Finding Using Adjacency List.
    By Geolingo in forum C++ Programming
    Replies: 7
    Last Post: 05-16-2005, 02:34 PM
  4. Given tha path, find an exe(given the size) into it
    By BianConiglio in forum C Programming
    Replies: 1
    Last Post: 04-01-2004, 06:50 PM
  5. linked list recursive function spaghetti
    By ... in forum C++ Programming
    Replies: 4
    Last Post: 09-02-2003, 02:53 PM