• 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;
    }
  • 相关阅读:
    虚函数与动态多态
    我读苏东坡
    Linux高性能服务器编程学习-第三章思维导图笔记
    Linux高性能服务器编程学习-第二章思维导图笔记
    Linux高性能服务器编程学习-第一章思维导图笔记
    【Knockout】五、创建自定义绑定
    【Knockout】三、data-bind声明式绑定
    【Knockout】四、绑定上下文
    【Knockout】一、认识Knockout,你会爱上它
    【Knockout】二、监控属性Observables
  • 原文地址:https://www.cnblogs.com/strive-sun/p/12893293.html
Copyright © 2020-2023  润新知