Add PowerManager C++/CLI project.
Monitor Power On/Off notifications working.
Moving to .NET 4.6
1 // This is the main DLL file.
5 #include "PowerManager.h"
7 using namespace System::Diagnostics;
12 PowerSettingNotifier::PowerSettingNotifier(IntPtr aHandle, Boolean aService)
14 Construct(aHandle, aService);
18 PowerSettingNotifier::PowerSettingNotifier(IntPtr aHandle)
20 //By default we assume we run as a Window
21 Construct(aHandle, false);
25 void PowerSettingNotifier::Construct(IntPtr aHandle, Boolean aService)
28 iIsService = aService;
29 iMonitorPowerOnDelegate = nullptr;
30 iMonitorPowerOffDelegate = nullptr;
31 iMonitorPowerObserverCount = 0;
35 Boolean PowerSettingNotifier::RegisterPowerSettingNotification(IntPtr aHandle, Boolean aService)
37 HANDLE handle = aHandle.ToPointer();
38 HPOWERNOTIFY res=::RegisterPowerSettingNotification(handle, &GUID_MONITOR_POWER_ON, (aService?DEVICE_NOTIFY_SERVICE_HANDLE:DEVICE_NOTIFY_WINDOW_HANDLE));
43 Boolean PowerSettingNotifier::RegisterPowerSettingNotification(IntPtr aHandle)
45 return RegisterPowerSettingNotification(aHandle,false);
49 void PowerSettingNotifier::WndProc(Message% aMessage)
51 POWERBROADCAST_SETTING* setting;
53 if (aMessage.Msg == WM_POWERBROADCAST && aMessage.WParam.ToInt32() == PBT_POWERSETTINGCHANGE)
55 setting=(POWERBROADCAST_SETTING*)aMessage.LParam.ToPointer();
56 if (setting->PowerSetting == GUID_MONITOR_POWER_ON)
58 if (setting->Data[0] == 0x0)
60 Debug::WriteLine(L"POWERBROADCAST: Monitor Power Off");
63 else if (setting->Data[0] == 0x1)
65 Debug::WriteLine(L"POWERBROADCAST: Monitor Power On");
75 void PowerSettingNotifier::OnMonitorPowerOn::add(PowerManagerDelegate^ d)
77 iMonitorPowerOnDelegate += d;
78 iMonitorPowerObserverCount++;
79 //iMonitorPowerOnDelegate->GetInvocationList()->GetLength(0)
80 if (iMonitorPowerObserverCount == 1)
83 RegisterPowerSettingNotification(iHandle,iIsService);
89 void PowerSettingNotifier::OnMonitorPowerOn::remove(PowerManagerDelegate^ d)
91 iMonitorPowerOnDelegate -= d;
92 iMonitorPowerObserverCount--;
93 if (iMonitorPowerObserverCount==0)
100 void PowerSettingNotifier::OnMonitorPowerOn::raise()
102 if (iMonitorPowerOnDelegate != nullptr)
104 iMonitorPowerOnDelegate->Invoke();
109 void PowerSettingNotifier::OnMonitorPowerOff::add(PowerManagerDelegate^ d)
111 iMonitorPowerOffDelegate += d;
112 iMonitorPowerObserverCount++;
113 if (iMonitorPowerObserverCount == 1)
116 RegisterPowerSettingNotification(iHandle, iIsService);
121 void PowerSettingNotifier::OnMonitorPowerOff::remove(PowerManagerDelegate^ d)
123 iMonitorPowerOffDelegate -= d;
124 iMonitorPowerObserverCount--;
125 if (iMonitorPowerObserverCount == 0)
132 void PowerSettingNotifier::OnMonitorPowerOff::raise()
134 if (iMonitorPowerOffDelegate != nullptr)
136 iMonitorPowerOffDelegate->Invoke();