473,320 Members | 2,073 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Process.Start and cmd.exe closing prematurely

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>

Apr 23 '06 #1
7 2832


"Vadym Stetsyak" wrote:
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?
Apr 24 '06 #2
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
Apr 24 '06 #3

Hello Vadym, thanks for your quick reply.
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(...);


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.

Expand|Select|Wrap|Line Numbers
  1. private static void start()
  2. {
  3. Process p = new Process();
  4. StreamWriter sw;
  5. StreamReader sr;
  6. StreamReader err;
  7.  
  8. ProcessStartInfo psI = new ProcessStartInfo("cmd");
  9. psI.UseShellExecute = false;
  10. psI.RedirectStandardInput = true;
  11. psI.RedirectStandardOutput = true;
  12. psI.RedirectStandardError = true;
  13. psI.CreateNoWindow = true;
  14. p.StartInfo = psI;
  15. p.Start();
  16. sw = p.StandardInput;
  17. sr = p.StandardOutput;
  18. err = p.StandardError;
  19.  
  20. sw.AutoFlush = true;
  21.  
  22. sw.WriteLine("myCommand"); // Run Command Here
  23. sw.WriteLine("myPassword"); // Above code may request a pwd, send it.
  24. sw.WriteLine("exit"); // Close cmd window
  25.  
  26. sw.Close();
  27.  
  28. //System.Console.Write ( err.ReadToEnd() );
  29. //System.Console.Write ( sr.ReadToEnd() );
  30.  
  31. sr.Close();
  32. err.Close();
  33.  
  34. }
  35.  


Kind Regards,
Robert

Apr 25 '06 #4
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
Apr 25 '06 #5


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
Apr 25 '06 #6
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.

Apr 26 '06 #7
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
Apr 27 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: serge calderara | last post by:
Dear all, I have an application which is suppose to start another executable process. As soon as that process is running, I need to retrive its handle. The problem of the particular process I am...
10
by: Tom Szabo | last post by:
Is there an event when that triggers when the window is closing.... I am talking about when the user clicks on the cross on the right top corner of the window!!!
7
by: Samantha | last post by:
Hello , I am developping a program for Win98 plateform, and I am stucking with a problem, hope you can help me. I have a program running 2 process : - One process running the Xcopy.exe -...
4
by: Strah | last post by:
Is there a way to catch the event of terminating the application from the the processes tab in task manager? I have created windows app, and if a user termintates the app in the task manager,...
2
by: andreas | last post by:
Hi, In my code I have the following System.diagnostics.process.start("wordpad",sFileN) And this is follow bij others codelines It seems that these other lines are executed before the process is...
19
by: rbrowning1958 | last post by:
Hello, I am confused by dispose etc. and hope someone can set me right. 1. The Dispose(Bool) the IDE generates for a form has nothing to do with IDisposable, right? 2. So when is this called?...
3
by: sewid | last post by:
Hi! I have a very simple windows form (Visual Basic, .NET-Framework 2.0). This is my whole code: Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As...
0
by: John Halet | last post by:
I have a application that crunches a bunch of data, creating log files, excel files etc... I use Process.Start to open any number of these file for viewing. My goal is to have the application...
5
by: Markgoldin | last post by:
I am searching for a solution of sending messages from not .Net process into a .Net process written in C#. I have gone already thru sockets solution and have porblems with it. I am not a C# coder...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.