What WINAPI could be hooked to prevent chrome from being activated?

Viewed 135

Im using EasyHook to hook google chrome, trying to prevent the window from being activated when sending clicks to it using PostMessage, i hooked WM_WINDOWPOSCHANGING and 'forced' it to remove the flags SWP_NOACTIVATE | SWP_NOZORDER, but when i send the PostMessage click:

lParam = MAKELPARAM(x, y);
PostMessageW(hWnd, WM_LBUTTONDOWN, 0x0001, lParam);
PostMessageW(hWnd, WM_LBUTTONUP, 0, lParam);

the window still get activated, i confirmed that the hook is working correctly, the problem is just finding the correct WINAPI to hook/intercept.

    typedef LRESULT __stdcall DefDlgProcWFunc(HWND, UINT, WPARAM, LPARAM);
    
    // Innermost hook handler
    LRESULT __stdcall DefDlgProcW_Hook(HWND   hWnd, UINT   Msg, WPARAM wParam, LPARAM lParam)
    {
        DefWindowProcW_Msg(hWnd, Msg, wParam, lParam, 1);
    
        // Innermost hook just call original
        return DefDlgProcW(hWnd, Msg, wParam, lParam);
    }
    
    
    
    void DefWindowProcW_Msg(HWND hWnd, UINT& Msg, WPARAM& wParam, LPARAM& lParam, int CalledByFunction)
    {
        if (!hooks_enabled)
            return;
    
        std::wstringstream str;

        HWND hwnd = nullptr;
        HWND hwndInsertAfter = nullptr;
        tagWINDOWPOS* wp;
        int x;
        int y;
        int cx;
        int cy;
        UINT flags;
    
        switch (Msg)
        {
            case WM_WINDOWPOSCHANGING:
            {
                wp = reinterpret_cast<tagWINDOWPOS*>(lParam);
                hwnd = wp->hwnd;
                hwndInsertAfter = wp->hwndInsertAfter;
                x = wp->x;
                y = wp->y;
                cx = wp->cx;
                cy = wp->cy;
                flags = wp->flags;
    
    
                HWND hWnd2 = FindWindow(0, L"New Tab - Google Chrome");
    
                str << L"\nWM_WINDOWPOSCHANGING"
                    << L"\nx:                   " << x  << L"        y:        " << y
                    << L"\nw:                   " << cx << L"        h:        " << cy
                    << L"\nhwnd:                " << hwnd
                    << L"\nflags:               " << flags
                    << L"\n";
                OutputDebugString(str.str().c_str());
    
                if (hWnd == hWnd2) {
                    OutputDebugString(L"CHROME!!!!\n");
                    wp->flags &= ~(SWP_NOACTIVATE | SWP_NOZORDER);
                    return;
                }
    
                return;
            }
        }
    }

This works:

Is it possible to prevent an application from being activated (brought to front) without using DLL injection?

but the cons is: you always need to call LockSetForegroundWindow before and after sending the message (to unlock), if i try to perform a click in the taskbar while it are locked no window is activated.

In the link there's a mention to SetWinEventHook with the event EVENT_SYSTEM_FOREGROUND it would not help as when the event is returned the window already changed.

I also tried hooking SetForegroundWindow, but looks like chrome is not calling it.

This is all window message triggered after sending a PostMessage click: https://i.imgur.com/04dKTVb.png

My goal is to get it working using PostMessage, and not UI Automation/JavaScript.

0 Answers
Related