0

I'm trying to create a function that detects software signals within a game. However, my thread is a separate thread from the UI thread and should not affect the UI thread.

I've tested it by creating a message-only window and calling getmessage on my thread, I receive messages fine until I click on a game, but when I click on a game, all messages are not received.

My test code is below.

mythread.cpp

    HINSTANCE instance = GetModuleHandle(nullptr);

    WNDCLASSEX wndclass;
    wndclass.cbSize = sizeof(WNDCLASSEX);
    wndclass.lpfnWndProc = myproc;
    wndclass.hInstance = instance;
    wndclass.lpszClassName = "test";

    DWORD ret = RegisterClassEx(&wndclass);
    HWND hwnd = CreateWindowEx(0, "test", "dummy", 0, 0, 0, 0, 0, HWND_MESSAGE, NULL, instance, 0);

    RAWINPUTDEVICE device = { 0 };

    devices.usUsagePage = 1;
    devices.usUsage = 2;
    devices.dwFlags = RIDEV_INPUTSINK;
    devices.hwndTarget = hwnd;

    RegisterRawInputDevices(devices, 1, sizeof(RAWINPUTDEVICE));

    MSG msg;
    while(true)
    {
        while (GetMessage(&msg, hwnd, 0, 0))
        {
            TranslateMessage(&msg);
            DispatchMessageW(&msg);
        }
    }

myproc

LRESULT CALLBACK myproc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
    switch (msg)
    {
        case WM_INPUT:
        {
            ...
        }
    }
    return DefWindowProc(hwnd, msg, wparam, lparam);
}

Can you please let me know what I did wrong?

drescherjm
  • 10,365
  • 5
  • 44
  • 64
i like cat
  • 135
  • 8
  • Messages are delivered to the thread which owns the window to which the message was sent/posted. – David Heffernan Jul 17 '23 at 15:16
  • I created a window in my thread. How do I know if the window is owned by my thread? – i like cat Jul 17 '23 at 15:41
  • Your window *is* owned by your thread. But that doesn't guarantee that is the window which messages are being sent to. In this case, you are registering the window with the RawInput API, so it *should* receive all RawInput messages, especially since you are using the `RIDEV_INPUTSINK` flag. If that is not working for you, then something else is going on to prevent the messages from reaching your window. For instance, make sure `RegisterClassEx()` and `CreateWindowEx()` are actually successful, as there is no error handling in the code you have shown. – Remy Lebeau Jul 17 '23 at 20:33
  • Windows have affinity with the thread in which they are created. – David Heffernan Jul 17 '23 at 22:12
  • The call to `GetMessage` is filtering messages by window. You should probably be passing `nullptr` here so that your thread can also receive thread messages (or messages posted to a different window owned by this thread). Might want to read [The dangers of filtering window messages](https://devblogs.microsoft.com/oldnewthing/20050209-00/?p=36493). – IInspectable Aug 06 '23 at 16:41

1 Answers1

0

You cannot do that, all your window message processing has to be on the thread that created the window. You can pass messages from other threads (or processes) to it using PostMessage, but not the other way around.

I'm not really sure why you want to receive your messages on another thread anyway, it sounds like what you're looking for is the producer-consumer pattern, where your gui thread is producing events into a collection of some kind (a vector synchronized using a critical section that triggers an event wait handle, for example), and your other thread consumes them as they come.

Blindy
  • 65,249
  • 10
  • 91
  • 131
  • I created a message procedure (myproc) and a getmessage routine in my thread, what's the problem? – i like cat Jul 17 '23 at 15:42
  • The problem is that that is not how windows work in Windows. One thread per application is the main gui thread, anything else doesn't touch them or anything related to them. – Blindy Jul 17 '23 at 18:33
  • 5
    @Blindy *By convention only*, most apps have 1 single UI thread. But the Win32 API does not enforce this. *ANY* thread can have a message queue and create its own UI windows. The API has no concept of a "main GUI thread". That is an artificial limitation of most UI frameworks, not the OS itself. – Remy Lebeau Jul 17 '23 at 20:30
  • Nothing in this proposed answer even touches on the described issue: Why does a thread stop receiving *global* Raw Input messages when input focus changes. None of this is helpful. – IInspectable Aug 06 '23 at 16:39