tags:

views:

105

answers:

1

i would like to execute a batch file from a program (exe file). i usually create the batch file while program (exe file) execution. and will the execl("START","",NULL);

the function is calling the batch file, but unfortunately new command window is showing up as the execl function is process START and parses as “cmd.exe /k " .

can you please share the easiest way to call the function which executes the batch file in background, if possible please share the code snippet.

+1  A: 

Don't use start but cmd /c.

ETA: As Chris Jester-Young notes in a comment, this will then look like

execlp("cmd", "/c", batchfile, static_cast<char*>(0))

in your code.

ETA 2: It appears that you don't even need cmd there. Just the batch file as command should suffice. Since start worked and start is a built-in command of cmd.exe there has to be an instance of cmd running for your original invocation to work. So just execute the batch file as if it were the program, this should suffice.

Joey
Which, here, translates to `execlp("cmd", "/c", batchfile, static_cast<char*>(0))`.
Chris Jester-Young
@Chris, thanks. My experience with C++ and C is quite limited. The underlying problem was pretty obvious, though.
Joey
execlp() API says, first argument should be path of the file to execute , does this interpret cmd to cmd.exe ?
Venkat
@Venkat: Try it out. I honestly have no idea. You can put cmd.exe in there too if you like.
Joey
@Johannes: No worries. If you're curious, read http://www.opengroup.org/onlinepubs/9699919799/functions/exec.html for more info on the various `exec*` functions. The `static_cast<char*>(0)` is the C++ way of expressing `(char *) 0` which is listed in their documentation.
Chris Jester-Young
@Venkat: You may have to use `cmd.exe`, yes. I haven't done enough Windows programming to tell you one way or the other. (Cygwin will automatically do `.exe` resolution for you, but can't speak about other Windows programming environments.)
Chris Jester-Young
@Venkat: On second thought, you only need the batch file directly; see the second edit.
Joey
@Johannes: Huh, how interesting (that `exec*` can execute shell builtins, at least for the OP's environment). I certainly do not expect the same to work in Unix; there, using `sh -c ...` is required. :-P
Chris Jester-Young
@Chris: That was inferred from the OP's original usage. But I faintly remember some docs by MS that said so. But batch files can be run like normal programs in any case, afaik; but especially so if there is a shell already executing that ;-)
Joey
@johannes and @chris , the only function call working is system() and not the execl and execlp are not at all executing... and also i would like to execute the batch file in background.... user should not be prompted with new command screen nor any popup should appear.
Venkat