Is it possible to send a window handle with WM_COPYDATA?

668 Views Asked by At

I am trying to send an HWND with the WM_COPYDATA IPC method. So far when sending a string LPCTSTR it works.

LPCTSTR str = L"Test";
COPYDATASTRUCT cds;
cds.dwData = 20;
cds.cbData = sizeof(TCHAR) * wcslen(str);
cds.lpData = (PVOID)str;
LRESULT l = SendMessage(myhWnd, WM_COPYDATA, (WPARAM)nullptr, (LPARAM)&cds);

But when using the HWND the message doesn't even arrive...

COPYDATASTRUCT cds;
cds.dwData = 20;
cds.cbData = sizeof(HWND);
cds.lpData = (PVOID)targetWnd;
LRESULT l = SendMessage(myhWnd, WM_COPYDATA, (WPARAM)nullptr, (LPARAM)&cds);

A PVOID should be able to point at anything afaik.

My HWNDs are both set and both of the methods above return 0 as LRESULT. How do I debug this? Or is there something fundamentally wrong?

1

There are 1 best solutions below

1
On BEST ANSWER

An HWND is not a pointer. You most likely want:

COPYDATASTRUCT cds;
cds.dwData = 20;
cds.cbData = sizeof(HWND);
cds.lpData = &targetWnd;
//           ^
LRESULT l = SendMessage(myhWnd, WM_COPYDATA, (WPARAM)nullptr, (LPARAM)&cds);

Also, there seems to be some confusion between the source and destination HWNDs, but perhaps that's just the way you named them.

As Jonathan Potter (and some of the other commenters) point out, there are more efficient ways of sending an HWND, if that's all you want to do.