Microsoft KB Archive/149901

{|
 * width="100%"|

INFO: Console App Launched from a Service Exits When User Logs Off

 * }

Q149901

-

The information in this article applies to:


 * Microsoft Win32 Application Programming Interface (API), used with:
 * the operating system: Microsoft Windows NT, versions 3.51, 4.0
 * the operating system: Microsoft Windows 2000

-

SUMMARY
Console applications contain a default console control handler. The console control handler function is called by the system when a process receives a CTRL_C, CTRL_BREAK, CTRL_CLOSE, CTRL_LOGOFF, or CTRL_SHUTDOWN signal. By default, when the console control handler receives any one of these signals, it calls ExitProcess.

When a console application is launched from a service, the application receives a modified default console control handler. The modified handler does not call ExitProcess for the CTRL_LOGOFF signal. This allows a console application to continue running when a user logs off the system. This corresponds with Windows NT services that run when no user is logged onto the system.

If the console application being launched from a service installs its own console control handler, this handler is called before the default handler. If the installed handler calls ExitProcess as a result of a CTRL_LOGOFF signal, the console application exits when the user logs off the system.

Using any third party libraries or DLLs could cause a console control handler to be installed for an application. If installed, this handler overrides the default handler, causing the application to exit when receiving a CTRL_LOGOFF signal.

For example, if you install a SIGBREAK handler via the C run-time function signal, the function installs a handler which overrides the default handler. This causes the application to exit when receiving a CTRL_LOGOFF event even though it was launched from a service. This problem can be resolved by either not installing the SIGBREAK handler or by installing another console control handler after the call to signal. The new console control handler must intercept the CTRL_LOGOFF event and just return TRUE for the application not to exit.