Win32 SendMessage WM_COPYDATA to another Window app

I need to send a text between a Xojo app and some legacy apps via a WM_COPYDATA message which uses a COPYDATASTRUCT to pass data.

typedef struct tagCOPYDATASTRUCT {
ULONG_PTR dwData;
DWORD cbData;
PVOID lpData;
} COPYDATASTRUCT, *PCOPYDATASTRUCT;

How to do that in Xojo ?

If you want a neater version that uses Structures instead of MemoryBlocks, let me know (tested in 32 & 64):

Const WM_COPYDATA = &h004A

Declare Function SendMessage Lib "User32.dll" Alias "SendMessageW" ( _
hWnd As Integer, _
Msg As UInt32, _
wParam As UInteger, _
lParam As Ptr _
) As Integer

'data to send over
Dim data As New MemoryBlock(4)
data.Int32Value(0) = 123

Dim cd As New MemoryBlock(COM.SIZEOF_PTR * 3)

Dim dwData As UInteger = 0
'Ptr used here as the size changes depending on the architecture,
'would be nice to have Integer/UInteger,
'not really needed if you are using <32bit values but done just in case
cd.Ptr(0) = Ptr(dwData)
cd.UInt32Value(COM.SIZEOF_PTR * 1) = data.Size
cd.Ptr(COM.SIZEOF_PTR * 2) = data

Dim targetWindowHandle As UInteger = &h201156
Dim ok As Integer 'a returned value from the target app after processing WM_COPYDATA
ok = SendMessage(targetWindowHandle, WM_COPYDATA, 0, cd)
system.DebugLog("ok=" + ok.ToString())

Hello Julian,
Thanks for the example.
COM.SIZEOF_PTR is not available in Xojo 2014 but I guess I can hard code that.
I have a FindWindow func to get the targetWindowHandle.
Can I use PostMessage or is SendMessage required for processing WM_COPYDATA ?
Best regards,
Alain

Yes you can substitute COM.SIZEOF_PTR for an #if that sets a variable to 4 or 8 depending on the architecture which is all that COM.SIZEOF_PTR is.

I’ve not tested it but I don’t see why you can’t use PostMessage if you don’t want your app to wait for the other app to process the message.

Let me know if you need any more pointers (pun intended) :wink:

All the best