I\'m writing a C# app (specifically for laptops) and I want to be aware of the state of the lid, i.e. when is it open and when is it closed.
I\'ve already used pInvoke a
I don't think 'lid state' is guaranteed to be reflected in the power management properties. Is your interest in the lid state simply down to the monitor? If so, you could find a way to determine the properties of the integrated screen (could be an issue if they install it while connected to an external monitor) and save them. Then check the properties of the screen/s in use and act accordingly.
See System.Windows.Forms.Screen - http://msdn.microsoft.com/en-us/library/system.windows.forms.screen%28v=vs.110%29.aspx
Serial numbers can be extracted from the 'Device Parameters' keys in HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\DISPLAY
. How to extract it (along with other info) is demonstrated here:
http://sourceforge.net/p/wmimonitor/code/HEAD/tree/DisplayInfoWMIProvider/WMIProvider/WMIProvider.cs
Alternatively, assuming that the callbacks still fire even if the lid close event in Windows is set to 'Do Nothing', you could perhaps consider a helper process or service to run all the time and keep track of changes. You could share the state via the registry or a file. You could also look into whether any other items of hardware (eg. touchpad) change power state on close regardless of the 'power plan' and use the GetDevicePowerState API function.
So after a long search led by @Llwyd's answer, I found a solution.
I noticed that when the lid is closed the device is removed from the device manager, so the solution is to check whether it is there or not.
The code behind it is a bit massive to upload here so I won't, but the following was need:
1) Identify Internal screen's name\ID with WmiMonitorConnectionParams
and VideoOutputTechnology
and state (DISPLAY_DEVICE_ATTACHED
and/or DISPLAY_DEVICE_ACTIVE
)
2) Once I know the device's name/ID I can easily look it up by enumerating System.Windows.Forms.Screen.AllScreens (or save the value from the DISPLAY_DEVICE_ATTACHED)