vencon_s 2015-07-13 03:19 采纳率: 75%
浏览 5066
已采纳

c++ WH_GETMESSAGE 钩子 ,勾住程序,但是回调函数没执行,请教

需要拦截目标程序的显示和关闭窗口消息。照搬网上的代码。显示注入DLL成功。但是回调函数并没有执行。请教


#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
HHOOK hooker;
HWND notepadhandle;
BOOL handled;
extern "C" __declspec(dllexport) LRESULT CALLBACK HookProc(int nCode,WPARAM wParam,LPARAM lParam);
char* ConvertInttoChar(int i);
BOOL APIENTRY DllMain(HANDLE hModule,DWORD ul_reason_for_call,LPVOID lpReserved)
{
    if(ul_reason_for_call==DLL_PROCESS_ATTACH)
    {
        handled=FALSE;
        notepadhandle=FindWindow(TEXT("Notepad"),NULL);

        if(notepadhandle==NULL)
        {
            printf("Notepad Not Found.\n");
            return TRUE;
        }
        hooker=SetWindowsHookEx(WH_GETMESSAGE,HookProc,(HINSTANCE)hModule,GetWindowThreadProcessId(notepadhandle,NULL));
        if(hooker)
        {
            OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE,(DWORD)notepadhandle);
            printf("hookpro:%d\n",HookProc);
            printf("hmodule:%d\n",hModule);
            printf("pid:%d\n",notepadhandle);
            printf("Hook Successfully.\nHookID:%d\n",hooker);
        }
        else
        {
            printf("Hook Failed.\nError:%d\n",GetLastError());
            return TRUE;
        }
        //PostMessage(notepadhandle,WM_CLOSE,0,0);
        //printf("发送关闭指令");

    }

    if(ul_reason_for_call==DLL_PROCESS_DETACH)
    {
        UnhookWindowsHookEx(hooker);
        return TRUE;
    }   
    return TRUE;
}
extern "C" __declspec(dllexport) LRESULT CALLBACK HookProc(int nCode,WPARAM wParam,LPARAM lParam)
{
            printf("NCOD111");
    if(nCode<0)
    {
        printf("NCOD<0");
        return CallNextHookEx(hooker,nCode,wParam,lParam);
    }
    tagMSG* msg;
    msg=(tagMSG*)lParam;
    if(nCode==HC_ACTION && (msg->message==WM_CLOSE))
    {
        if(handled==FALSE)
            printf("拦截到消息:%d\n",msg->message);
        handled=TRUE;
        UnhookWindowsHookEx(hooker);
        msg->message=WM_NULL;

        return CallNextHookEx(hooker,nCode,wParam,(LPARAM)msg);
    }
            printf("LAST");
    return CallNextHookEx(hooker,nCode,wParam,lParam);
}

执行的结果,DLLmain的代码能正常执行,并显示注入成功;但是回调函数中的打印代码均没执行,请教。
执行结果
hookpro:1666846815
hmodule:1666777088
pid:4063406
Hook Successfully.
HookID:17567643

  • 写回答

3条回答 默认 最新

  • oyljerry 2015-07-13 03:29
    关注

    你应该用全局钩子,第四个参数用0

    dwThreadId [in]
    Type: DWORD
    The identifier of the thread with which the hook procedure is to be associated. For desktop apps, if this parameter is zero, the hook procedure is associated with all existing threads running in the same desktop as the calling thread. For Windows Store apps, see the Remarks section.

    https://msdn.microsoft.com/en-us/library/ms644990(VS.85).aspx

    hooker=SetWindowsHookEx(WH_GETMESSAGE,HookProc,(HINSTANCE)hModule,0);
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?