USB device and Windows shutdown

ssalmi 1 Reputation point
2022-02-01T16:45:52.907+00:00

Hello,

I am using a USB device that works like a USB keyboard connected to a USB port of PC or laptop. The USB device powered separately and the PC USB port is not providing the power to the USB device. .
I'd like to let the USB device know when Windows is shutting down on the PC or laptop.
Is there any messages, during Windows shut down, sent to the USB port that the USB device can be listening on?

Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,449 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Junjie Zhu - MSFT 15,836 Reputation points Microsoft Vendor
    2022-02-02T09:57:15.677+00:00

    Hello,
    Welcome to Microsoft Q&A!

    Windows shutdown will send WM_QUERYENDSESSION message to all programs, programs receive this message will automatically exit, your program if running will receive this message, as long as the program intercept the message and process.
    So you can intercept to this message in the program and send Windows shutdown Message to your USB device via port.

    https://learn.microsoft.com/en-us/windows/win32/shutdown/wm-queryendsession

    About “How to distinguish shutdown between restart,” need access to system logs.
    You can use ReadEventLog to read the System log when WM_QUERYENDSESSION, You can refer to this Querying for Event Information sample.

    https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-readeventloga
    https://learn.microsoft.com/en-us/windows/win32/eventlog/querying-for-event-source-messages

    170718-capture1.png
    Thank you.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.