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

Windows Service Stdout/Stderr Redirection not working

763 views
Skip to first unread message

Anand CS

unread,
Oct 10, 2003, 12:50:24 PM10/10/03
to
Hi All
I have a question regarding redirecting stdout and stderr in a
WindowsService
I have duped a log file descriptor to stdout and stderr in an
application.
When the application is run in console mode the redirecting of
stdout/stderr works fine. i.e. my printfs are redirected to my log
file

When the same application is run in Windows Service mode, even though
the redirecting succeeds I dont see the outputs in my log file. I have
a valid log file and there is no problems in redirecting...Just that
in windows service mode I dont see the output in my redirected file.

Do anybody know what is the mistake Im doing here
Any help is appreciated.
Thanks
Anand

Oliver Fleischmann

unread,
Oct 10, 2003, 4:15:09 PM10/10/03
to

Hi,

if you set the subsystem in your linker settings to WINDOWS instead of
CONSOLE, anything sent to stdout or stderr will be silently
disregarded.

So if you would like to print anything to your logfiles in a
SUBSYSTEM:WINDOWS built, you would have to open your logfile manually
and write to it with e.g. fprintf.
Or you create yourself some macros, which write to stdout/stderr in
your Debug built, which may use SUBSYSTEM:CONSOLE
and to a custom stream in your Release built which may use WINDOWS as
subsystem.


Olli

K_Lee

unread,
Oct 11, 2003, 4:12:05 AM10/11/03
to
anan...@yahoo.com (Anand CS) wrote in message news:<7cccd2d3.03101...@posting.google.com>...

Here's how Apache did stdin/out dup in Win32

http://www.slink-software.com/W/SrcDoc_Top/apache_1.3.28/apache_1.3.28.sdoc/N_105

/* Open a null handle to nak our stdin */
hNullFile = CreateFile("nul", GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
&sa, OPEN_EXISTING, 0, NULL);
if (hNullFile == INVALID_HANDLE_VALUE) {
ap_log_error(APLOG_MARK, APLOG_WIN32ERROR | APLOG_CRIT, NULL,
"Parent: Unable to create null stdin pipe for
this service process.\n");
}
else {
int fh;
FILE *fl;
fflush(stdin);
SetStdHandle(STD_INPUT_HANDLE, hNullFile);
fh = _open_osfhandle((long) STD_INPUT_HANDLE,
_O_RDONLY | _O_BINARY);
dup2(fh, STDIN_FILENO);
fl = _fdopen(STDIN_FILENO, "rcb");
memcpy(stdin, fl, sizeof(FILE));

0 new messages