Global hook for WH_GETMESSAGE hooks only to one application (VisualStudio)

空扰寡人 提交于 2019-12-13 03:59:29

问题


I have a C# application that calls an external DLL file for the hook process. The hook process simply 'hijacks' key presses, converting lowercase characters into uppercase. I thought it was working only to find out that only VisualStudio gets hooked successfully. Other applications like chrome and explorer does not seem to perform the hook process. Is there anything I missed from creating global hooks?

Any help is greatly appreciated.

dllmain.cpp file:

// dllmain.cpp : Defines the entry point for the DLL application.
#include "pch.h"

#include <stdio.h>
#include <ctime>

#pragma data_seg("Shared")
HHOOK hkKey = NULL;
HINSTANCE hInstHookDll = NULL;  //our global variable to store the instance of our DLL
#pragma data_seg() //end of our data segment

#pragma comment(linker,"/section:Shared,rws")    

__declspec(dllexport) LRESULT CALLBACK procCharMsg(int nCode, WPARAM wParam, LPARAM lParam)
//this is the hook procedure
{       
    MSG* msg;
    char charCode;
    if (nCode >= 0 && nCode == HC_ACTION)

    {           
        msg = (MSG*)lParam;
        if (msg->message == WM_CHAR)                
        {
            charCode = msg->wParam;
            if (IsCharLower(charCode))
                //we check if the character pressed is a small letter
            {
                //if so, make it to capital letter
                charCode -= 32;
                msg->wParam = (WPARAM)charCode;
                //overwrite the msg structure's wparam 
                //with our new value. 
            }
        }
    }
    return CallNextHookEx(NULL, nCode, wParam, lParam);
    //passing this message to target application
}

extern "C" __declspec(dllexport) void __stdcall SetHook()
 {
     if (hkKey == NULL)
         hkKey = SetWindowsHookEx(WH_GETMESSAGE, procCharMsg, hInstHookDll, 0); // initialize global hook
 }

 //remove the hook
extern "C" __declspec(dllexport) void __stdcall RemoveHook()
 {
     if (hkKey != NULL)
         UnhookWindowsHookEx(hkKey);
     hkKey = NULL;
 }

 INT APIENTRY DllMain(HMODULE hDLL, DWORD Reason, LPVOID Reserved) {
     switch (Reason)
     {
     case DLL_PROCESS_ATTACH:
         //we initialize our variable with the value that is passed to us
         hInstHookDll = (HINSTANCE)hDLL;             
         break;
     case DLL_PROCESS_DETACH:
         RemoveHook();
         break;
     default:
         break;          
     }    

     return TRUE;    
 }

main app function that calls the hook from dll file:

IntPtr hInstance = IntPtr.Zero;
IntPtr hProc = IntPtr.Zero;    
private delegate void HookSetting();

public void SetHook()
        {
            hInstance = LoadLibrary("Dll1");

            if (IntPtr.Zero == hInstance)
            {
                // null check
            }
            hProc = GetProcAddress(hInstance, "_SetHook@0");
            if(IntPtr.Zero == hProc)
            {
                // null check
            }
            HookSetting hookset = (HookSetting)Marshal.GetDelegateForFunctionPointer(hProc, typeof(HookSetting));
            hookset();                
        }

来源:https://stackoverflow.com/questions/57052505/global-hook-for-wh-getmessage-hooks-only-to-one-application-visualstudio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!