Passing strings between different apps **RESOLVED**
How do I send a string as a message between two apps using SendMessage or PostMessage?
This is the code I have, but it doesn't work, since I think I will have to use WM_COPYDATA with SendMessage, as suggested by MerrionComputin.
'In the first project
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 Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
'\\ Pointer validation in StringFromPointer
Private Declare Function IsBadStringPtrByLong Lib "kernel32" Alias "IsBadStringPtrA" (ByVal lpsz As Long, ByVal ucchMax As Long) As Long
Public Const GWL_WNDPROC = (-4)
Public lngPrevWndProc As Long
Public Const WM_USER = &H400
Public Const MY_MSG = WM_USER + 100
Public Function WindowProc(ByVal hWnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Select Case uMsg
Case MY_MSG
MsgBox StringFromPointer(lParam, 12)
'what to do here
'lParam returns a pointer, how do I get the string
Private Declare Function SendString Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As COPYDATASTRUCT) As Long
Public Function SendStringCrossTask(ByVal TargetWnd As Long, ByVal smessage As String)
Dim cpThis As COPYDATASTRUCT
Dim sAnsiString As String
sAnsiString = StrConv(smessage, vbFromUnicode)
With cpThis
.dwData = MCL_COPYSTRING
.cbData = LenB(sAnsiString)
.lpData = StrPtr(sAnsiString)
End With
If SendString(TargetWnd, WM_COPYDATA, Me.hwnd, cpThis) Then
'\\ target window accepted and processed the data
Else
'\\ target window did not accept the data
End If
End Function
::EDITTED FOR ANSI / UNICODE CHANGES ::
Last edited by MerrionComputin; Oct 29th, 2002 at 09:48 AM.
To think of it, I can use WM_SETTEXT messsage and then look for the same in the Window proc. But still, I would rather like to have a self-defined message to be sent, with the string as a parameter if its possible.
#2 - recieving the string
Subclass the recieving window and put the following as a replacement for it's windowproc:
VB Code:
Option Explicit
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 Type COPYDATASTRUCT
dwData As Long
cbData As Long
lpData As Long
End Type
Private Const WM_COPYDATA = &H4A
Private Const MCL_COPYSTRING = &HE
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)
Private mhOldWndproc As Long
Public Function VBWndproc(ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
BTW, what to do in the WindowProc of the subcalssed APP. I am currently looking out for WM_COPYDATA, then using the StringFromPointer function given by you earlier. But this is returning failure in the calling app and the data too is not available.
Merrion
I have having problems with the code. It is not letting me even subclass the button. I know I must be doing something wrong, but I am not able to pin point it out. Can you take a look at the code, and tell me where am I going wrong. The subclassing line is working properly, but the app terminates when it goes ot the VBWndProc.
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