'form code
Private Sub Form_Load()
Me.Caption = "Test Target Window"
Command1.Caption = "Test_Target_Button"
'subclass the command button
'lngPrevWndProc = SetWindowLong(Command1.hWnd, GWL_WNDPROC, AddressOf VBWndproc)
mhOldWndproc = SetWindowLong(Command1.hWnd, GWL_WNDPROC, AddressOf VBWndproc)
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
'set it back to normal before exiting
SetWindowLong Command1.hWnd, GWL_WNDPROC, mhOldWndproc
End Sub
'module code
Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" _
(ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Public 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 Type COPYDATASTRUCT
dwData As Long
cbData As Long
lpData As Long
End Type
Private Const WM_COPYDATA = &H4A
Private Const MCL_COPYSTRING = &HE
Public Const GWL_WNDPROC = (-4)
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Private Declare Sub CopyMemoryCopyDataStruct Lib "kernel32" Alias "RtlMoveMemory" (Destination As COPYDATASTRUCT, ByVal Source As Long, ByVal Length As Long)
Public mhOldWndproc As Long
Public Function VBWndproc(ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
If Msg = WM_COPYDATA Then
Dim cpThis As COPYDATASTRUCT
Call CopyMemoryCopyDataStruct(cpThis, lParam, Len(cpThis))
If cpThis.dwData = MCL_COPYSTRING Then
Dim smessage As String
smessage = StringFromPointer(cpThis.lpData, cpThis.cbData)
Debug.Print smessage
'\\ tell other window we processed this message
VBWndproc = 1
Else
'\\ pass message on for standard processing
VBWndproc = CallWindowProc(mhOldWndproc, hWnd, wMsg, wParam, lParam)
End If
Else
'\\ pass message on for standard processing
VBWndproc = CallWindowProc(mhOldWndproc, hWnd, wMsg, wParam, lParam)
End If
End Function
Public Function StringFromPointer(lpString As Long, lMaxLength As Long) As String
Dim sRet As String
Dim lret As Long
If lpString = 0 Then
StringFromPointer = ""
Exit Function
End If
If IsBadStringPtrByLong(lpString, lMaxLength) Then
'\\ An error has occured - do not attempt to use this pointer
StringFromPointer = ""
Exit Function
End If
'\\ Pre-initialise the return string...
sRet = Space$(lMaxLength)
CopyMemory ByVal sRet, ByVal lpString, ByVal Len(sRet)
If Err.LastDllError = 0 Then
If InStr(sRet, Chr$(0)) > 0 Then sRet = Left$(sRet, InStr(sRet, Chr$(0)) - 1)
End If
StringFromPointer = sRet
End Function