zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    加法的位运算实现
    C++装饰器模式
    字符串类型的相互转换
    手算CRC及其实现
    Linux下搭建C/C++编程环境
    Deepin Linux 实体机安装
    Atom + Texlive 配置 Latex 环境
    有关字符串的算法(KMP,Manacher,BM)陆续补充
    Linux 下大文件分割与合并
    Ubuntu /目录满,发现是docker image 太多解决办法
  • 原文地址:https://www.cnblogs.com/strive-sun/p/12893293.html
Copyright © 2011-2022 走看看