Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Using popen within a service

2 views
Skip to first unread message

Brian Hoyt

unread,
Nov 21, 1997, 3:00:00 AM11/21/97
to

I am trying to use popen within a service
but it always fails. I have tried to run
the service using a userid of admin and
it is able to start but popen still fails.

What gives?
Brian

Felix Kasza [MVP]

unread,
Nov 21, 1997, 3:00:00 AM11/21/97
to

Brian,

> I am trying to use popen within a service
> but it always fails.

Let me consult my crystal ball. Hmmm ... <intense concentration> ...
<deep frown> ... nope. Sorry, but "it always fails" is not enough info
for my crystal ball to work on.

Cheers,
Felix.

--
If you post a reply, kindly refrain from emailing it, too.

Brian Hoyt

unread,
Nov 21, 1997, 3:00:00 AM11/21/97
to
Felix,
You are right, my initial posting was a bit brief.
I may have found part of the problem though. I traced popen
and it fails when trying to duplicate stdout. A look at
stdout and stderr in the process structures for the service
show that they are not valid. This sort of makes sense because
a service can't write out to the "console". I was trying to
run a command and capture its stdout. I switched to using system()
and redirecting to a file and this appears to work.

Do you know if I am write about stderr and stdout for services?

Brian

Felix Kasza [MVP]

unread,
Nov 22, 1997, 3:00:00 AM11/22/97
to

Brian,

> Do you know if I am write about stderr and stdout for services?

It depends on whether you have a console or not. The easiest way to
circumvent that is to handle your pipes yourself:

Use CreatePipe() to create two pipes (better, three -- one for
stderr). Let's call them In, Out, Err, as seen from the program you
will start.

For each pipe you will get two handles, as in In_read and In_write.

Use DuplicateHandle() to make the following handles inheritable:

In_read --> In_read_inh
Out_write --> Out_write_inh
Err_write --> Err_write_inh

Close the non-inheritable copies (Inh_read, Out_write, Err_write).

Now, plug the inheritable handles into the STARTUPINFO structure and
set STARTF_USESTDHANDLES in its dwFlags member. Call CreateProcess().
Right afterwards, close the inheritable handles In_read_inh,
Out_write_inh, Err_write_inh.

You are left with three pipe handles, In_write (to write something to
the child's stdin), Out_read, and Err_read.

Do your thing and close the handles.

0 new messages