Process.Start and cmd.exe closing prematurely

T

TheRain

Hey there,

I am writing some test code, trying to hook standard IO from cmd.exe.
What I really want to do is to open cmd.exe, keep it open, and have it
process the DOS commands as I sent them from my program. The problem
I'm having is that my program seems to be opening cmd.exe, but it
dissapears rather quickly. The code that I've commented out here is
code I want to use eventually in order to have the cmd window not
visible. Anyone have some idea as to why the window keeps
dissapearing?? thanks a bunch for any help..

<code>

ProcessStartInfo* startInfo = new ProcessStartInfo("cmd");
//startInfo->CreateNoWindow;
//startInfo->WindowStyle=ProcessWindowStyle::Hidden;
startInfo->UseShellExecute=false;
startInfo->RedirectStandardInput=true;
startInfo->RedirectStandardOutput=true;
startInfo->RedirectStandardError=true;
commandProc->Start(startInfo);

</code>
 
G

Guest

Vadym Stetsyak said:
Hello, TheRain!

T> I am writing some test code, trying to hook standard IO from cmd.exe.
T> What I really want to do is to open cmd.exe, keep it open, and have it
T> process the DOS commands as I sent them from my program. The problem
T> I'm having is that my program seems to be opening cmd.exe, but it
T> dissapears rather quickly. The code that I've commented out here is
T> code I want to use eventually in order to have the cmd window not
T> visible. Anyone have some idea as to why the window keeps
T> dissapearing?? thanks a bunch for any help..

Did you try specifying startInfo->UseShellExecute=true;

Also if you won't specify cmd anything to do ( no arguments ) nothig will be done, try launching cmd from "Run..." ( that's shellexecuted ) and then type in the console window "cmd" command. The latter is what you observe...

If one executes cmd from the process, is it then possible to send
further commands to that window?
 
V

Vadym Stetsyak

Hello, Robert!

if one executes cmd from the process, is it then possible to send
further commands to that window?

If Input stream is overriden, then it is possible to send data to that process.
Process will get the data via Console.Read(...);
--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
 
G

Guest

Hello Vadym, thanks for your quick reply.
If Input stream is overriden, then it is possible to send data to that process.
Process will get the data via Console.Read(...);

I have tried what you suggested and it seems to work. There is
just one follow up question to this.. If I execute a command and that
command sometimes wants to recieve a password and sometimes
not. Would the below code work?

I am aware of that it is not the best solution since if a password is
not requested it will still be sent (and as a command instead). But
for now that is acceptable.

Code:
private static void start()
{
Process p = new Process();
StreamWriter sw;
StreamReader sr;
StreamReader err;

ProcessStartInfo psI = new ProcessStartInfo("cmd");
psI.UseShellExecute = false;
psI.RedirectStandardInput = true;
psI.RedirectStandardOutput = true;
psI.RedirectStandardError = true;
psI.CreateNoWindow = true;
p.StartInfo = psI;
p.Start();
sw = p.StandardInput;
sr = p.StandardOutput;
err = p.StandardError;

sw.AutoFlush = true;

sw.WriteLine("myCommand"); // Run Command Here
sw.WriteLine("myPassword"); // Above code may request a pwd, send it.
sw.WriteLine("exit"); // Close cmd window

sw.Close();

//System.Console.Write ( err.ReadToEnd() );
//System.Console.Write ( sr.ReadToEnd() );

sr.Close();
err.Close();

}



Kind Regards,
Robert
 
V

Vadym Stetsyak

Hello, Robert!

R> I am aware of that it is not the best solution since if a password is
R> not requested it will still be sent (and as a command instead). But
R> for now that is acceptable.

Since you've overriden input and output streams you can ask the program if it needs password. ( I assume that you have access to the source code of program you're executing ).

To ask it about this, you can give something on imput and analuze the output.
--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
 
G

Guest

Hi Vadym,
R> I am aware of that it is not the best solution since if a password is
R> not requested it will still be sent (and as a command instead). But
R> for now that is acceptable.

Since you've overriden input and output streams you can ask the program if it needs password. ( I assume that you have access to the source code of program you're executing ).

To ask it about this, you can give something on imput and analuze the output.

Thank you very much for your help, highly appreciated, it seems to
work like a charm. All left now is to move the code to my windows
service. Thanks again!

Kind Regards,
Robert
 
T

TheRain

thanks to both of you... I'm beginning to notice that most people who
use .NET do not program in C++. My MSDN discs hardly have any C++
examples... but it looks like the online version has them. Anyhow, I
figured out through trial and error that if I first create an instance
of a Process with it's location stored in a pointer, all of the
ProcessInfo commands will work and the cmd.exe process will not end
untill I instruct it to.

Class Members:
Process *commandProc;
StreamWriter *swr;
StreamReader *sr;
StreamReader *err;

Initialization Code:
commandProc=new Process;
commandProc->StartInfo->FileName="cmd.exe";
commandProc->StartInfo->CreateNoWindow=true;
commandProc->StartInfo->WindowStyle=ProcessWindowStyle::Hidden;
commandProc->StartInfo->UseShellExecute=false;
commandProc->StartInfo->RedirectStandardInput=true;
commandProc->StartInfo->RedirectStandardOutput=true;
commandProc->StartInfo->RedirectStandardError=true;
commandProc->Start();
swr=commandProc->StandardInput;
sr=commandProc->StandardOutput;
err=commandProc->StandardError;

Then the WriteLine and ReadLine (and other functions) can be accessed
through the StreamReader and StreamWriter class pointers. Obviousely
one would do well to put in some error handling code as well for these
initializtions.
 
V

Vadym Stetsyak

Hello, TheRain!

T> I'm beginning to notice that most people who
T> use .NET do not program in C++.

If you have questions related to managed c++ you can ask them on
microsoft.public.dotnet.languages.vc

--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top