Skip to content

Instantly share code, notes, and snippets.

@vaibhavpandeyvpz
Last active August 29, 2015 14:03
Show Gist options
  • Save vaibhavpandeyvpz/13f1916cd4d2d6392e09 to your computer and use it in GitHub Desktop.
Save vaibhavpandeyvpz/13f1916cd4d2d6392e09 to your computer and use it in GitHub Desktop.
Blocking User Interaction in WinForms (C++/CLR)
#include "stdafx.h"
#include "blocker.h"
namespace VPZ
{
namespace Security
{
Blocker::Blocker(IntPtr Handle)
{
this->Handle = Handle;
this->SYSTEM_REGISTRY_KEY = "Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System";
this->SYSTEM_WINDOW_HIDE = 0;
this->SYSTEM_WINDOW_SHOW = 1;
}
IntPtr Blocker::CaptureKey(int Code, IntPtr Window, IntPtr Keypress)
{
if (Code >= 0)
{
bool Suppress = false;
KBDLLHOOKSTRUCT Key = safe_cast<KBDLLHOOKSTRUCT>(Marshal::PtrToStructure(Keypress, KBDLLHOOKSTRUCT::typeid));
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(this->Hook, Code, Window, Keypress);
}
void Blocker::DisableCloseButton()
{
DeleteMenu(GetSystemMenu(this->Handle, false), 6, 1024);
}
void Blocker::DisableSpecialKeys()
{
ProcessModule ^Module = Process::GetCurrentProcess()->MainModule;
this->Process = gcnew LowLevelKeyboardProc(this, &Blocker::CaptureKey);
this->Hook = SetWindowsHookEx(13, Process, GetModuleHandle(Module->ModuleName), 0);
}
void Blocker::DisableTaskBar()
{
ShowWindow(FindWindow("Shell_TrayWnd", ""), SYSTEM_WINDOW_HIDE);
}
void Blocker::DisableTaskManager()
{
RegistryKey ^Root = nullptr;
try
{
Root = Registry::CurrentUser->CreateSubKey(this->SYSTEM_REGISTRY_KEY);
Root->SetValue("DisableTaskMgr", "1");
}
catch (Exception ^Error)
{
}
finally
{
if (Root != nullptr)
{
try
{
Root->Close();
}
catch (Exception ^Error)
{
}
}
}
}
void Blocker::EnableCloseButton()
{
DeleteMenu(GetSystemMenu(this->Handle, true), 6, 1024);
}
void Blocker::EnableSpecialKeys()
{
UnhookWindowsHookEx(this->Hook);
}
void Blocker::EnableTaskBar()
{
ShowWindow(FindWindow("Shell_TrayWnd", ""), SYSTEM_WINDOW_SHOW);
}
void Blocker::EnableTaskManager()
{
RegistryKey ^Root = nullptr;
try
{
Root = Registry::CurrentUser;
RegistryKey^ Sub = Root->OpenSubKey(this->SYSTEM_REGISTRY_KEY);
if (Sub != nullptr)
Root->DeleteSubKeyTree(SYSTEM_REGISTRY_KEY);
}
catch (Exception ^Error)
{
}
finally
{
if (Root != nullptr)
{
try
{
Root->Close();
}
catch (Exception ^Error)
{
}
}
}
}
}
}
#pragma once
using namespace Microsoft::Win32;
using namespace System;
using namespace System::Collections::Generic;
using namespace System::Diagnostics;
using namespace System::Reflection;
using namespace System::Runtime::InteropServices;
using namespace System::Windows::Forms;
namespace VPZ
{
namespace Security
{
// Hooks
[DllImport("user32.dll")]
extern int GetSystemMenu(IntPtr Handle, bool Revert);
[DllImport("user32.dll")]
extern bool DeleteMenu(int Menu, int Position, int Flags);
[DllImport("user32.dll")]
extern int FindWindow(String^ Class, String^ Text);
[DllImport("user32.dll")]
extern int ShowWindow(int Handle, int Command);
delegate IntPtr LowLevelKeyboardProc(int Code, IntPtr Window, IntPtr Keypress);
[DllImport("user32.dll", CharSet = CharSet::Auto, SetLastError = true)]
extern IntPtr SetWindowsHookEx(int ID, LowLevelKeyboardProc^ Callback, IntPtr Handle, unsigned int Thread);
[DllImport("user32.dll", CharSet = CharSet::Auto, SetLastError = true)]
extern bool UnhookWindowsHookEx(IntPtr Hook);
[DllImport("user32.dll", CharSet = CharSet::Auto, SetLastError = true)]
extern IntPtr CallNextHookEx(IntPtr Hook, int Code, IntPtr Window, IntPtr Keypress);
[DllImport("kernel32.dll", CharSet = CharSet::Auto, SetLastError = true)]
extern IntPtr GetModuleHandle(String^ Name);
[StructLayout(LayoutKind::Sequential)]
value struct KBDLLHOOKSTRUCT
{
Keys Key;
int Code;
int Flags;
int Time;
IntPtr Extra;
};
// Class Definition
public ref class Blocker
{
private:
// Methods
IntPtr Handle;
IntPtr Hook;
LowLevelKeyboardProc^ Process;
String^ SYSTEM_REGISTRY_KEY;
int SYSTEM_WINDOW_HIDE;
int SYSTEM_WINDOW_SHOW;
// Keypress Capture
IntPtr CaptureKey(int Code, IntPtr Window, IntPtr Keypress);
public:
// Contructor
Blocker(IntPtr Handle);
// Disable close-button
void DisableCloseButton();
// Disable Key-press
void DisableSpecialKeys();
// Disable TaskBar
void DisableTaskBar();
// Disabke Task Manager
void DisableTaskManager();
// Re-enable Close Button
void EnableCloseButton();
// Re-enable Special Keys
void EnableSpecialKeys();
// Re-enable TaskBar
void EnableTaskBar();
// Re-enable Task Manager
void EnableTaskManager();
};
}
}
#include "demo.h"
namespace VPZ
{
namespace Security
{
Demo::Demo(void) {
this->InitializeComponent();
this->Blocker_ = gcnew Blocker(this->Handle);
}
Demo::~Demo() {
if (this->Components)
delete this->Components;
}
void Demo::Block()
{
this->Blocker_->DisableCloseButton();
this->Blocker_->DisableSpecialKeys();
this->Blocker_->DisableTaskBar();
this->Blocker_->DisableTaskManager();
this->Overlay = gcnew Form();
this->Overlay->BackColor = Color::Black;
this->Overlay->FormBorderStyle = ::FormBorderStyle::None;
this->Overlay->Opacity = 0.5;
this->Overlay->WindowState = FormWindowState::Maximized;
this->Overlay->Show();
}
void Demo::InitializeComponent(void) {
this->Buttons = gcnew Panel();
this->Negative = gcnew Button();
this->Positive = gcnew Button();
this->Description = gcnew Label();
this->Link = gcnew LinkLabel();
this->Buttons->SuspendLayout();
this->SuspendLayout();
//
// Buttons
//
this->Buttons->BackColor = System::Drawing::SystemColors::Window;
this->Buttons->Controls->Add(this->Negative);
this->Buttons->Controls->Add(this->Positive);
this->Buttons->Dock = System::Windows::Forms::DockStyle::Bottom;
this->Buttons->Location = System::Drawing::Point(0, 153);
this->Buttons->Name = "Buttons";
this->Buttons->Padding = System::Windows::Forms::Padding(8);
this->Buttons->Size = System::Drawing::Size(344, 48);
this->Buttons->TabIndex = 0;
//
// Negative
//
this->Negative->Dock = System::Windows::Forms::DockStyle::Right;
this->Negative->Enabled = false;
this->Negative->Location = System::Drawing::Point(240, 8);
this->Negative->Name = "Negative";
this->Negative->Size = System::Drawing::Size(96, 32);
this->Negative->TabIndex = 1;
this->Negative->Text = "Unblock";
this->Negative->UseVisualStyleBackColor = true;
this->Negative->Click += gcnew System::EventHandler(this, &Demo::OnNegativeClick);
//
// Positive
//
this->Positive->Dock = System::Windows::Forms::DockStyle::Left;
this->Positive->Location = System::Drawing::Point(8, 8);
this->Positive->Name = "Positive";
this->Positive->Size = System::Drawing::Size(96, 32);
this->Positive->TabIndex = 0;
this->Positive->Text = "Block";
this->Positive->UseVisualStyleBackColor = true;
this->Positive->Click += gcnew System::EventHandler(this, &Demo::OnPositiveClick);
//
// Demo
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(344, 201);
this->Controls->Add(this->Link);
this->Controls->Add(this->Description);
this->Controls->Add(this->Buttons);
this->MaximizeBox = false;
this->MinimizeBox = false;
this->Name = "Demo";
this->ShowInTaskbar = false;
this->StartPosition = System::Windows::Forms::FormStartPosition::CenterScreen;
this->Text = "VPZ | Blocking Demo";
this->TopMost = true;
this->Buttons->ResumeLayout(false);
this->ResumeLayout(false);
}
void Demo::OnNegativeClick(Object ^Sender, EventArgs ^Event)
{
this->Unblock();
(safe_cast<Button^>(Sender))->Enabled = false;
this->Positive->Enabled = true;
}
void Demo::OnPositiveClick(Object ^Sender, EventArgs ^Event)
{
this->Block();
(safe_cast<Button^>(Sender))->Enabled = false;
this->Negative->Enabled = true;
}
void Demo::Unblock()
{
this->Blocker_->EnableCloseButton();
this->Blocker_->EnableSpecialKeys();
this->Blocker_->EnableTaskBar();
this->Blocker_->EnableTaskManager();
this->Overlay->Close();
}
}
}
#pragma once
#include "stdafx.h"
#include "blocker.h"
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
namespace VPZ
{
namespace Security
{
public ref class Demo : public Form
{
public:
Demo(void);
protected:
~Demo();
private:
Blocker ^Blocker_;
Panel ^Buttons;
System::ComponentModel::Container ^Components;
Button ^Positive;
Button ^Negative;
Form ^Overlay;
void Block();
void InitializeComponent(void);
void OnNegativeClick(Object ^Sender, EventArgs ^Event);
void OnPositiveClick(Object ^Sender, EventArgs ^Event);
void Unblock();
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment