Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting USB power state

Windows has the option of powering down certain peripherals, such as USB ports, to save power (this behavior can be enabled/disabled via Device Manager). The power down happens under various conditions such as when the lid of a laptop is closed. This is causing a problem for me as I have a GUI which talks to hardware attached to the USB port and communications are severed every time the laptop lid is closed. Is there a way to programmatically detect this power-down (standby?) event before it happens and more gracefully shut down my USB device? Is there a way to programmatically configure each of the system’s USB ports to disable this behavior?

Right now I'm looking at SystemEvents.PowerModeChanged, is this the right event to detect this?

like image 484
Joel B Avatar asked May 04 '11 14:05

Joel B


1 Answers

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

like image 123
Gavin Avatar answered Sep 24 '22 19:09

Gavin