Thread: Executing programs in your PATH

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    50

    Executing programs in your PATH

    Hello,

    I'm using C++ Builder and trying to run a program with CreateProcess. For example:

    Code:
    if ( !CreateProcess("cmd", NULL, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi) )
    		MessageBox(NULL, "Error running cmd.exe", "Error", NULL);
    This compiles fine, however it doesn't run cmd.exe. (having "cmd.exe" as the argument doesn't help either). The MessageBox with the error is executed.

    If I use system("cmd"); it works just fine. This is the same for other programs that I have in my PATH, for example my gcc compiler. Is there any way I can run these with CreateProcess without specifying the full path? Because CreateProcess gives you a lot more control, like a process handle and ID that you can use with WaitForSingleObject() etc...

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Read the documentation, http://msdn.microsoft.com/en-us/library/ms682425.aspx

    It specifically mentions cmd.exe

    Quote Originally Posted by the manual
    lpCommandLine [in, out, optional]

    The command line to be executed. The maximum length of this string is 32,768 characters, including the Unicode terminating null character. If lpApplicationName is NULL, the module name portion of lpCommandLine is limited to MAX_PATH characters.

    ... *SNIP* ...

    If the file name does not contain a directory path, the system searches for the executable file in the following sequence:

    1. The directory from which the application loaded.
    2. The current directory for the parent process.
    3. The 32-bit Windows system directory. Use the GetSystemDirectory function to get the path of this directory.
    4. The 16-bit Windows system directory. There is no function that obtains the path of this directory, but it is searched. The name of this directory is System.
    5. The Windows directory. Use the GetWindowsDirectory function to get the path of this directory.
    6. The directories that are listed in the PATH environment variable. Note that this function does not search the per-application path specified by the App Paths registry key. To include this per-application path in the search sequence, use the ShellExecute function.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    50
    That works! Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  2. Path of file relative to program
    By ulillillia in forum C Programming
    Replies: 13
    Last Post: 04-15-2007, 05:30 AM
  3. I never Finish my programs :(
    By epidemic in forum C++ Programming
    Replies: 11
    Last Post: 04-02-2007, 12:35 PM
  4. Altering The Program's Executable?
    By Aidman in forum C++ Programming
    Replies: 7
    Last Post: 12-31-2002, 05:11 AM
  5. executing c++ programs on the web
    By gulti01 in forum C++ Programming
    Replies: 4
    Last Post: 08-12-2002, 03:12 AM