How to use CreateProcess to start a .cmd file?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: How to use CreateProcess to start a .cmd file?

  1. #1
    Join Date
    Feb 2004
    Posts
    9

    How to use CreateProcess to start a .cmd file?

    Hello,

    I am working on a simple script that calls a .cmd file on a remote share. Right now it looks like this:

    CreateProcess(
    NULL,
    commandLine,
    NULL,
    NULL,
    FALSE,
    0,
    NULL,
    NULL,
    &si,
    &pi)

    the value of commandLine is "\\server\share\script"

    Out on server\share the script is actually a .cmd file. So if I call it directly from the command line by typing '\\server\share\script <enter>' the script will execute. However when using CreateProcess it returns error code 2 (cannot find file specified).

    As I understand, this is because CreateProcess assumes .exe extension if none is given. Is there a way for me to make it assume .cmd? Or is there a wildcard way to do it?

    The problem is we have both .exe and .cmd files out there on the share so rather than force the user to know which it is I would like to be able to just give the name of the file to execute and my program will figure it out from there.

    Thanks,
    Ian

  2. #2
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128
    You can try ShellExecute().

    Code:
    ShellExecute(NULL, "open", "\\\\server\\share\\script.cmd", NULL, NULL, SW_SHOWNORMAL);

  3. #3
    Join Date
    Feb 2004
    Posts
    9
    Thanks Kheun but I don't think that will solve the problem.

    Perhaps I should clarify:

    We currently have a batch (cmd) script called "do.cmd" that (among other things) simply calls scripts and executables on a remote server.

    So the user can just type in:
    do SomeScript

    and it will execute the script from the server.

    Now, most of those scripts out there are .cmd files but there are the occassional .exe or .msi or .bat files too. The beauty of the current do.cmd is it doesn't care what the extension is on the remote file.

    Now for grins (and C++ practice) I am trying to convert do.cmd over to an exe written in C++ (do.exe). I am using CreateProcess to execute the commands.

    However, here is the problem:
    User cannot just type 'do SomeScript' anymore.

    The reason as I understand is that CreateProcess assumes and appends .exe if it cannot find an extension. However, I need it to be indifferent to the extension.

    So if user types 'do SomeScript' i need it to execute regardless of whether that is a .cmd, .exe, .msi, etc.


    Hmm ok
    Now that I've looked up ShellExecute it looks more promising than create process. I'll see if it works...

  4. #4
    Join Date
    Feb 2004
    Posts
    9
    Ok I tried ShellExecute and it seems to work better... but:

    I would like the .exes, .cmds, etc to run from within the command window they were called (not open a new command window).

    Is this possible?

    Thanks,
    Ian

  5. #5
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128
    Sorry. I don't how to do that in ShellExecute(). A reason for using ShellExecute is to launch the corresponding application that is associated with the extension of the specified file.

    After some experimenting with CreateProcess() again, I am able to get it working.

    Code:
        STARTUPINFO si;
        PROCESS_INFORMATION pi;
    
        ZeroMemory( &si, sizeof(si) );
        si.cb = sizeof(si);
        ZeroMemory( &pi, sizeof(pi) );
    
        if( !CreateProcess( NULL,  
            "cmd.exe /c \\\\server\\share\\script.cmd",
            NULL,
            NULL,
            FALSE,
            0,
            NULL,
            NULL,
            &si,
            &pi )
        ) 
        {
            printf( "CreateProcess failed." );
        }
    Last edited by Kheun; February 24th, 2004 at 09:34 PM.

  6. #6
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128
    Oops, I forgotten to mention that I did to specify "cmd.exe /c" to launch the command prompt before the batch file can be executed.

  7. #7
    Join Date
    Feb 2004
    Posts
    9
    Thanks Kheun that works!

    -Ian

  8. #8
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    The reason is simply (as shown by Kheun) that you need to specify the application you want to use with 'CreateProcess()' explicitly. 'ShellExecute()'/'ShellExecuteEx()' does the lookup for the correct application itself by looking at the file extension passed to it...
    Ciao, Andreas

    "Software is like sex, it's better when it's free." - Linus Torvalds


    Article(s): Allocators (STL) Function Objects (STL)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center