吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 837|回复: 0
收起左侧

[求助] WindowsHook怎么才能检测到其他的窗口?

[复制链接]
真的小白 发表于 2022-5-25 14:15
以下是Dll代码,想用来获取正在移动的窗口,无论是 SetWindowsHookExW 还是 SetWinEventHook ,最后获取到的窗口总是调用其中两个 Start() 方法的窗口,获取不到其他正在移动的窗口。
恳求各位大佬指点一二

[C++] 纯文本查看 复制代码
// dllmain.cpp : 定义 DLL 应用程序的入口点。
#include "pch.h"

static HINSTANCE DllHandle = nullptr;
static HHOOK HookHandle = nullptr;
static HWND HostHandle = nullptr;
static HWINEVENTHOOK EventHookHandle = nullptr;
LPCWPSTRUCT CWPStruct = nullptr;

BOOL APIENTRY DllMain(HMODULE hModule, DWORD  ul_reason_for_call, LPVOID lpReserved)
{
    switch (ul_reason_for_call)
    {
        case DLL_PROCESS_ATTACH:
            DllHandle = hModule;
            break;
        case DLL_THREAD_ATTACH:
        case DLL_THREAD_DETACH:
        case DLL_PROCESS_DETACH:
            break;
    }
    return TRUE;
}

void DoEvent(HWND hWnd)
{
    if (IsWindow(hWnd))
    {
        if ((GetWindowLongPtrW(hWnd, GWL_STYLE) & WS_CHILD) != WS_CHILD)
        {
            int length = GetWindowTextLengthW(hWnd) + 1;
            WCHAR title[MAX_PATH];
            GetWindowTextW(hWnd, title, length);
            SendMessageW(HostHandle, WM_USER + 9000, (WPARAM)length, (LPARAM)title);
        }
    }
}

void Wineventproc(HWINEVENTHOOK hWinEventHook, DWORD event, HWND hwnd, LONG idObject, LONG idChild, DWORD idEventThread, DWORD dwmsEventTime)
{
    if (hWinEventHook == EventHookHandle)
    {
        if (event == EVENT_OBJECT_LOCATIONCHANGE)
        {
            DoEvent(hwnd);
        }
    }
}

extern "C"
{
    __declspec(dllexport) LRESULT CALLBACK CallWndProc(int nCode, WPARAM wParam, LPARAM lParam)
    {
        if (nCode >= HC_ACTION)
        {
            if (wParam != 0)
            {
                CWPStruct = (LPCWPSTRUCT)lParam;
                if (CWPStruct->message == WM_MOVING)
                {
                    DoEvent(CWPStruct->hwnd);
                }
            }
        }

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

    __declspec(dllexport) void Start(HWND hHost)
    {
        HostHandle = hHost;
        HookHandle = SetWindowsHookExW(WH_CALLWNDPROC, CallWndProc, DllHandle, 0);
    }

    __declspec(dllexport) void Stop()
    {
        UnhookWindowsHookEx(HookHandle);
    }

    __declspec(dllexport) void Start2(HWND hHost)
    {
        HostHandle = hHost;
        EventHookHandle = SetWinEventHook(EVENT_OBJECT_LOCATIONCHANGE, EVENT_OBJECT_LOCATIONCHANGE, DllHandle, Wineventproc, 0, 0, WINEVENT_INCONTEXT | WINEVENT_SKIPOWNPROCESS);
    }

    __declspec(dllexport) void Stop2()
    {
        UnhookWinEvent(EventHookHandle);
    }
}

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-25 11:30

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表