Skip to content

Instantly share code, notes, and snippets.

@vaibhavpandeyvpz
Last active May 12, 2018 21:04
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vaibhavpandeyvpz/5471d6799f7a976ba366 to your computer and use it in GitHub Desktop.
Save vaibhavpandeyvpz/5471d6799f7a976ba366 to your computer and use it in GitHub Desktop.
Blocking User Interaction in WinForms (C#)
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace VPZ.Security
{
public class Blocker
{
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr CallNextHookEx(IntPtr hook, int nCode, IntPtr wp, IntPtr lp);
[DllImport("user32")]
private static extern bool DeleteMenu(int Menu, int Position, int Flags);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern int FindWindow(string Class, string Text);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern short GetAsyncKeyState(Keys key);
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr GetModuleHandle(string name);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern int GetSystemMenu(IntPtr Handle, bool Revert);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr SetWindowsHookEx(int id, LowLevelKeyboardProc callback, IntPtr hMod, uint dwThreadId);
[DllImport("user32.dll")]
private static extern int ShowWindow(int Handle, int Command);
private delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern bool UnhookWindowsHookEx(IntPtr hook);
[StructLayout(LayoutKind.Sequential)]
private struct KBDLLHOOKSTRUCT
{
public Keys Key;
public int Code;
public int Flags;
public int Time;
public IntPtr Extra;
}
private IntPtr Handle;
private IntPtr Hook;
private LowLevelKeyboardProc Process;
private const String SYSTEM_REGISTRY_KEY = "Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System";
private const int SYSTEM_WINDOW_HIDE = 0;
private const int SYSTEM_WINDOW_SHOW = 1;
public Blocker(IntPtr Handle)
{
this.Handle = Handle;
}
private IntPtr CaptureKey(int Code, IntPtr Window, IntPtr Process)
{
if (Code >= 0)
{
bool Suppress = false;
KBDLLHOOKSTRUCT Key = (KBDLLHOOKSTRUCT)Marshal.PtrToStructure(Process, typeof(KBDLLHOOKSTRUCT));
if ((Key.Key == Keys.Escape && ((Key.Flags & 0x20) == 0x20)))
Suppress = true;
if ((Key.Key == Keys.Escape) && ((Control.ModifierKeys & Keys.Control) == Keys.Control))
Suppress = true;
if ((Key.Key == Keys.F4 && ((Key.Flags & 0x20) == 0x20)))
Suppress = true;
if ((Key.Key == Keys.Tab && ((Key.Flags & 0x20) == 0x20)))
Suppress = true;
if ((Key.Key == Keys.LWin) || (Key.Key == Keys.RWin))
Suppress = true;
if (Suppress)
return (IntPtr)1;
}
return CallNextHookEx(Hook, Code, Window, Process);
}
public void DisableCloseButton()
{
DeleteMenu(GetSystemMenu(this.Handle, false), 6, 1024);
}
public void DisableSpecialKeys()
{
ProcessModule Module = System.Diagnostics.Process.GetCurrentProcess().MainModule;
Process = new LowLevelKeyboardProc(CaptureKey);
Hook = SetWindowsHookEx(13, Process, GetModuleHandle(Module.ModuleName), 0);
}
public void DisableTaskBar()
{
ShowWindow(FindWindow("Shell_TrayWnd", ""), SYSTEM_WINDOW_HIDE);
}
public void DisableTaskManager()
{
RegistryKey Root = null;
try
{
Root = Registry.CurrentUser.CreateSubKey(SYSTEM_REGISTRY_KEY);
Root.SetValue("DisableTaskMgr", "1");
}
catch (Exception Error)
{ }
finally
{
if (Root != null)
{
try
{
Root.Close();
}
catch (Exception Error)
{ }
}
}
}
public void EnableCloseButton()
{
DeleteMenu(GetSystemMenu(this.Handle, true), 6, 1024);
}
public void EnableSpecialKeys()
{
UnhookWindowsHookEx(Hook);
}
public void EnableTaskBar()
{
ShowWindow(FindWindow("Shell_TrayWnd", ""), SYSTEM_WINDOW_SHOW);
}
public void EnableTaskManager()
{
RegistryKey Root = null;
try
{
Root = Registry.CurrentUser;
RegistryKey Sub = Root.OpenSubKey(SYSTEM_REGISTRY_KEY);
if (Sub != null)
Root.DeleteSubKeyTree(SYSTEM_REGISTRY_KEY);
}
catch (Exception Error)
{ }
finally
{
if (Root != null)
{
try
{
Root.Close();
}
catch (Exception Error)
{ }
}
}
}
}
}
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
namespace VPZ.Security
{
public partial class Demo : Form
{
private Blocker Blocker;
private Form Overlay;
public Demo()
{
this.InitializeComponent();
this.Blocker = new Blocker(this.Handle);
}
private void Block()
{
this.Blocker.DisableCloseButton();
this.Blocker.DisableSpecialKeys();
this.Blocker.DisableTaskBar();
this.Blocker.DisableTaskManager();
// This overlay prevents mouse-clicks
this.Overlay = new Form()
{
BackColor = Color.Black,
FormBorderStyle = FormBorderStyle.None,
Opacity = 0.5,
WindowState = FormWindowState.Maximized
};
this.Overlay.Show();
}
private void OnNegativeClick(object Sender, EventArgs Event)
{
this.Unblock();
((Button)Sender).Enabled = false;
this.Positive.Enabled = true;
}
private void OnPositiveClick(object Sender, EventArgs Event)
{
this.Block();
((Button)Sender).Enabled = false;
this.Negative.Enabled = true;
}
private void Unblock()
{
this.Blocker.EnableCloseButton();
this.Blocker.EnableSpecialKeys();
this.Blocker.EnableTaskBar();
this.Blocker.EnableTaskManager();
this.Overlay.Close();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment