概要
cscの作法、調べてみた。
WtsApi32.dll使ってみた。
参考にしたページ
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace LockDetect
{
public partial class Form1 : Form {
[DllImport("WtsApi32.dll")]
private static extern bool WTSRegisterSessionNotification(IntPtr hWnd, [MarshalAs(UnmanagedType.U4)] int dwFlags);
[DllImport("WtsApi32.dll")]
private static extern bool WTSUnRegisterSessionNotification(IntPtr hWnd);
const int NOTIFY_FOR_THIS_SESSION = 0;
const int WM_WTSSESSION_CHANGE = 0x2b1;
const int WTS_SESSION_LOCK = 0x7;
const int WTS_SESSION_UNLOCK = 0x8;
const int WTS_SESSION_REMOTE_CONTROL = 0x9;
public Form1() {
WTSRegisterSessionNotification(this.Handle, NOTIFY_FOR_THIS_SESSION);
}
protected override void WndProc(ref Message m) {
if (m.Msg == WM_WTSSESSION_CHANGE)
{
int value = m.WParam.ToInt32();
switch (value)
{
case WTS_SESSION_LOCK:
Console.WriteLine("PCがロックされました");
break;
case WTS_SESSION_UNLOCK:
Console.WriteLine("PCのロックが解除されました");
break;
case WTS_SESSION_REMOTE_CONTROL:
Console.WriteLine("PCがRDP制御されました");
break;
default:
break;
}
}
base.WndProc(ref m);
}
[STAThread]
public static void Main() {
Application.Run(new Form1());
}
}
}
以上。