Dll钩子获取某个窗口的消息:
当该钩子为全局钩子的时候,可以捕获到鼠标的消息;
当该钩子指向某个窗口的创立线程的时候,则捕获不到该窗口的消息;
哪位有时间帮忙看看,不胜感激!//DLL 中代码
#pragma data_seg("Shared")
HHOOK h_hook = NULL;
HINSTANCE h_dll = NULL;
DWORD dw_TargetThreadid = 0;
#pragma data_seg()
#pragma comment(linker,"/section:Shared,RWS")
BOOL WINAPI DllMain(HINSTANCE hinstDll,DWORD fdwReason,LPVOID lpvReserved)
{
switch(fdwReason)
{
case DLL_PROCESS_ATTACH:
{
    h_dll = hinstDll;
}
    break;
}

return TRUE;
}LRESULT CALLBACK GetMsgProc(int nCode,WPARAM wParam,LPARAM iParam)
{
    return 1;
}void SetHook(HWND hwnd)//hwnd为传入的窗口句柄,此函数在一个应用程序中调用
{
     dw_TargetThreadid = GetWindowThreadProcessId(h_wnd,NULL);
     h_hook = SetWindowsHookEx(WH_CALLWNDPROC,GetMsgProc,h_dll,dw_TargetThreadid);}

解决方案 »

  1.   

    如果 
    h_hook = SetWindowsHookEx(WH_CALLWNDPROC,GetMsgProc,h_dll,dw_TargetThreadid);修改为
    h_hook = SetWindowsHookEx(WH_CALLWNDPROC,GetMsgProc,h_dll,0);

    GetMsgProc可以被调用,
      

  2.   

    w_TargetThreadid = GetWindowThreadProcessId(h_wnd,NULL);
    换成GetWindowThreadProcessId(h_wnd,&w_TargetThreadid);
    试试
      

  3.   

    钩子类型用WH_GETMESSAGE 以下代码测试过。//DLL 中代码
    #pragma data_seg("Shared")
    HHOOK h_hook = NULL;
    HINSTANCE h_dll = NULL;
    DWORD dw_TargetThreadid = 0;
    #pragma data_seg()
    #pragma comment(linker,"/section:Shared,RWS")
    BOOL WINAPI DllMain(HINSTANCE hinstDll,DWORD fdwReason,LPVOID lpvReserved)
    {
    switch(fdwReason)
    {
    case DLL_PROCESS_ATTACH:
    {
    h_dll = hinstDll;
    }
    break; 
    }

    return TRUE;
    }LRESULT CALLBACK GetMsgProc(int nCode,WPARAM wParam, LPARAM lParam)

    if (nCode<0 )
    return CallNextHookEx(h_hook,nCode,wParam,lParam); //传递钩子信息  switch (  ((MSG  *)lParam)->message  )  
    {
    case WM_RBUTTONDOWN: 
    {
    ::MessageBox(NULL,TEXT("提示 Error"),NULL,MB_OK);
    }
    break;
    }
    return CallNextHookEx(h_hook,nCode,wParam,lParam); //传递钩子信息   

    extern "C" _declspec(dllexport) void SetHook(HWND hwnd)//hwnd为传入的窗口句柄,此函数在一个应用程序中调用
    { dw_TargetThreadid = GetWindowThreadProcessId(hwnd,NULL);
    h_hook = SetWindowsHookEx(WH_GETMESSAGE,GetMsgProc,h_dll,dw_TargetThreadid);

    }
      

  4.   

    void SetHook(HWND hwnd)//hwnd为传入的窗口句柄,此函数在一个应用程序中调用
    {
      dw_TargetThreadid = GetWindowThreadProcessId(h_wnd,NULL);
      h_hook = SetWindowsHookEx(WH_CALLWNDPROC,GetMsgProc,h_dll,dw_TargetThreadid);}红色标注的变量,笔误?
      

  5.   

    GetWindowThreadProcessId(h_wnd,NULL);用来得到创建该窗口的线程的ID
    GetWindowThreadProcessId(h_wnd,&amp;w_TargetThreadid);用来得到该窗口的所在进程的ID如果理解的不错的话,这个应该没有错误
      

  6.   

    jacky_qiu:非常感谢你的回复,
    LRESULT CALLBACK GetMsgProc(int nCode,WPARAM wParam, LPARAM lParam)
    {}此函数进入与否,不应该用断点来判断,因为一旦注入DLL成功,此时它已经在其他的应用程序空间里了。
    只能获取相应的消息框来判断。