VB6 SendMessage发送自定义数据类型
我目前正在成功使用 Win32 API 的 SendMessage 函数使用 WM_SETTEXT 参数在两个线程之间发送文本。
我想做的是发送自定义数据类型而不是原始数据类型。
因此,假设我
Type myType
a as Integer
b(5) as Boolean
d(15) as Double
End Type
Dim tmp as myType
希望能够:
Call SendMessage(dstHWnd, WM_SETTEXT, 0, tmp)
我猜我必须使用 WM_COPYDATA 或类似的,但另一个问题是这会产生错误,因为我的数据类型无法根据函数定义转换为 Any:
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
是吗?可以诱导这种转变吗?或者是否有替代的最佳实践方法(快速且最佳)?
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)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将
SendMessage
的最后一个参数声明为byref lParam as myType
。然而,您正在滥用消息传递系统。只有当您知道自己在做什么并且确定系统默认处理逻辑不会应用于该消息时才可以。
澄清一下,在接收端,您将执行以下操作来获取数据。
首先,声明消息处理例程,最后一个参数为
ByVal lParam As Long
。还有一个功能:然后,当您收到消息时:
进一步澄清一点。
因为所有线程都在一个进程内,所以您可以简单地共享指针并通过
WM_COPYDATA
来实现。您只需要COPYDATASTRUCT
结构。在发送端,您设置
COPYDATASTRUCT.dwData = VarPrt(your_struct)
。在接收端,您执行上面所示的相同的
CopyMemory
操作。请注意,如果您的消息处理例程仅接收该单个消息(而不接收其他消息),那么您可以简单地将其最后一个参数声明为
ByRef lParam As myType
并直接使用它,从而避免复制。Declare the last parameter of
SendMessage
asbyref 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 theCOPYDATASTRUCT
structure.On the sending end, you set
COPYDATASTRUCT.dwData = VarPrt(your_struct)
.On the receiving end, you do that same
CopyMemory
thing 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 myType
and use it directly, avoiding the copying.使用内存映射文件怎么样?
How about using a Memory Mapped File?
如果它们是两个线程(每个线程都有自己的窗口?在 VB6 中?嗯),那么您只需发送一个指向变量
VarPtr(blah)
的指针,并确保在窗口过程中复制出来返回。但是,如果线程位于两个独立的进程中,那么您的选择就会少得多。
您可以使用 WM_COPYDATA 消息来为您进行编组,或者设置一些共享/全局内存并通过普通的 SendMessage() 传递指针/偏移量
普通线程同步实践适用于后一种方法。
If they are two threads (each with their own window? in VB6? hmm) then you just need to send a pointer to the variable
VarPtr(blah)
and make sure you copy out in the window procedure before returning.If however, the threads are in two separate processes, you have far fewer options.
You can either use the
WM_COPYDATA
message which does the marshalling for you, or setup some shared/global memory and pass pointer/offsets via a normalSendMessage()
Normal thread sync practices apply to the latter methods.