PowerManager/PowerManager.cpp
author StephaneLenclud
Thu, 24 Sep 2015 21:39:05 +0200
changeset 159 e7c8c2b500bd
child 160 de942d321cfb
permissions -rw-r--r--
Add PowerManager C++/CLI project.
Monitor Power On/Off notifications working.
Moving to .NET 4.6
     1 // This is the main DLL file.
     2 
     3 #include "stdafx.h"
     4 
     5 #include "PowerManager.h"
     6 
     7 using namespace System::Diagnostics;
     8 
     9 namespace PowerManager
    10 {
    11     ///
    12     PowerSettingNotifier::PowerSettingNotifier(IntPtr aHandle, Boolean aService)
    13     {
    14         Construct(aHandle, aService);
    15     }
    16 
    17     ///
    18     PowerSettingNotifier::PowerSettingNotifier(IntPtr aHandle)
    19     {
    20         //By default we assume we run as a Window
    21         Construct(aHandle, false);
    22     }
    23 
    24     ///
    25     void PowerSettingNotifier::Construct(IntPtr aHandle, Boolean aService)
    26     {
    27         iHandle = aHandle;
    28         iIsService = aService;
    29         iMonitorPowerOnDelegate = nullptr;
    30         iMonitorPowerOffDelegate = nullptr;
    31         iMonitorPowerObserverCount = 0;
    32     }
    33 
    34     ///
    35     Boolean PowerSettingNotifier::RegisterPowerSettingNotification(IntPtr aHandle, Boolean aService)
    36 	{
    37         HANDLE handle = aHandle.ToPointer();        
    38         HPOWERNOTIFY res=::RegisterPowerSettingNotification(handle, &GUID_MONITOR_POWER_ON, (aService?DEVICE_NOTIFY_SERVICE_HANDLE:DEVICE_NOTIFY_WINDOW_HANDLE));
    39         return (res != NULL);
    40 	};
    41 
    42     /// 
    43     Boolean PowerSettingNotifier::RegisterPowerSettingNotification(IntPtr aHandle)
    44     {
    45         return RegisterPowerSettingNotification(aHandle,false);
    46     };
    47 
    48     ///
    49     void PowerSettingNotifier::WndProc(Message% aMessage)
    50     {
    51         POWERBROADCAST_SETTING* setting;
    52 
    53         if (aMessage.Msg == WM_POWERBROADCAST && aMessage.WParam.ToInt32() == PBT_POWERSETTINGCHANGE)
    54         {
    55             setting=(POWERBROADCAST_SETTING*)aMessage.LParam.ToPointer();
    56             if (setting->PowerSetting == GUID_MONITOR_POWER_ON)
    57             {
    58                 if (setting->Data[0] == 0x0)
    59                 {
    60                     Debug::WriteLine(L"POWERBROADCAST: Monitor Power Off");
    61                     OnMonitorPowerOff();
    62                 }
    63                 else if (setting->Data[0] == 0x1)
    64                 {
    65                     Debug::WriteLine(L"POWERBROADCAST: Monitor Power On");
    66                     OnMonitorPowerOn();
    67                 }
    68             }
    69         }
    70 
    71 
    72     }
    73 
    74     ///
    75     void PowerSettingNotifier::OnMonitorPowerOn::add(PowerManagerDelegate^ d)
    76     {
    77         iMonitorPowerOnDelegate += d;
    78         iMonitorPowerObserverCount++;
    79         //iMonitorPowerOnDelegate->GetInvocationList()->GetLength(0)
    80         if (iMonitorPowerObserverCount == 1)
    81         {
    82             //TODO: register
    83             RegisterPowerSettingNotification(iHandle,iIsService);
    84         }
    85 
    86     }
    87 
    88     ///
    89     void PowerSettingNotifier::OnMonitorPowerOn::remove(PowerManagerDelegate^ d)
    90     {
    91         iMonitorPowerOnDelegate -= d;
    92         iMonitorPowerObserverCount--;
    93         if (iMonitorPowerObserverCount==0)
    94         {
    95             //TODO: unregister
    96         }
    97     }
    98 
    99     //
   100     void PowerSettingNotifier::OnMonitorPowerOn::raise()
   101     {        
   102         if (iMonitorPowerOnDelegate != nullptr)
   103         {
   104             iMonitorPowerOnDelegate->Invoke();
   105         }
   106     }
   107 
   108     ///
   109     void PowerSettingNotifier::OnMonitorPowerOff::add(PowerManagerDelegate^ d)
   110     {
   111         iMonitorPowerOffDelegate += d;
   112         iMonitorPowerObserverCount++;
   113         if (iMonitorPowerObserverCount == 1)
   114         {
   115             //TODO: register
   116             RegisterPowerSettingNotification(iHandle, iIsService);
   117         }
   118     }
   119 
   120     ///
   121     void PowerSettingNotifier::OnMonitorPowerOff::remove(PowerManagerDelegate^ d)
   122     {
   123         iMonitorPowerOffDelegate -= d;
   124         iMonitorPowerObserverCount--;
   125         if (iMonitorPowerObserverCount == 0)
   126         {
   127             //TODO: unregister
   128         }
   129     }
   130 
   131     //
   132     void PowerSettingNotifier::OnMonitorPowerOff::raise()
   133     {
   134         if (iMonitorPowerOffDelegate != nullptr)
   135         {
   136             iMonitorPowerOffDelegate->Invoke();
   137         }        
   138     }
   139 
   140 
   141 
   142 }