Sending strings to other application (SendMessage API)
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Sending strings to other application (SendMessage API)

  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    

  2. #2
    Join Date
    Sep 2000
    Location
    FL
    Posts
    1,451

    Re: Sending strings to other application (SendMessage API)

    After you find the main window, you need to find the child that matches your criteria. Search the forum for enumchildwindows and you should get your solution

  3. #3
    Join Date
    Oct 2010
    Posts
    2

    Re: Sending strings to other application (SendMessage API)

    Thanks!!
    Using Spy++, the parent window's class name is "DialogFrameCL" with caption "Printing" having 2 child windows with the same class name and caption with the parent and I need to pump strings into one of the textbox of one of the child window. How to enemurate the child windows using Enumchildwindows API from the code

    Code:
    Public Sub barcode3(strBarcode As String)
        Dim hWndForm        As Long
        Dim hChildWin     As Long
        Dim hTxtBox As Long
        
        ' Find the destination window
        hWndForm = FindWindow("DialogFrameCL", "Printing")
        
        If (hWndForm = 0) Then
            MsgBox "Couldn't find a destination window with that caption.", vbExclamation
            Exit Sub
        End If
        
        ' Find the second window as a child of the destination window
        hChildWin = FindWindowEx(hWndForm, 0&, "DialogFrameCL", "Printing")
        
        If (hChildWin = 0) Then
            MsgBox "Couldn't find child window of the destination window.", vbExclamation
            Exit Sub
        End If
        
        ' Find the textbox
        hTxtBox = FindWindowEx(hChildWin, 0&, "Edit", vbNullString)
        
        If (hTxtBox = 0) Then
            MsgBox "Couldn't find target VB text box in the child window.", vbExclamation
            Exit Sub
        End If
    
        SendMessage hTxtBox, EM_REPLACESEL, 0&, ByVal CStr(strBarcode)
        
    End Sub
    Last edited by hypermount; October 13th, 2010 at 01:57 AM.

  4. #4
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,984

    Re: Sending strings to other application (SendMessage API)

    Please go back and add CODE TAGS [] [/]
    Code:
    ' like this
    So, you preserve formatting, and we can READ it
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  5. #5
    Join Date
    Oct 2011
    Posts
    1

    Re: Sending strings to other application (SendMessage API)

    Hi,

    I have this code -
    '--------------------------------------------------------------------------------------------------------------------
    Sub Test()

    ' To open the file...

    For x = 1 To 9999 Step 1

    ShellExecute 0, "Open", "C:\pdf\pdf.pdf", "", "", conShow

    x = Range("A" & x).Value

    ' Need to put code here to send the password to the PDF file

    Next x
    End Sub

    '--------------------------------------------------------------------------------------------------------------

    I need another code to be able to send the password and click ok on the adobe screen.

    Can some one help me, I have searched in CG, but no luck.

    Why do I want to code this: I have a list of files and their passwords stored on my excel sheet. I need to open the pdf files and print them and close the files back.

    Can someone please help me with this.

    Thanks
    Mazhar

  6. #6
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,984

    Re: Sending strings to other application (SendMessage API)

    Don't add to old threads that have been answered. If you have a different question, and didn't find it after searching, start a NEW THREAD.

    Although, you'd probably do better off at the Adobe site.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  7. #7
    DataMiser is offline Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    4,843

    Re: Sending strings to other application (SendMessage API)

    Hmm you are trying to open 9999 copies of the same pdf file? Why on earth would anyone do such a thing?
    Always use [code][/code] tags when posting code.

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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center