Discussion:
How To Execute OS Commands Using CreateProcess
(too old to reply)
Roshan
2008-06-17 23:04:41 UTC
Permalink
To Any Whom Can Answer,

Hello. I was successfully executing the command "mode com6 baud=19200
parity=n data=8'
using the system() function. What I wanted though was for the console
window to not show up
every time ran the function. So after reading posts on the net, I
tried using CreateProcess() which gave the option of hiding the
window. The Problem now is CreateProcess() is not running the command
at all. Not sure why. I have placed a snippet of my code. Please let
me know why CreateProcess can't execute the "mode" command in DOS.

...
STARTUPINFO si;
PROCESS_INFORMATION pi;
CString tempString = "mode com6 baud=19200 parity=n data=8";

char* cmdLine = tempString.GetBuffer(tempString.GetLength());
CreateProcess(NULL, cmdLine, NULL, NULL, false, 0, NULL, NULL, &si,
&pi);
tempString.ReleaseBuffer();
...
Tim Roberts
2008-06-18 06:46:56 UTC
Permalink
Post by Roshan
Hello. I was successfully executing the command "mode com6 baud=19200
parity=n data=8'
using the system() function. What I wanted though was for the console
window to not show up
every time ran the function. So after reading posts on the net, I
tried using CreateProcess() which gave the option of hiding the
window. The Problem now is CreateProcess() is not running the command
at all. Not sure why. I have placed a snippet of my code. Please let
me know why CreateProcess can't execute the "mode" command in DOS.
...
STARTUPINFO si;
PROCESS_INFORMATION pi;
CString tempString = "mode com6 baud=19200 parity=n data=8";
Make that:
CString tempString = "mode.com com6 baud=19200 parity=n data=8";

STARTUPINFO is an input parameter. You should clear it to zero first:
ZeroMemory( &si, sizeof(si) );
--
Tim Roberts, ***@probo.com
Providenza & Boekelheide, Inc.
Roshan
2008-06-18 15:50:33 UTC
Permalink
Post by Roshan
Hello. I was successfully executing the command "mode com6 baud=19200
parity=n data=8'
using the system() function. What I wanted though was for the console
window to not show up
every time ran the function. So after reading posts on the net, I
tried using CreateProcess() which gave the option of hiding the
window. The Problem now is CreateProcess() is not running the command
at all. Not sure why. I have placed a snippet of my code. Please let
me know why CreateProcess can't execute the "mode" command in DOS.
...
STARTUPINFO si;
PROCESS_INFORMATION pi;
CString tempString = "mode com6 baud=19200 parity=n data=8";
    CString tempString = "mode.com com6 baud=19200 parity=n data=8";
    ZeroMemory( &si, sizeof(si) );
--
Providenza & Boekelheide, Inc.- Hide quoted text -
- Show quoted text -
Tim,

Thanks so much for the help! Okay, I've done that now and it seems to
be working.

There's is this one last line I need to make work though.

Again, I'm trying to use CreateProcess to execute the following line:
"AVROSP.exe -dATtiny461 -lfc -Lfc -pf -vf -if\\dba1\D\serialdat\AVRPRG
\939-3IM22-000.hex -e -cCOM6 -z >>
\\dba1\D\serialdat\AVRPRG\BURNLOGS\IM2-M-2-6-0-00.100198.txt"

AVROSP is an executable. -d, -l, -L, -p, -v, -if, -e, -c, and -z are
all switches.
Here is a summary of some of the switches for your understanding:
-d requires a device name, in this case, ATtiny461.
-if requires a hex source code file.
-z indicates the filename that the output will be piped to.

When I run the same code the I used for the "mode" command, it doesn't
work.
One way that I'm making sure if it works is the output file, IM2-
M-2-6-0-00.100198.txt, should get created, but
it doesn't.

What should I do to make the AVROSP.exe command line work?

Thanks,

