I’m currently successfully using Win32 API’s SendMessage function to send text between two threads using the WM_SETTEXT parameter.
What I would like to do is send a custom data type instead of primitive data types.
So let’s say I have
Type myType
a as Integer
b(5) as Boolean
d(15) as Double
End Type
Dim tmp as myType
I would like to be able to:
Call SendMessage(dstHWnd, WM_SETTEXT, 0, tmp)
I am guessing I would have to use WM_COPYDATA or similar, but the other issue is this produces a error because my data type cannot be casted into Any, per function definition:
Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Integer, ByVal lParam As Any) As Long
Is it possible to coax this conversion? Or is there an alternative best-practices method (fast and optimal)?
Declare the last parameter of
SendMessageasbyref lParam as myType.You are, however, abusing the messaging system. It’s only fine while you know what you’re doing and you’re sure no system-default procesing logic will ever be applied to that message.
To clarify, on the receiving end, you’re doing the following to get the data.
First, you declare your message processing routine with the last parameter being
ByVal lParam As Long. Also have a function:Then, when you receive a message:
To clarify a bit further.
Because all threads are inside one process, you can simply share pointers and do that by the mean of
WM_COPYDATA. You’ll only need the first member of theCOPYDATASTRUCTstructure.On the sending end, you set
COPYDATASTRUCT.dwData = VarPrt(your_struct).On the receiving end, you do that same
CopyMemorything shown above.Note that if your message processing routine is only going to receive that single message (and no other messages), then you can simply declare its last parameter as
ByRef lParam As myTypeand use it directly, avoiding the copying.