controlling cmd.exe via named pipes
- From: toottifrootti@xxxxxxxx
- Date: 26 Feb 2007 06:46:21 -0800
Hi you all.
<I've already posted this same discussion on
it.comp.lang.delphi and borland.public.delphi.language.delphi.general
without
receiving answer as I hoped to, so I re-post it here, hoping this
doesn't offend
any groups' netiquette. If so, I'm sorry.>
I'm trying to use CreateProcess API to launch an "invisible" instance
of cmd.exe and control it via named pipes.
In order to do so, I use TPipeServer and TPipeClient components which
can be found on Russell's Delphi Pages
(http://users.adelphia.net/~rllibby/source.html), Pipes.zip file.
I use them to avoid handling named pipes directly by Windows
Pipes API. You could say this is discussible, anyway let's go on.
This is how I proceed:
1-I create two different named pipes, say 'cmdIN' and 'cmdOUT';
2-as suggested in th following thread ( http://tinyurl.com/2pmls6 ),
I
fill in a STARTUPINFO structure in the appropriate way, and then I
create the cmd.exe process:
=============================
stupinfo.dwFlags := STARTF_USESTDHANDLES or STARTF_USESHOWWINDOW;
stupinfo.hStdInput := <handle to the 'cmdIN' serverside,obtained from
the wrapping TPipeServer>
stupinfo.hStdOutput := <handle to the 'cmdOUT' clientside, obt from
the wrapping TPipeClient>
stupinfo.hStdError := <as above>
stupinfo.wShowWindow := SW_HIDE;
proc_name := 'C:\WINDOWS\system32\cmd.exe';
proc_string := '/K';
hcp := CreateProcess(proc_name, proc_string, nil, nil, TRUE, 0, nil,
nil, stupInfo, info);
if hcp then WaitForSingleObject(info.hProcess, 3000);
=============================
3-from my GUI application, I control the serverside of 'cmdOUT' and
the clientside of 'cmdIN';
I then send (using a TEdit) commands meant for cmd.exe writing on the
second one, catching on the first one the incoming message events,
and
finally copying these messages on a TMemo.
As soon as the process is created, on the memo an "Invalid handle"
message appears, and from then on, cmd.exe does not react to commands
I send to it, eve though it receives them.
And I say "even though it receives them" because I also catch
incoming
message events on the serverside of 'cmdIN': commands regularly reach
the other end of the pipe, but they do not have any effect at all,
even if cmd.exe is still alive, as task manager allows to verify.
I suppose the cause's the following.
Both the TPipeServer that wraps 'cmdIN' serverside and similarly the
TPipeClient that wraps 'cmdOUT' clientside have a thread dedicated to
listening incoming messages. Maybe
duplicating handles to those pipe sides would be better than passing
to cmd.exe the handles owned by those threads.
Anyway -I'm sorry for my long, complicated and a bit dazed thread-,
can anyone help me?
Is my last guess correct, and if so, how to correctly duplicate those
handle?
Or maybe there's some other mess, and if so, what is it?
Any help would be very appreciated, thanks in advance,
Bye
.
- Follow-Ups:
- Re: controlling cmd.exe via named pipes
- From: Rob Kennedy
- Re: controlling cmd.exe via named pipes
- Prev by Date: Re: for loop (c-lang)
- Next by Date: Re: React when the program isn't focused
- Previous by thread: for loop (c-lang)
- Next by thread: Re: controlling cmd.exe via named pipes
- Index(es):