How can I "GetStdHandle" of a child process?

  • Thread starter Thread starter daat99
  • Start date Start date
D

daat99

Guest
Hi,

I have a windows form application that starts multiple "cmd.exe" processes.

I want to get the standard handle of those child processes.

If I have only a single process I can simply do the following:


Process p1 = new Process();
p1.StartInfo.FileName = "cmd.exe" ;
p1.Start();
AttachConsole(p1.Id); //using kernel32.dll attach the process console to the windows form
IntPtr handle = GetStdHandle(); //using Kernel32.dll get the current process (windows form) std handle - we attached the console a second ago
FreeConsole(); //using Kernel32.dll we free the attached console handle so we could attach another later




That works just find and I can use the handle I got without problem.

When I try to have 2 proccesses I have problems though:


Process p1 = new Process();
p1.StartInfo.FileName = "cmd.exe" ;
p1.Start();
AttachConsole(p1.Id); //attach first console
IntPtr handle1 = GetStdHandle(); //get handle
FreeConsole(); //free first console
Process p2 = new Process();
p2.StartInfo.FileName = "cmd.exe" ;
p2.Start();
AttachConsole(p2.Id); //attach 2nd console
IntPtr handle2 = GetStdHandle(); //get 2nd handle
FreeConsole(); //free 2nd console




The problem is that no matter what I do I always get both handle1 and handle2 to be "7" (PS in my real application I checked the return code of the AttachConsole and FreeConsole and they both returned success, I also have 4 processes and they all register the same magic number "7" as standard handle).

Does anyone have any idea how I can get the standard handle of at least 2 child processes?

I need this handle in order to use the SetConsoleScreenBufferSize .

For easier reference here are some links: FreeConsole , AttachConsole , GetStdHandle .

Thanks in advance for any assistance you can provide,

daat99

I always try to help ;) sometimes I don't know how :(

Continue reading...
 
Back
Top