Skip to main content
GameDev.net gamedev.net
🔒 Locked

Problems with creating a messaging 'hook'

Started by DividedByZero Jan 26, 2020 at 7:23 AM 1 replies 4.5k views
Original Post
DividedByZero
DividedByZero

Hi Guys,

I am trying to catch a 'dragged file' windows message via a DLL I am creating (for a third party engine).

I have the DLL currently talking with the engine correctly and it now ‘accepts files’ that are dragged onto the window, as it now displays the + symbol when a file is dragged on the window, which it didn't before and shows the return code of ‘998’.

I am new to Windows hooks though, so am not 100% sure I am going about this the right way.

It appears that my hook is also working because when I try to catch the HCBT_SYSCOMMAND message, it is displaying the message box, whenever I attempt to maximise etc…

But I can't get it to respond to a WM_DROPFILES message no matter what I try.

This is where my entire source is at present. Any advice would be greatly appreciated.

#include <Windows.h>
#include <iostream>

#define fn_export extern "C" __declspec (dllexport)

HHOOK hook_handle = NULL;


LRESULT CALLBACK DragFilesProc(int nCode, WPARAM wParam, LPARAM lParam) {
    if (nCode < HC_ACTION)
        return CallNextHookEx(hook_handle, nCode, wParam, lParam);
    
    // This works as it is catching window resize etc...
    /*
    if (nCode == HCBT_SYSCOMMAND)
    {
        MessageBox(0, L"", L"", 0);
    }
    */

    if(wParam == WM_DROPFILES)
    {
        MessageBox(0, L"", L"", 0);
        // Can't get this to fire.
    }
    
    return CallNextHookEx(hook_handle, nCode, wParam, lParam);
}


fn_export double test(HWND hWnd)
{
    // Make the window 'accept files'
    SetWindowLongPtr(hWnd, GWL_EXSTYLE, GetWindowLongA(hWnd, GWL_EXSTYLE) | WS_EX_ACCEPTFILES);
    hook_handle = SetWindowsHookEx(WH_CBT, &DragFilesProc, GetModuleHandle(NULL), GetCurrentThreadId());

    // Alternate method
    DragAcceptFiles(hWnd, TRUE);

    return 998;
}
Shaarigan
Shaarigan

Sometimes there are messages send to windows only, not the process or any hooks so it is required to create the window already handling some of those messages by it's own. I use a message loop like this

WNDCLASSEXA wc;
wc.lpfnWndProc = WndProc;

RegisterClassExA(&wc);
…

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch(message)
    {
        case WM_CLOSE:
        case WM_DESTROY:
        case WM_SIZE:
        {
            PostMessageA(0, message, wParam, lParam);
        }
        return 0;
        default: return DefWindowProcA(hWnd, message, wParam, lParam);
    }
}

in my windows to post certain messages to the main thread. Not just using it for resize and close messages but some messages not delivered to the process I need here are for example WM_DEVICECHANGE for UPnP support. Maybe your WM_DROPFILES message belongs to the same category

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.