Is there any way to override the default HandlerEx in a C# Windows service within ServiceBase? All I can find is OnPowerEvent and OnCustomCommand.
For those of you wondering why I need this: OnPowerEvent doesn't handle GUID_LIDSWITCH_STATE_CHANGE. OnPowerEvent fires but doesn't indicate if the lid was closed or opened.
Here's my code:
using System;
using System.Runtime.InteropServices;
using System.ServiceProcess;
namespace KeefService
{
public partial class Main : ServiceBase
{
[DllImport("user32.dll")]
static extern bool BlockInput(bool fBlockIt);
[DllImport("User32.dll")]
static extern IntPtr RegisterPowerSettingNotification(IntPtr hRecipient, ref Guid PowerSettingGuid, Int32 Flags);
static Guid GUID_LIDSWITCH_STATE_CHANGE = new Guid(0xBA3E0F4D, 0xB817, 0x4094, 0xA2, 0xD1, 0xD5, 0x63, 0x79, 0xE6, 0xA0, 0xF3);
const int DEVICE_NOTIFY_SERVICE_HANDLE = 0x00000001;
const int PBT_POWERSETTINGCHANGE = 0x8013;
public Main()
{
InitializeComponent();
}
protected override bool OnPowerEvent(PowerBroadcastStatus powerStatus)
{
if ((int)powerStatus == PBT_POWERSETTINGCHANGE)
BlockInput(true); //TRUE OR FALSE!?!
return base.OnPowerEvent(powerStatus);
}
protected override void OnStart(string[] args)
{
RegisterPowerSettingNotification(this.ServiceHandle, ref GUID_LIDSWITCH_STATE_CHANGE, DEVICE_NOTIFY_SERVICE_HANDLE);
}
protected override void OnStop()
{
BlockInput(false);
}
}
}