February 24th, 2004, 06:55 PM
How to use CreateProcess to start a .cmd file?
I am working on a simple script that calls a .cmd file on a remote share. Right now it looks like this:
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.
February 24th, 2004, 07:19 PM
You can try ShellExecute().
ShellExecute(NULL, "open", "\\\\server\\share\\script.cmd", NULL, NULL, SW_SHOWNORMAL);
February 24th, 2004, 07:38 PM
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:
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.
Now that I've looked up ShellExecute it looks more promising than create process. I'll see if it works...
February 24th, 2004, 07:52 PM
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?
February 24th, 2004, 09:31 PM
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.
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );
if( !CreateProcess( NULL,
"cmd.exe /c \\\\server\\share\\script.cmd",
printf( "CreateProcess failed." );
Last edited by Kheun; February 24th, 2004 at 09:34 PM.
February 24th, 2004, 09:40 PM
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.
February 25th, 2004, 12:51 PM
Thanks Kheun that works!
February 25th, 2004, 03:37 PM
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...
Click Here to Expand Forum to Full Width
This is a Codeguru.com survey!