Windows Messages in Services

 
Hey. I need to get Windows messages in a service program.
Since services don't support OnChartEvent, I am trying to build something similar.
(Btw: Are there any plans to implement this?)

I installed a WH_GETMESSAGE hook procedure. Here is the callback (in DLL):

LRESULT CALLBACK GetMsgProc(int nCode, WPARAM wParam, LPARAM lParam)
{
   if (nCode < 0)  // do not process the message 
      return CallNextHookEx(hGetMsgHook, nCode, wParam, lParam);

   PMSG msg = (PMSG)lParam;
   PostThreadMessage(dwServiceThreadId, msg->message, 1111, 2222); // dwServiceThreadId is stored outside the procedure
   
   return CallNextHookEx(hGetMsgHook, nCode, wParam, lParam);
}
I am able to receive messages already, but the message data seems to be misaligned (see result at the end of this post).

Btw: I believe the definitions of the MSG structure (Include\WinAPI\winuser.mqh) and the POINT structure (Include\WinAPI\windef.mqh) are not correct.
The size of the MSG structure is 44 bytes, it should be 48 bytes (https://docs.microsoft.com/en-us/windows/win32/api/winuser/ns-winuser-msg).
The size of the POINT structure is 8 bytes, it should be 16 bytes (https://docs.microsoft.com/en-us/previous-versions/dd162805(v=vs.85)).

Here are the correct (?) ones:

struct MYPOINT
{
   long         x;
   long         y;
};
struct MYMSG
{
   long         hwnd;
   uint         message;
   uint         wParam;
   long         lParam;
   uint         time;
   MYPOINT      pt;
   uint         lPrivate;
};

Then I do (in MQL):

MYMSG msg;
int res = PeekMessageW(msg, NULL, 0, 0, PM_REMOVE);
PrintFormat(
   "hwnd: %d, "
   "message: %u, "
   "wParam: %u, "
   "lParam: %d, "
   "time: %u, "
   "pt.x: %d, "
   "pt.y: %d, "
   "lPrivate: %u",
   msg.hwnd,
   msg.message,
   msg.wParam,
   msg.lParam,
   msg.time,
   msg.pt.x,
   msg.pt.y,
   msg.lPrivate);

The result is (for WM_LBUTTONDOWN):

hwnd: 0, message: 513, wParam: 0, lParam: 1111, time: 2222, pt.x: 0, pt.y: 2222, lPrivate: 0
As you can see, the data is shifted and the test value for the lParam (2222) appears twice.
So, obviously there must be something wrong ...
I fiddled with redefining the structures for MSG and POINT together with #pragma pack(push, 1) on the c++ side without success ...

What am I missing?
Appreciate any help :)
MSG (winuser.h) - Win32 apps
MSG (winuser.h) - Win32 apps
  • 2018.12.05
  • jwmsft
  • docs.microsoft.com
Contains message information from a thread's message queue.