CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Threaded View

  1. #1
    Join Date
    Oct 2010
    Posts
    2

    Sending strings to other application (SendMessage API)

    I'm trying to create an application to send strings to other application's textbox.

    Used these 2 functions but unable to sendmessage. It seems to manage to find the Window with the caption "Printing" but can't find the textbox.


    Code:
    Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    Public Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
    Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long
    Public Const EM_REPLACESEL = &HC2
    
    
    'For Second method
    'Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    Private Declare Function SendMessageByString Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long
    'Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
    Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
    Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
    
    
    Private Const WM_SETTEXT = &HC
    Private Const GW_HWNDNEXT = 2
    Private Const GW_CHILD = 5

    Code:
    Public Function barcode2(strBarcode As String)
    Dim wnd As Long, meswnd As Long, lhndchild As Long
    Dim StrClass As String * 255
    Dim i As Integer
    
    wnd = FindWindow(vbNullString, "Printing")
    
    wnd = FindWindowEx(wnd, 0&, vbNullString, "Printing")
    
    lhndchild = GetWindow(wnd, GW_CHILD)
    
    'Continue enumuration
    Do
        Call GetClassName(lhndchild, StrClass, 255)
        i = InStr(StrClass, "Edit")
        If i > 0 Then
           SendMessageByString lhndchild, WM_SETTEXT, 0, strBarcode
    '       Me.CmdSend.Enabled = True
           Exit Function
        End If
        
        lhndchild = GetWindow(lhndchild, GW_HWNDNEXT)
        If lhndchild = 0 Then Exit Do
    
    Loop
    
    End Function
    Code:
    Public Sub barcode3(strBarcode As String)
        Dim hWndForm        As Long
        Dim hWndTextBox     As Long
        
        ' Find the destination window
        hWndForm = FindWindow(vbNullString, "Printing")
        
        If (hWndForm = 0) Then
            MsgBox "Couldn't find a destination window with that caption.", vbExclamation
            Exit Sub
        End If
        
        ' Find the text box as a child of the destination window
        hWndTextBox = FindWindowEx(hWndForm, 0&, "Edit", vbNullString)
        
        If (hWndTextBox = 0) Then
            MsgBox "Couldn't find target VB text box in the destination window.", vbExclamation
            Exit Sub
        End If
        
        ' If all's well, send the message.
        '
        ' Be aware that, in this example, until the message
        ' box is dismissed, this app will be hung.
        SendMessage hWndTextBox, WM_SETTEXT, 0&, ByVal CStr(strBarcode)
        
    End Sub
    Attached Images Attached Images
    • File Type: jpg 1.JPG (85.9 KB, 12702 views)
    • File Type: jpg 2.JPG (87.8 KB, 12054 views)
    • File Type: jpg 3.JPG (86.4 KB, 12130 views)

Tags for this Thread

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

Featured