Microsoft KB Archive/199060

= PRB: ATL Service Run from the IDE Doesn't Log Events to Console =

Article ID: 199060

Article Last Modified on 12/11/2003

-

APPLIES TO

 Microsoft ActiveX Template Library 2.0, when used with:  Microsoft Visual C++ 5.0 Enterprise Edition

 Microsoft Visual C++ 6.0 Enterprise Edition

 Microsoft Visual C++ 5.0 Professional Edition

 Microsoft Visual C++ 6.0 Professional Edition</li></ul>

 Microsoft Visual C++ 6.0 Standard Edition</li></ul> </li> Microsoft ActiveX Template Library 3.0, when used with:  Microsoft Visual C++ 5.0 Enterprise Edition</li></ul>

 Microsoft Visual C++ 6.0 Enterprise Edition</li></ul>

 Microsoft Visual C++ 5.0 Professional Edition</li></ul>

 Microsoft Visual C++ 6.0 Professional Edition</li></ul>

 Microsoft Visual C++ 6.0 Standard Edition</li></ul> </li></ul>

-

<div class="notice_section">

This article was previously published under Q199060

<div class="symptoms_section">

SYMPTOMS
Using the ATL COM AppWizard to generate an NT service may not produce code that behaves as expected. When the AppWizard-generated service is started as a local server, the message strings passed to CServiceModule::LogEvent are not displayed to the console. When started as a service, the messages are logged into the applications' event log.

<div class="cause_section">

CAUSE
In the generated code CServiceModule::LogEvent calls _putts in to write the message to stdout, but stdout is not setup by default.

<div class="resolution_section">

RESOLUTION
To remedy this behavior, the developer should do one of the following:


 * Edit CServiceModule::Start to initialize stdout as desired (that is, redirect it either to a console or to a file). The code to do this is in the More Information section below.
 * Replace _putts with ATLTRACE.
 * Redirecting the output of the application to a file, in a console window. For example:

MyApp.exe > output.txt


 * In CServiceModule::LogEvent, remove the "if" statement and _putts statement, so that the output always goes to the application log file.

<div class="moreinformation_section">

MORE INFORMATION
To correct this problem, the developer should edit CServiceModule::Start as follows:

inline void CServiceModule::Start {      SERVICE_TABLE_ENTRY st[] = {          { m_szServiceName, _ServiceMain }, { NULL, NULL } };      if (m_bService && !::StartServiceCtrlDispatcher(st)) {          m_bService = FALSE; }      if (m_bService == FALSE) {          // Allocate a new console. AllocConsole; // Redirect stdout to the console. freopen("CONOUT$", "wta+", stdout); Run; // Free the console. FreeConsole; }  }

If the logs should be sent to a file, just redirect stdout to that file, without allocating/freeing the console.

Steps to Reproduce Behavior

 * 1) Create a new project with ATL COM AppWizard. In step 1, specify Server Type as Service(EXE).
 * 2) Build the project.
 * 3) Execute the project either from the command line or from the IDE.

Notice that the message "Service started" does not appear anywhere.
 * 1) Make the changes outlined in the first resoultion.
 * 2) Build and run.

You'll notice a new console displaying the message "Service started."

Keywords: kbservice kbprb KB199060

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.