Pipes on Wince (I have them), and device auto-loading.
- From: Pedro Alves <alves.ped@xxxxxxxxx>
- Date: Mon, 04 Jun 2007 03:45:12 -0700
Hi guys,
I'm new to this group.
(If you're quick browsing mode, I know pipes aren't implemented
on WinCE, read on.)
I needed to somehow redirect a child process' stdout/stderr/stdin to
a parent process. I don't have the source of this child process. In
fact, the tool I'm building should be able to launch any child
process.
At first I tryed redirecting to temp files and use
GetStdioPathW/SetStdioPathW. Somehow it wasn't working correctly,
because the parent process wouldn't see the temp file grow passed
the few bytes (I think the parent would only see the first WriteFile
the child does.). I even tryied closing/reopening the file in a
loop waiting for it to grow, but no can do. It just wouldn't.
The second try I've done, is to implement a new stream device that
implements a pipe. I implemented a lib (hereafter called PipeLib)
that hides the device from the user and exposes an interface
similar to native WinNT/9x pipes, namelly, CreatePipe.
To get child's stdio redirected to the parent using PipeLib, the
parent will do:
/* showing only stdout for simplicity sake. */
HANDLE rd, wr;
WCHAR pipe_name[MAX_PATH];
WCHAR old_stdout_path[MAX_PATH];
/* create it - CreatePipe returns BOOL. */
CreatePipe (&rd, &wr, NULL, 0);
GetStdioPathW (1, old_stdout_path, &len);
/* get internal pipe name - this is really the device's name, like
PD00: */
GetPipeName (rd, pipe_name);
/* set it in the parent so the child inherits it */
SetStdioPathW (1, pipe_name);
CreateProcess (...);
/* restore */
SetStdioPathW (1, old_stdout_path);
while (1)
{
DWORD read;
/* read from the read end of the pipe */
/* block waiting for input */
if (!ReadFile (rd, ..., &read))
return;
if (read)
{
/* do whatever with the data. */
}
}
It all works well. I just have one issue, that is:
I'm using ActivateDevice to load the driver. Since I want to hide
the device nature of the pipe from the user, and I want to reuse
closed pipe devices, I don't pass the HANDLE returned by
ActivateDevice to the pipe creator (the CreatePipe caller). I would
like to somehow have the device auto deactivate when the last
file handle to it is closed.
Currently, I'm keeping a device internal reference count, and when
it reaches 0, I post a Windows message to an internal window that
is implemented in PipeLib. In PipeLib there is a thread that waits
for these messages and DeactivateDevice's the device accordingly.
This deactivating only works if the child closes all the pipes
handles before the parent. If the parent dies, the device will leak.
I'm a WinCE device writing newbie, sorry if I missed something
obvious.
The real solution I'm after is auto device unloading. Any ideas?
B.t.w: I'll release the Pipe driver and lib as BSD or similar once
these issues are fixed.
Cheers,
Pedro Alves
.
- Follow-Ups:
- Re: Pipes on Wince (I have them), and device auto-loading.
- From: Paul G. Tobey [eMVP]
- Re: Pipes on Wince (I have them), and device auto-loading.
- Prev by Date: WLAN Coverage area detection
- Next by Date: Re: mainstoneii and SDRAM support
- Previous by thread: WLAN Coverage area detection
- Next by thread: Re: Pipes on Wince (I have them), and device auto-loading.
- Index(es):
Relevant Pages
|
Loading