Results 1 to 13 of 13

Thread: Passing strings between different apps **RESOLVED**

Threaded View

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Aug 2000
    Location
    India
    Posts
    2,288

    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
    VB Code:
    1. 'in a form with a command button
    2. Private Sub Form_Load()
    3.     Me.Caption = "Test Target Window"
    4.     Command1.Caption = "Test_Target_Button"
    5.     'subclass the command button
    6.     lngPrevWndProc = SetWindowLong(Command1.hWnd, GWL_WNDPROC, AddressOf WindowProc)
    7. End Sub
    8.  
    9. Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    10.     'set it back to normal before exiting
    11.     SetWindowLong Command1.hWnd, GWL_WNDPROC, lngPrevWndProc
    12. End Sub
    13.  
    14. '=====================
    15. 'in a module
    16. Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    17.  
    18. 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
    19.  
    20. Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
    21. '\\ Pointer validation in StringFromPointer
    22. Private Declare Function IsBadStringPtrByLong Lib "kernel32" Alias "IsBadStringPtrA" (ByVal lpsz As Long, ByVal ucchMax As Long) As Long
    23.  
    24.  
    25. Public Const GWL_WNDPROC = (-4)
    26.  
    27. Public lngPrevWndProc As Long
    28. Public Const WM_USER = &H400
    29. Public Const MY_MSG = WM_USER + 100
    30.  
    31. Public Function WindowProc(ByVal hWnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    32.     Select Case uMsg
    33.         Case MY_MSG
    34.             MsgBox StringFromPointer(lParam, 12)
    35.             'what to do here
    36.             'lParam returns a pointer, how do I get the string
    37.         Case Else
    38.             WindowProc = CallWindowProc(lngPrevWndProc, hWnd, uMsg, wParam, lParam)
    39.     End Select
    40. End Function
    41.  
    42. Public Function StringFromPointer(lpString As Long, lMaxLength As Long) As String
    43.  
    44.   Dim sRet As String
    45.   Dim lret As Long
    46.  
    47.   If lpString = 0 Then
    48.     StringFromPointer = ""
    49.     Exit Function
    50.   End If
    51.  
    52.   If IsBadStringPtrByLong(lpString, lMaxLength) Then
    53.     '\\ An error has occured - do not attempt to use this pointer
    54.       StringFromPointer = ""
    55.     Exit Function
    56.   End If
    57.  
    58.   '\\ Pre-initialise the return string...
    59.   sRet = Space$(lMaxLength)
    60.   CopyMemory ByVal sRet, ByVal lpString, ByVal Len(sRet)
    61.   If Err.LastDllError = 0 Then
    62.     If InStr(sRet, Chr$(0)) > 0 Then sRet = Left$(sRet, InStr(sRet, Chr$(0)) - 1)
    63.   End If
    64.  
    65.  
    66.   StringFromPointer = sRet
    67.  
    68. End Function

    In the second project:
    VB Code:
    1. 'in a form with a command button
    2. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
    3. (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Integer, ByVal lParam As String) As Long
    4.  
    5. Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, _
    6. ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    7.  
    8. Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" _
    9. (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
    10.  
    11. Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
    12. (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    13.  
    14. Private Const WM_USER = &H400
    15. Private Const MY_MSG = WM_USER + 100
    16.  
    17. Private Sub Command1_Click()
    18. Dim lngFrmHwnd As Long
    19. Dim lngBtnHwnd As Long
    20.     lngFrmHwnd = FindWindow(vbNullString, "Test Target Window")
    21.     lngBtnHwnd = FindWindowEx(lngFrmHwnd, 0&, vbNullString, "Test_Target_Button")
    22.     SendMessage lngBtnHwnd, MY_MSG, 0&, "hello world"
    23.    
    24. End Sub

    I am also attaching the code.

    Thanks
    Amitabh Kant
    Attached Files Attached Files
    Last edited by amitabh; Oct 29th, 2002 at 02:22 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width