Detect / Hook Window Move/Drag of other external p

2020-02-29 07:37发布

What is the best way to Detect Window Move/Drag of other Processes? In Windows7 64-bit

I'm currently investigating Global Hooks from a DLL using C++ & C#. It's a pain as it doesn't want to work properly. I've gotten some success with keyboard and mouse hooks. but for window messages I've just got no idea whats wrong.

this is the code in my .dll file

#include <windows.h>
#include <iostream>
#include <stdio.h>

HINSTANCE hinst;
#pragma data_seg(".shared")
HHOOK hhk;
WNDPROC realProc;
#pragma data_seg()
//#pragma comment(linker, "/SECTION:.shared,RWS") compiler error in VC++ 2008 express

LRESULT CALLBACK wireKeyboardProc(int code, WPARAM wParam,LPARAM lParam) {  
    //open the standard out stream for writing
    freopen("CONOUT$","w+t", stdout);
    printf("code:%d wparam:%d lparam:%d\n", code, wParam, lParam);

    /*
    if (code < 0) {
        return CallNextHookEx(0, code, wParam, lParam);
    }
    */
    //Beep(1000, 20);

    return CallNextHookEx(hhk, code, wParam, lParam);
}

LRESULT CALLBACK hookProc(HWND h, UINT msg, WPARAM wp, LPARAM lp)
{
    //open the standard out stream for writing
    freopen("CONOUT$","w+t", stdout);
    printf("h:%d msg:%d wp:%d lp:%d\n", h, msg, wp, lp);
    return CallWindowProc(realProc, h, msg, wp, lp);
}

extern "C" __declspec(dllexport) void install(unsigned long threadId, HWND hwnd) {
    //open the standard out stream for writing
    freopen("CONOUT$","w+t", stdout);

    //works for WH_KEYBOARD WH_MOUSE but doesnt work for WH_CALLWNDPROC
    hhk = SetWindowsHookEx(WH_CALLWNDPROC, wireKeyboardProc, hinst, threadId);
    printf("threadId: %d xxx: %d\n", threadId, hhk);

    /*
    //dont know whats wrong the return value of realProc is 0
    realProc = (WNDPROC)SetWindowLongPtr(hwnd, GWL_WNDPROC, (LONG_PTR)hookProc);
    printf("hwnd: %d xxx: %d\n", hwnd, realProc);
    */
}

extern "C" __declspec(dllexport) void uninstall() {
    UnhookWindowsHookEx(hhk); 
}

BOOL WINAPI DllMain(__in HINSTANCE hinstDLL, __in  DWORD fdwReason, __in  LPVOID lpvReserved) {
    hinst = hinstDLL;
    return TRUE;
}

I was thinking of making my own Aero Snap. This is just for fun.

Thanks for any help.

1条回答
Deceive 欺骗
2楼-- · 2020-02-29 08:19

After some extra googling i found a open source project that does almost exactly what i want.

http://sourceforge.net/projects/powerresizer/

it compiles easy without errors too. it shows in the code that it uses

SetWinEventHook(EVENT_SYSTEM_MOVESIZESTART, EVENT_SYSTEM_MOVESIZEEND,

and a custom dll for the hook procedure. it also shows some other tricks. never seen SetWinEventHook anywhere else. upvote if you learned something.

damn of course it bugs with some windows too.

查看更多
登录 后发表回答