GetMessage Hook

Jun 30, 2013 at 5:33 PM
Hello,

I’m trying to hook GetMessage, to get WM_WINDOWPOSCHANGED. I’ve tried hooking SendMessage/PostMessage and PeekMessage but they all get different messages than the one I want. The hook is working and I’m getting a lot of messages in, but I don’t know how to filter them.

CreateFileHook = LocalHook.Create(LocalHook.GetProcAddress("user32.dll", "GetMessageW"), new DGetMessage(GetMessage_Hooked), this);
    [UnmanagedFunctionPointer(CallingConvention.StdCall, CharSet = CharSet.Unicode, SetLastError = true)]
    delegate void DGetMessage(out NativeMessage lpMsg, IntPtr hWnd, uint wMsgFilterMin, uint wMsgFilterMax);

    static void GetMessage_Hooked(out NativeMessage lpMsg, IntPtr hWnd, uint wMsgFilterMin, uint wMsgFilterMax)
    {
        Interface.Print("GetMessage");

        // call original API...
        GetMessageW(out lpMsg, hWnd, wMsgFilterMin, wMsgFilterMax);
    }

    [DllImport("user32.dll", CharSet = CharSet.Unicode)]
    static extern sbyte GetMessageW(out NativeMessage lpMsg, IntPtr hWnd, uint wMsgFilterMin, uint wMsgFilterMax);

So the lpMsg is of type NativeMessage which has the uint msg I need to filter on. Because it’s an out parameter the variable only gets initialized in GetMessageW (I think...). So where do I check what message actually got sent? I’ve tried printing the message after calling the original API method but then my hooked process crashes.

Any help would be appreciated. Thanks,
Vincent