VB6之WM_COPYDATA

WM_COPYDATA消息是一种进程间通信的一种方式,参考文档如下:

http://msdn.microsoft.com/en-us/library/windows/desktop/ms649011(v=vs.85).aspx

http://www.flounder.com/wm_copydata.htm

http://support.microsoft.com/kb/176058

直接上代码,接收端:

 '模块部分
'code by lichmama from cnblogs.com
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, _
Source As Any, ByVal Length As Long)
Public Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, _
ByVal nIndex As Long) As Long
Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, _
ByVal nIndex As Long, _
ByVal dwNewLong As Long) As Long
Private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, _
ByVal hWnd As Long, _
ByVal Msg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long Private Const WM_COPYDATA = &H4A
Public Const GWL_WNDPROC = (-)
Private Type COPYDATASTRUCT
dwData As Long '数据标识
cbData As Long '数据长度
lpData As Long '数据地址
End Type
Public lpPrevWndFunc As Long Public Function CallbackWndProc(ByVal hWnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long If wMsg = WM_COPYDATA Then
Dim lpCDS As COPYDATASTRUCT
Dim buff() As Byte Call CopyMemory(lpCDS, ByVal lParam, Len(lpCDS))
ReDim buff(lpCDS.cbData) As Byte
Call CopyMemory(buff(), ByVal lpCDS.lpData, lpCDS.cbData)
Debug.Print "#WM_COPYDATA", "dwData:" & lpCDS.dwData, "cbData:" & lpCDS.cbData, "lpData:" & Left(buff, lpCDS.cbData)
Erase buff
End If
CallbackWndProc = CallWindowProc(lpPrevWndFunc, hWnd, wMsg, wParam, lParam)
End Function
 '窗体部分
'code by lichmama from cnblogs.com
Private Sub Form_Load()
lpPrevWndFunc = GetWindowLong(Me.hWnd, GWL_WNDPROC)
Call SetWindowLong(Me.hWnd, GWL_WNDPROC, AddressOf CallbackWndProc)
Me.Hide
End Sub Private Sub Form_Unload(Cancel As Integer)
Call SetWindowLong(Me.hWnd, GWL_WNDPROC, lpPrevWndFunc)
End Sub

接下来是,发送端:

 'code by lichmama from cnblogs.com
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
lParam As Any) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long Private Const WM_COPYDATA = &H4A
Private Type COPYDATASTRUCT
dwData As Long '数据标识
cbData As Long '数据长度
lpData As Long '数据地址
End Type Private Sub Command1_Click()
Dim hWnd As Long
Dim lpCDS As COPYDATASTRUCT
Dim buff() As Byte
Dim msgId As Long hWnd = FindWindow(vbNullString, "Receiver")
buff = "你好,我是lichmama。"
'在COPYDATASTRUCT结构中,dwData是[接收端]用来区分不同数据用的。
' 因为,正常的数据交互中,程序要根据不同的数据做不同处理。
' 所以,就需要一个dwData这样的变量来做数据区分的标志。
' 但是,我建议你在给dwData赋值时,最好满足{dwData>=cbData}
lpCDS.cbData = UBound(buff) +
lpCDS.lpData = VarPtr(buff())
For msgId = To
lpCDS.dwData = msgId
Call SendMessage(hWnd, WM_COPYDATA, Me.hWnd, lpCDS)
Next
End Sub

接收端收到消息,处理后如下:

#WM_COPYDATA  dwData:      cbData:     lpData:你好,我是lichmama。
#WM_COPYDATA dwData: cbData: lpData:你好,我是lichmama。
#WM_COPYDATA dwData: cbData: lpData:你好,我是lichmama。
#WM_COPYDATA dwData: cbData: lpData:你好,我是lichmama。
#WM_COPYDATA dwData: cbData: lpData:你好,我是lichmama。
#WM_COPYDATA dwData: cbData: lpData:你好,我是lichmama。
#WM_COPYDATA dwData: cbData: lpData:你好,我是lichmama。
#WM_COPYDATA dwData: cbData: lpData:你好,我是lichmama。
#WM_COPYDATA dwData: cbData: lpData:你好,我是lichmama。
#WM_COPYDATA dwData: cbData: lpData:你好,我是lichmama。
#WM_COPYDATA dwData: cbData: lpData:你好,我是lichmama。
上一篇:【2017-04-25】winform公共控件、菜单和工具栏、Tab和无边框窗*作


下一篇:js实现无刷新上传