Roshan
Alex Blekhman
2008-06-18 16:27:34 UTC
Permalink
"AVROSP.exe -dATtiny461 -lfc -Lfc -pf -vf -if\\dba1\D\serialdat\AVRPRG\939-3IM22-000.hex
-e -cCOM6 -z >>
\\dba1\D\serialdat\AVRPRG\BURNLOGS\IM2-M-2-6-0-00.100198.txt"
When I run the same code the I used for the "mode" command, it
doesn't work. One way that I'm making sure if it works is the
output file, IM2-M-2-6-0-00.100198.txt, should get created, but
it doesn't.
What should I do to make the AVROSP.exe command line work?
It doesn't work because you use output redirection (>>) in your
command line. Output redirection is implemented by command-line
processor. You can see the full path of command-line processor
executable by examining %COMSPEC% environment variable:

echo %COMSPEC%

Under Windows NT/2K/XP/Vista it is "cmd.exe".

When you start new process from "AVROSP.exe" executable there is
nothing in the middle to interpret >> sign and redirect program's
output to a file.

You have two possible solutionL:

1. Start "AVROSP.exe" process with redirected input and output and
then write the output into the file by yourself.

"Creating a Child Process with Redirected Input and Output"
http://msdn.microsoft.com/en-us/library/ms682499(VS.85).aspx

2. Create temporary batch file and write there your command. Then
run this batch file with `CreateProcess' function.

HTH
Alex
Alf P. Steinbach
2008-06-18 17:08:20 UTC
Permalink
Post by Alex Blekhman
"AVROSP.exe -dATtiny461 -lfc -Lfc -pf -vf -if\\dba1\D\serialdat\AVRPRG\939-3IM22-000.hex
-e -cCOM6 -z >>
\\dba1\D\serialdat\AVRPRG\BURNLOGS\IM2-M-2-6-0-00.100198.txt"
When I run the same code the I used for the "mode" command, it
doesn't work. One way that I'm making sure if it works is the
output file, IM2-M-2-6-0-00.100198.txt, should get created, but
it doesn't.
What should I do to make the AVROSP.exe command line work?
It doesn't work because you use output redirection (>>) in your
command line. Output redirection is implemented by command-line
processor. You can see the full path of command-line processor
echo %COMSPEC%
Under Windows NT/2K/XP/Vista it is "cmd.exe".
When you start new process from "AVROSP.exe" executable there is
nothing in the middle to interpret >> sign and redirect program's
output to a file.
1. Start "AVROSP.exe" process with redirected input and output and
then write the output into the file by yourself.
"Creating a Child Process with Redirected Input and Output"
http://msdn.microsoft.com/en-us/library/ms682499(VS.85).aspx
2. Create temporary batch file and write there your command. Then
run this batch file with `CreateProcess' function.
Or perhaps

3. Use CreateProcess or ShellExecute to run [cmd.exe] with the relevant command
as argument. As I recall that's [cmd.exe] option "/c".

Disclaimer: haven't checked right now if that works.


Cheers,

- Alf
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Alex Blekhman
2008-06-18 17:44:28 UTC
Permalink
Post by Alf P. Steinbach
3. Use CreateProcess or ShellExecute to run [cmd.exe] with the
relevant command as argument. As I recall that's [cmd.exe]
option "/c".
You're correct. Also, if you want to catch Unicode output, then it
will be:

cmd.exe /U /C command_name >> file_path


Alex
Roshan
2008-06-18 18:17:43 UTC
Permalink
Post by Alex Blekhman
Post by Alf P. Steinbach
3. Use CreateProcess or ShellExecute to run [cmd.exe] with the
relevant command as argument. As I recall that's [cmd.exe]
option "/c".
You're correct. Also, if you want to catch Unicode output, then it
cmd.exe /U /C command_name >> file_path
Alex
Alex,

Thanks for the response. the latter solution works beautifully and is
very simple!!!!!
Adding the "command interpreter" string manually emulates what the
system() function does by default!

Here it is for both commands:
1) "cmd.exe /c mode com6 baud=19200 parity=n data=8"
2) "cmd.exe /c AVROSP -dATtiny461 -lfc -Lfc -pf -vf -if\\dba1\D
\serialdat\AVRPRG\939-3IM22-000.hex -e -cCOM6 -z >> \\dba1\D\serialdat
\AVRPRG\BURNLOGS\IM2-M-2-6-0-00.100198.txt"

Again I appreciate your input.

- Roshan

Loading...