Thread: Gettting path of a program

  1. #1
    Registered User ch4's Avatar
    Join Date
    Jan 2007
    Posts
    154

    Gettting path of a program

    Hi !!!
    How can i get the path of specific program using C ?
    I need to get the path of x program in order to copy files.... in reality i'm trying to make a autocrack.exe file.
    The all i need to known is the above command

    Thanks

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Do you mean this?
    Code:
    dir /s/b progname.exe
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    I need to get the path of x program in order to copy files
    Assuming that you do not know the location of X program, then you would use FindFirstfile and FindNextfile and recursivley call the file finding function to search for X program.

  4. #4
    Registered User mikeman118's Avatar
    Join Date
    Aug 2007
    Posts
    183
    Code:
    char szFileName[MAX_PATH];
    HINSTANCE hInstance = GetModuleHandle(NULL);
    
    GetModuleFileName(hInstance, szFileName, MAX_PATH);
    szFileName being the char array to copy the file name to, you can replace MAX_PATH with whatever no. you want, or you could probably use a string (such as str1.c_str()) in place of a char. But that will return the path to the executable file (C:\test.exe), so you'll have to change that to C:\ or whatever.

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    66
    Code:
    #include <direct.h>
    
    ...
    
    void Foo()
    {
    	...
    
    	char *tempPath = new char[512];		  // Create a cstring to store the path in
    
    	getcwd(tempPath, 511);				// tempPath now stores the path of the directory
    									// of the *.exe file.
    	...
    
    	cout << "This program has been executed from the directory: " << tempPath;
    
    	...
    
    	delete [] tempPath;
    }
    I'm not good at writing snippets of code like that, but I think you get the idea, getcwd() takes two parameters, the first is a cstring that will hold the directory in it and the second is how much space you are giving it.

  6. #6
    Registered User ch4's Avatar
    Join Date
    Jan 2007
    Posts
    154
    Thanks for your answers, were really helpful !
    Matsp i really mean this dir /s/b progname.exe

    I want to get the dir of a program like patches do, and copy/paste crack to the folder of x program but using C.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Well, you can either execute "dir /s/b somename.exe" from inside a C program, using a pipe to read the output, or redirect the output to a file, then read the file. Just remember that not everyone has one big disk, there may be several physical or logicial disks, so you need to get a list of those [there was a post on how to do that in Windows just a few days ago].

    Or, if you really want to do it in your application, use the method described by mikeman118. There will be little difference in performance, since the speed of finding a file is mainly dependant on the time it takes to get the relevant data off the disk, rather than how you get to the final data.

    But really, it's probably much easier to just write a little batch-file to do the same thing, rather than C.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User ch4's Avatar
    Join Date
    Jan 2007
    Posts
    154
    Thank you for the advice.
    I will try both and choose the best.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  2. Getting the path file of your program?
    By RedZone in forum C++ Programming
    Replies: 13
    Last Post: 07-23-2006, 11:25 AM
  3. simple frontend program problem
    By gandalf_bar in forum Linux Programming
    Replies: 16
    Last Post: 04-22-2004, 06:33 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM