PRB: ATL Service Run from the IDE Doesn't Log Events to Console
ID: Q199060
|
The information in this article applies to:
-
The Microsoft Active Template Library (ATL), versions 2.0, 3.0, included with:
-
Microsoft Visual C++, 32-bit Editions, versions 5.0, 6.0
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.
CAUSE
In the generated code CServiceModule::LogEvent() calls _putts() in to write the message to stdout, but stdout is not setup by default.
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.
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
- Create a new project with ATL COM AppWizard. In step 1, specify Server Type as Service(EXE).
- Build the project.
- Execute the project either from the command line or from the IDE.
Notice that the message "Service started" does not appear anywhere.
- Make the changes outlined in the first resoultion.
- Build and run.
You'll notice a new console displaying the message "Service started."
Additional query words:
Keywords : kbATL200 kbATL210 kbService kbVC500 kbVC600 kbATL300 kbGrpMFCATL
Version : WINDOWS:2.0,3.0
Platform : WINDOWS
Issue type : kbprb