Adding user given name to EAR events and enabling User Events.
1 // This is the main DLL file.
5 #include "PowerManager.h"
7 using namespace System::Diagnostics;
12 SettingNotifier::SettingNotifier(IntPtr aHandle, Boolean aService)
14 Construct(aHandle, aService);
18 SettingNotifier::SettingNotifier(IntPtr aHandle)
20 //By default we assume we run as a Window
21 Construct(aHandle, false);
25 void SettingNotifier::Construct(IntPtr aHandle, Boolean aService)
28 iIsService = aService;
29 iMonitorPowerOnDelegate = nullptr;
30 iMonitorPowerOffDelegate = nullptr;
31 iMonitorPowerObserverCount = 0;
32 iMonitorPowerHandle = NULL;
36 HPOWERNOTIFY SettingNotifier::RegisterPowerSettingNotification(LPCGUID aGuid)
38 HANDLE handle = iHandle.ToPointer();
39 return ::RegisterPowerSettingNotification(handle, aGuid, (iIsService?DEVICE_NOTIFY_SERVICE_HANDLE:DEVICE_NOTIFY_WINDOW_HANDLE));
43 void SettingNotifier::WndProc(Message% aMessage)
45 POWERBROADCAST_SETTING* setting;
47 if (aMessage.Msg == WM_POWERBROADCAST && aMessage.WParam.ToInt32() == PBT_POWERSETTINGCHANGE)
49 setting=(POWERBROADCAST_SETTING*)aMessage.LParam.ToPointer();
50 if (setting->PowerSetting == GUID_MONITOR_POWER_ON)
52 if (setting->Data[0] == 0x0)
54 Debug::WriteLine(L"POWERBROADCAST: Monitor Power Off");
57 else if (setting->Data[0] == 0x1)
59 Debug::WriteLine(L"POWERBROADCAST: Monitor Power On");
67 void SettingNotifier::OnMonitorPowerOn::add(PowerManagerDelegate^ d)
69 iMonitorPowerOnDelegate += d;
70 iMonitorPowerObserverCount++;
71 //iMonitorPowerOnDelegate->GetInvocationList()->GetLength(0)
72 if (iMonitorPowerObserverCount == 1)
74 //Register for monitor power notifications
75 iMonitorPowerHandle=RegisterPowerSettingNotification(&GUID_MONITOR_POWER_ON);
81 void SettingNotifier::OnMonitorPowerOn::remove(PowerManagerDelegate^ d)
83 iMonitorPowerOnDelegate -= d;
84 iMonitorPowerObserverCount--;
85 if (iMonitorPowerObserverCount==0)
87 //Unregister from corresponding power setting notification
88 UnregisterPowerSettingNotification(iMonitorPowerHandle);
93 void SettingNotifier::OnMonitorPowerOn::raise()
95 if (iMonitorPowerOnDelegate != nullptr)
97 iMonitorPowerOnDelegate->Invoke();
102 void SettingNotifier::OnMonitorPowerOff::add(PowerManagerDelegate^ d)
104 iMonitorPowerOffDelegate += d;
105 iMonitorPowerObserverCount++;
106 if (iMonitorPowerObserverCount == 1)
108 //Register for monitor power notifications
109 iMonitorPowerHandle = RegisterPowerSettingNotification(&GUID_MONITOR_POWER_ON);
114 void SettingNotifier::OnMonitorPowerOff::remove(PowerManagerDelegate^ d)
116 iMonitorPowerOffDelegate -= d;
117 iMonitorPowerObserverCount--;
118 if (iMonitorPowerObserverCount == 0)
120 //Unregister from corresponding power setting notification
121 UnregisterPowerSettingNotification(iMonitorPowerHandle);
126 void SettingNotifier::OnMonitorPowerOff::raise()
128 if (iMonitorPowerOffDelegate != nullptr)
130 iMonitorPowerOffDelegate->Invoke();