VC ++:如何使用Mouse Hook获取firefox浏览器的Window Handle?

时间:2015-07-09 19:39:07

标签: c++ firefox visual-c++ hook mouse-hook

DLL中的代码:

extern "C" __declspec(dllexport) bool install()
{

    hHookcbt = SetWindowsHookEx(WH_CBT, (HOOKPROC)CBTProc, hinst, 0);
    hook = SetWindowsHookEx(WH_MOUSE, (HOOKPROC)MouseProc, hinst, 0);
    khook = SetWindowsHookEx(WH_KEYBOARD, (HOOKPROC)KeyboardProc, hinst, 0);
    return TRUE;
}
LRESULT CALLBACK MouseProc(int nCode, WPARAM wParam, LPARAM lParam)
{
    if (nCode < 0)
    {
        return CallNextHookEx(hook, nCode, wParam, lParam);
    }

    //char c = MapVirtualKey(wParam,MAPVK_VK_TO_CHAR);

    switch (wParam)
    {
    case WM_LBUTTONUP:
        SendMessage(whandle, 9, wParam, lParam);
    break;
    case WM_RBUTTONUP:
        SendMessage(whandle, 0, wParam, lParam);
        break;
    default:
        break;
    }


    return CallNextHookEx(NULL, nCode, wParam, lParam);
}

EXDE中的代码:使用鼠标结构来获取鼠标单击窗口的窗口句柄,它适用于所有默认应用程序,例如:记事本,便签,但不适用于firefox,taskmanager等。

// SETTING THIS WINDOW HANDLE TO THE DLL
        swh = (SetWindowHandle)GetProcAddress(hinst, "setwindowhandle");
        if (!swh(hWnd))
        {
            printf("\nDLL FAILED TO ADDED THIS WINDOW HANDLE");
        }
        else{
            printf("\nDLL ADDED THIS WINDOW HANDLE");
        }
case 9:

        mhs = (MOUSEHOOKSTRUCT*)lParam;
        cout <<endl<< "x=" << mhs->pt.x<<" y="<<mhs->pt.y;
        SelWinH = mhs->hwnd;

        cout << endl << "Selected window process id=" << GetProcessId(SelWinH);
        tid = GetWindowThreadProcessId(mhs->hwnd, &pid);
        cout << endl << "tid =" << tid << "   " << "pid = " << pid;
        cout << endl << "---------------------------------------------------------------------------------";

        break;

这里SelWinH存储鼠标选择窗口的WHND。但是当我点击firefox浏览器时它没有响应。

1 个答案:

答案 0 :(得分:-1)

点击鼠标后调用GetForegroundWindow()怎么样?它会将句柄返回到您单击的窗口。