win32-WH_KEYBOARD的使用
我们使用WH_KEYBOARD来禁用记事本的ALT的按键
.cpp
#include <Windows.h> #include <stdio.h> #include <tchar.h> unsigned long GetTargetThreadIdFromWindow(const char* className, const char* windowName) { HWND targetWnd; HANDLE hProcess; unsigned long processID = 0; targetWnd = FindWindow(className, windowName); return GetWindowThreadProcessId(targetWnd, &processID); } int main() { unsigned long threadID = GetTargetThreadIdFromWindow("Notepad", "renote.txt - Notepad"); printf("TID: %i", threadID); HINSTANCE hinst = LoadLibrary(_T("Mydll.dll")); if (hinst) { typedef void (*Install)(unsigned long); typedef void (*Uninstall)(); Install install = (Install)GetProcAddress(hinst, "install"); Uninstall uninstall = (Uninstall)GetProcAddress(hinst, "uninstall"); install(threadID); MSG msg = {}; while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } uninstall(); } return 0; }
.mydll
// dllmain.cpp : Defines the entry point for the DLL application. #include "pch.h" #include <windows.h> #include <iostream> #include <stdio.h> HINSTANCE hinst; #pragma data_seg(".shared") HHOOK hhk; #pragma data_seg() LRESULT CALLBACK wireKeyboardProc(int code, WPARAM wParam, LPARAM lParam) { if (code >= 0) { switch (wParam) { case VK_MENU: { return 1; } } } return CallNextHookEx(hhk, code, wParam, lParam); } extern "C" __declspec(dllexport) void install(unsigned long threadID) { hhk = SetWindowsHookEx(WH_KEYBOARD, wireKeyboardProc, hinst, threadID); } extern "C" __declspec(dllexport) void uninstall() { UnhookWindowsHookEx(hhk); } BOOL WINAPI DllMain(__in HINSTANCE hinstDLL, __in DWORD fdwReason, __in LPVOID lpvReserved) { hinst = hinstDLL; return TRUE; }