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