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

HandlerRoutine callback function



注意 : これは、マイクロソフトのサポート組織内で直接作成された "緊急公開" の資料です。 この資料には、確認中の問題に関する現状ベースの情報が記載されています。 情報提供のスピードを優先するため、資料には誤植が含まれる可能性があり、予告なしに随時改定される場合があります。 その他の考慮事項については、使用条件を参照してください。

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

Windows Software Development Kit for Windows 7 and .NET Framework 4, Windows Software Development Kit for Windows 8, Windows Software Development Kit for Windows 8.1, Windows Software Development Kit for Windows 10

  • kbconsole KB3096128