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 - 最終更新日: 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
フィードバック