SetConsoleCtrlHandler function doesn’t receive logoff and shutdown events

現象

HandlerRoutine passed to SetConsoleCtrlHandler function doesn’t receive CTRL_LOGOFF_EVENT and CTRL_SHUTDOWN_EVENT during logoff and shutdown.

原因

This problem can be seen whenever the console application loads GDI32.dll and USER32.dll. OS after Windows 7 recognizes these process as a windows application instead of a console application because GDI32.dll and USER32.dll provide user interface functions. Windows application doesn’t receive CTRL_LOGOFF_EVENT and CTRL_SHUTDOWN_EVENT since these events are provided for console applications. Windows applications receives these events through window messages such as WM_QUERYENDSESSION and WM_ENDSESSION.

回避策

You can detect logoff and shutdown events by creating a hidden window in your console application. For example, create a hidden window by using CreateWindowEx function and pass zero to dwExStyle.

状況

Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.

詳細

This issue can be seen even if you don’t call GDI32.dll and USER32.dll directly inside your console application. For example, shell functions uses GDI32.dll and USER32.dll functions. Calling shell functions in your console application can make your application recognized as a windows application without having any intention.

関連情報

SetConsoleCtrlHandler function
https://msdn.microsoft.com/en-us/library/windows/desktop/ms686016(v=vs.85).aspx

HandlerRoutine callback function
https://msdn.microsoft.com/en-us/library/windows/desktop/ms683242(v=vs.85).aspx


WM_ENDSESSION message
https://msdn.microsoft.com/en-us/library/windows/desktop/aa376889(v=vs.85).aspx

WM_QUERYENDSESSION message
https://msdn.microsoft.com/en-us/library/windows/desktop/aa376890(v=vs.85).aspx

プロパティ

文書番号:3096128 - 最終更新日: 2016/09/29 - リビジョン: 1

フィードバック