Detecting USB power state Detecting USB power state windows windows

Detecting USB power state


It sounds like you want

  1. WM_POWERBROADCAST (http://msdn.microsoft.com/en-us/library/aa373247(v=vs.85).aspx)
  2. RegisterPowerSettingNotification (http://msdn.microsoft.com/en-us/library/aa373196.aspx)

You first need to call RegisterPowerSettingNotification then WM_POWERBROADCAST messages will be received by your application.

This page has a c# implementation of a power management class using these window messages. http://www.koders.com/csharp/fid00BAA34B0CAA3E320F9F5A44610A015973BF28ED.aspx?s=nativemethods#L175


As mentioned by the previous posters RegisterPowerSettingNotification is what you want. To clarify, you can reference Winforms (System.Windows.Forms.dll) from other types of .NET applications (console, etc). You can get access to a Window handle (in order to receive messages) by subclassing a Winform (the Forms class) and overriding its WndProc.

MSDN has a very good article of doing just that, along with example code.