0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

cscの作法 その132

Posted at

概要

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());
		}
	}
}





以上。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?