From: mbramlage on
Hi all

I'm having a problem implementing a message forwarding scheme from a
global hook. What I want to do is detect mouse messages in a certain
window and forward those messages virutally unchanged (except perhaps
for POINT transforms) to another window.

The problem I'm having is using PostMessage to forward on the messages
to my main app for processing. I'm using a WH_GETMESSAGE hook (I want
to use the most general hook possible, so I can modify it at a future
date for more than mouse messages) so everything I want to pass to my
main app is in the lParam passed in. I've tried to just forward this
on, and I've tried to allocate a copy variable on the heap and
deallocate it later, but both methods result in failure. Here's some
code snippets:

In my DLL, the filter procedure:

static LRESULT CALLBACK MessageHookProc(int nCode, WPARAM wParam,
LPARAM lParam)
{
// If the value of nCode is < 0, just pass it on and return 0
if(nCode < 0) return CallNextHookEx(MessageHook, nCode, wParam,
lParam);

LPMSG msg = reinterpret_cast<LPMSG>(lParam);


if(msg->message == WM_MOUSEMOVE ||
msg->message == WM_NCMOUSEMOVE ||
msg->message == WM_LBUTTONDOWN ||
msg->message == WM_LBUTTONUP ||
msg->message == WM_RBUTTONDOWN ||
msg->message == WM_RBUTTONUP)
{

MSG* Messenger = new MSG();
Messenger->hwnd = reinterpret_cast<LPMSG>(lParam)->hwnd;
switch (msg->message)
{
case(WM_MOUSEMOVE):
PostMessage(hWndServer, WM_FORWARDMOUSEMOVE, 1, lParam/* OR
(LPARAM)Messenger*/);
break;
case(WM_NCMOUSEMOVE):
PostMessage(hWndServer, WM_FORWARDNCMOUSEMOVE, 3, lParam/* OR
(LPARAM)Messenger*/);
break;
}

return CallNextHookEx(MessageHook, nCode, wParam, lParam);

}

}

My server app simply processes the messages forwarded to it to send
them to the secondary window:

MSG* msg = reinterpret_cast<MSG*>(lParam);
HWND TargetWnd = msg->hwnd;

//continue to do cool stuff

Allocating a variable on the heap is an accepted way to pass messages
between processes; however, the pointer that comes to my lParam at the
recieving end is not the same as the sending end, defeating the point.
I think even allocating on the heap is not resulting in a universal
pointer because of managed memory. I'm totally flummoxed as to why just
forwarding lParam should fail, but it too is just fluff by the time I
get it.

I would really like to attach the info I need with the message, so that
I don't have to deal with shared memory mapped files and mutexes and
whatnot -- I can do that, but there MUST be a better way. Any
suggestions?

From: Cat on
mbramlage(a)gmail.com wrote:
> [...]
> Allocating a variable on the heap is an accepted way to pass messages
> between processes; [...]
It is not. In Win32, there is no longer such a thing as a "global"
heap. Heaps are associated with a specific process and are not shared
between them. In fact, each process has a different virtual address
space, which means that the same numerical pointer value can point to
entirely different things in each process. For example, the EXE of the
process is normally loaded at the address 0x400000; that same address
will contain different things in different processes.

> I would really like to attach the info I need with the message, so that
The easiest method I can think of is using WM_COPYDATA messages.
Windows automatically transfers the data itself, not just the pointer,
into the target window's process. This is referred to as "marshalling".

Also, you should not allocate memory on the heap (new MSG()) without
freeing it later on. In fact, in this specific case, you don't even
need to allocate any memory. You can simply pass the MSG structure
received by your hook as the data block that Windows should transfer
into the other process.

-Cat

From: Lindsay on
Don't pass the structure. Just post the HWND if that's all you need. Makes
more sense.