A friend of mine is working on a service application for me.
He is trying to write to the console during the command line installation
and registration of the service but the printf does not produce output.
(neither does the writefile stdio. Also interestingly changing the winmain
to main results in duplicate declaration of new and dispose.

any ideas?

tia

--

peter walker Access MVP

Re: console output by William

William
Sun Apr 25 13:59:38 CDT 2004

"peter walker" <peter@papwalker.com.au.nospam> wrote in message
news:%23i415hqKEHA.2556@TK2MSFTNGP11.phx.gbl...
> A friend of mine is working on a service application for me.
> He is trying to write to the console during the command line installation
> and registration of the service but the printf does not produce output.
> (neither does the writefile stdio. Also interestingly changing the winmain
> to main results in duplicate declaration of new and dispose.
>
> any ideas?

Services by default run on an invisible desktop and not the interactive
desktop with which you are familiar.

Many service shell's provide a facility for running a service as a console
application on the interactive desktop. That's good as far as it goes but
ultimately you will want to debug the service where it sits.

If your friend passes a null terminated string containing the name of a log
file to this function:

/*************************************************************************
Sets the standard output device to point to the passed file name
*************************************************************************/
void CreateTraceLogFile(LPSTR pszName)
{
int fd;
FILE *fp;
HANDLE hFile;

hFile = CreateFile(pszName, GENERIC_WRITE, FILE_SHARE_READ, NULL,
OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

if ( hFile != INVALID_HANDLE_VALUE )
{
SetFilePointer(hFile, 0, NULL, FILE_END);

fd = _open_osfhandle( (long)hFile, 0);
fp = _fdopen(fd, "w");

*stdout = *fp;
setvbuf(stdout, NULL, _IONBF, 0);
}
}

then all of what would otherwise been displayed in a console window will be
written to a file without his having to change his code. What I do in my
services is to put the name of the trace log in the config file or registry
settings. If such a setting is present (as when I am debugging) I call the
function above.

The way I have written it, the trace log is cumulative. If you want to start
fresh each time change "FILE_END" to "FILE_BEGIN" above.

Regards,
Will



Re: console output by peter

peter
Mon Apr 26 09:13:47 CDT 2004

yes I realise what you say is true..however it's not relevant to what I'm
asking. I understand about services as I write my own using Delphi (not C++)
I can't offer him any worthwhile advise..

I am talking about writing to the console during the installation I know
some services that use
servicename /i

or

servicename / u

to install and uninstall the service from the SCM registry.
The generally write to the console ala...

servicename uninstalled successfully

or
...the service installed successfully - do you want to start it?
do you follow me?

--

peter walker MVP

Please post replies to the news group so everyone can benefit.
www.papwalker.com

"William DePalo [MVP VC++]" <willd.no.spam@mvps.org> wrote in message
news:u6NF1dvKEHA.3500@TK2MSFTNGP12.phx.gbl...
> "peter walker" <peter@papwalker.com.au.nospam> wrote in message
> news:%23i415hqKEHA.2556@TK2MSFTNGP11.phx.gbl...
> > A friend of mine is working on a service application for me.
> > He is trying to write to the console during the command line
installation
> > and registration of the service but the printf does not produce output.
> > (neither does the writefile stdio. Also interestingly changing the
winmain
> > to main results in duplicate declaration of new and dispose.
> >
> > any ideas?
>
> Services by default run on an invisible desktop and not the interactive
> desktop with which you are familiar.
>
> Many service shell's provide a facility for running a service as a console
> application on the interactive desktop. That's good as far as it goes but
> ultimately you will want to debug the service where it sits.
>
> If your friend passes a null terminated string containing the name of a
log
> file to this function:
>
> /*************************************************************************
> Sets the standard output device to point to the passed file name
> *************************************************************************/
> void CreateTraceLogFile(LPSTR pszName)
> {
> int fd;
> FILE *fp;
> HANDLE hFile;
>
> hFile = CreateFile(pszName, GENERIC_WRITE, FILE_SHARE_READ, NULL,
> OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
>
> if ( hFile != INVALID_HANDLE_VALUE )
> {
> SetFilePointer(hFile, 0, NULL, FILE_END);
>
> fd = _open_osfhandle( (long)hFile, 0);
> fp = _fdopen(fd, "w");
>
> *stdout = *fp;
> setvbuf(stdout, NULL, _IONBF, 0);
> }
> }
>
> then all of what would otherwise been displayed in a console window will
be
> written to a file without his having to change his code. What I do in my
> services is to put the name of the trace log in the config file or
registry
> settings. If such a setting is present (as when I am debugging) I call the
> function above.
>
> The way I have written it, the trace log is cumulative. If you want to
start
> fresh each time change "FILE_END" to "FILE_BEGIN" above.
>
> Regards,
> Will
>
>



Re: console output by William

William
Mon Apr 26 10:43:20 CDT 2004

"peter walker" <peter@papwalker.com.au.nospam> wrote in message
news:e0Ab1i5KEHA.3472@TK2MSFTNGP09.phx.gbl...
> yes I realise what you say is true..however it's not relevant to what I'm
> asking. I understand about services as I write my own using Delphi (not
C++)
> I can't offer him any worthwhile advise..
>
> I am talking about writing to the console during the installation I know
> some services that use
> servicename /i

If your friend can't write to the console from a service application which
is implemented as a console application and which runs on the interactive
desktop then he needs to find out what _he_ did wrong. :-)

Think about it, until he installs it, it is _not_ a service. If his
application is a console application and if he has not redirected the output
device it should work. Otherwise there would be a hue and cry about broken
consoles.

On the other hand, if your friend has a windowed application (i.e. one that
starts at WinMain() and pumps messages) then his expectations are misplaced.
Windowed applications do not have consoles to serve as sinks for printf().
Unless of course, one creates one with AllocConsole(). If he does this

AllocConsole();
HANDLE hCon = GetStdHandle(STD_OUTPUT_HANDLE);

then he can replace the handle returned from CreateFile() in my sample with
the handle to the console. At that point printf() will work.

The procedure to do that is documented from on high, here

http://support.microsoft.com/?id=105305

Regards,
Will