Type a page name and press Enter. You'll jump to the page if it exists, or you can create it if it doesn't.
To create a page in a module other than powrprof, prefix the name with the module name and a period.
Declare Function PowerSettingRegisterNotification Lib "powrprof.dll" (TODO) As TODO
User-Defined Types:
/// <summary>
/// OS callback delegate definition
/// </summary>
/// <param name="context">The context for the callback</param>
/// <param name="type">The type of the callback...for power notifcation it's a PBT_ message</param>
/// <param name="setting">A structure related to the notification, depends on type parameter</param>
/// <returns></returns>
delegate int DeviceNotifyCallbackRoutine(IntPtr context, int type, IntPtr setting);
/// <summary>
/// A callback definition
/// </summary>
[StructLayout(LayoutKind.Sequential)]
struct DEVICE_NOTIFY_SUBSCRIBE_PARAMETERS
{
public DeviceNotifyCallbackRoutine Callback;
public IntPtr Context;
}
[DllImport("Powrprof.dll", SetLastError = true)]
private static extern int PowerSettingUnregisterNotification(IntPtr registrationHandle);
public void Register()
{
_recipient = new DEVICE_NOTIFY_SUBSCRIBE_PARAMETERS
{
Callback = HandlerCallback,
Context = IntPtr.Zero
};
var registrationHandle = new IntPtr();
var result = PowerSettingRegisterNotification(ref GUID_CONSOLE_DISPLAY_STATE, DEVICE_NOTIFY_CALLBACK, ref _recipient, ref registrationHandle);
if (result == 0)
{
_handle = registrationHandle;
}
else
{
Console.WriteLine("Error: {0}", result);
}
}
public void Unregister()
{
if (_handle != IntPtr.Zero)
{
PowerSettingUnregisterNotification(_handle);
_handle = IntPtr.Zero;
}
}
private int HandlerCallback(IntPtr context, int eventType, IntPtr setting)
{
if (eventType == PBT_POWERSETTINGCHANGE
&& Marshal.PtrToStructure(setting, typeof(POWERBROADCAST_SETTING)) is POWERBROADCAST_SETTING powersetting
&& powersetting.PowerSetting == GUID_CONSOLE_DISPLAY_STATE)
{
switch (powersetting.Data)
{
case 0x0: // 0x0 - The display is off.
Console.WriteLine("Display OFF");
break;
case 0x1: // 0x1 - The display is on.
Console.WriteLine("Display ON");
break;
case 0x2: // 0x2 - The display is dimmed.
Console.WriteLine("Display DIMMED");
break;
}
}
return 0;
}
Please edit this page!
Do you have...
helpful tips or sample code to share for using this API in managed code?
corrections to the existing content?
variations of the signature you want to share?
additional languages you want to include?
Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).