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
直接上代码,接收端:
1 '模块部分 2 'code by lichmama from cnblogs.com 3 Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, _ 4 Source As Any, ByVal Length As Long) 5 Public Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, _ 6 ByVal nIndex As Long) As Long 7 Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, _ 8 ByVal nIndex As Long, _ 9 ByVal dwNewLong As Long) As Long 10 Private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, _ 11 ByVal hWnd As Long, _ 12 ByVal Msg As Long, _ 13 ByVal wParam As Long, _ 14 ByVal lParam As Long) As Long 15 16 Private Const WM_COPYDATA = &H4A 17 Public Const GWL_WNDPROC = (-4) 18 Private Type COPYDATASTRUCT 19 dwData As Long '数据标识 20 cbData As Long '数据长度 21 lpData As Long '数据地址 22 End Type 23 Public lpPrevWndFunc As Long 24 25 Public Function CallbackWndProc(ByVal hWnd As Long, _ 26 ByVal wMsg As Long, _ 27 ByVal wParam As Long, _ 28 ByVal lParam As Long) As Long 29 30 If wMsg = WM_COPYDATA Then 31 Dim lpCDS As COPYDATASTRUCT 32 Dim buff() As Byte 33 34 Call CopyMemory(lpCDS, ByVal lParam, Len(lpCDS)) 35 ReDim buff(lpCDS.cbData) As Byte 36 Call CopyMemory(buff(0), ByVal lpCDS.lpData, lpCDS.cbData) 37 Debug.Print "#WM_COPYDATA", "dwData:" & lpCDS.dwData, "cbData:" & lpCDS.cbData, "lpData:" & Left(buff, lpCDS.cbData) 38 Erase buff 39 End If 40 CallbackWndProc = CallWindowProc(lpPrevWndFunc, hWnd, wMsg, wParam, lParam) 41 End Function
1 '窗体部分 2 'code by lichmama from cnblogs.com 3 Private Sub Form_Load() 4 lpPrevWndFunc = GetWindowLong(Me.hWnd, GWL_WNDPROC) 5 Call SetWindowLong(Me.hWnd, GWL_WNDPROC, AddressOf CallbackWndProc) 6 Me.Hide 7 End Sub 8 9 Private Sub Form_Unload(Cancel As Integer) 10 Call SetWindowLong(Me.hWnd, GWL_WNDPROC, lpPrevWndFunc) 11 End Sub
接下来是,发送端:
1 'code by lichmama from cnblogs.com 2 Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, _ 3 ByVal wMsg As Long, _ 4 ByVal wParam As Long, _ 5 lParam As Any) As Long 6 Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, _ 7 ByVal lpWindowName As String) As Long 8 9 Private Const WM_COPYDATA = &H4A 10 Private Type COPYDATASTRUCT 11 dwData As Long '数据标识 12 cbData As Long '数据长度 13 lpData As Long '数据地址 14 End Type 15 16 Private Sub Command1_Click() 17 Dim hWnd As Long 18 Dim lpCDS As COPYDATASTRUCT 19 Dim buff() As Byte 20 Dim msgId As Long 21 22 hWnd = FindWindow(vbNullString, "Receiver") 23 buff = "你好,我是lichmama。" 24 '在COPYDATASTRUCT结构中,dwData是[接收端]用来区分不同数据用的。 25 ' 因为,正常的数据交互中,程序要根据不同的数据做不同处理。 26 ' 所以,就需要一个dwData这样的变量来做数据区分的标志。 27 ' 但是,我建议你在给dwData赋值时,最好满足{dwData>=cbData} 28 lpCDS.cbData = UBound(buff) + 1 29 lpCDS.lpData = VarPtr(buff(0)) 30 For msgId = 0 To 10 31 lpCDS.dwData = msgId 32 Call SendMessage(hWnd, WM_COPYDATA, Me.hWnd, lpCDS) 33 Next 34 End Sub
接收端收到消息,处理后如下:
#WM_COPYDATA dwData:0 cbData:28 lpData:你好,我是lichmama。 #WM_COPYDATA dwData:1 cbData:28 lpData:你好,我是lichmama。 #WM_COPYDATA dwData:2 cbData:28 lpData:你好,我是lichmama。 #WM_COPYDATA dwData:3 cbData:28 lpData:你好,我是lichmama。 #WM_COPYDATA dwData:4 cbData:28 lpData:你好,我是lichmama。 #WM_COPYDATA dwData:5 cbData:28 lpData:你好,我是lichmama。 #WM_COPYDATA dwData:6 cbData:28 lpData:你好,我是lichmama。 #WM_COPYDATA dwData:7 cbData:28 lpData:你好,我是lichmama。 #WM_COPYDATA dwData:8 cbData:28 lpData:你好,我是lichmama。 #WM_COPYDATA dwData:9 cbData:28 lpData:你好,我是lichmama。 #WM_COPYDATA dwData:10 cbData:28 lpData:你好,我是lichmama。