Results 1 to 10 of 10

Thread: I need SendMessage or PostMessage Help

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2006
    Posts
    8

    I need SendMessage or PostMessage Help

    I'm tryingt to send text (the word "Hello" for now) to an "Untitled - Notepad" document. I can change the title bar, but not the text field. What am I doing wrong?


    I'm using VB 5.0 in WinXP OS.

    VB Code:
    1. 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
    2. Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As Any, ByVal lpWindowName As Any) As Long
    3. Private Declare Function SendMessageByStr& Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String)
    4. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    5. Private Declare Function PostMessageA Lib "user32" (ByVal hwnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    6. Private Declare Function PostMessage Lib "user32.dll" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    7. Private Declare Function PostMessageByStr& Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String)
    8.  
    9.  
    10. Private Const WM_SETTEXT = &HC
    11. 'Private Const EM_SETSEL = &HC
    12. 'Private Const WM_CHAR As Long = &H102
    13.  
    14. Private Sub Command1_Click()
    15. Dim x&
    16. Dim richedit&
    17.     Dim WinWnd As Long, Ret As String, RetVal As Long, lpClassName As String
    18. Ret = "Untitled - Notepad"
    19.     WinWnd = FindWindow(vbNullString, Ret)
    20.     If WinWnd = 0 Then MsgBox "Could not find the window ...": Exit Sub
    21.     MsgBox WinWnd '####PRROLLLY wanna taket his out at some point
    22.    
    23.     Dim sText As String
    24.     sText = "Hello"
    25. SendMessageByStr WinWnd, WM_SETTEXT, 0, "hello" ' PUTS hello into the title bar of the notepad. Kinda cool, but... not my goal
    26. SendMessage WinWnd, WM_SETTEXT, 0, "hello" ' PUTS bizar characters into the App bar... Definatly, not my goal
    27. PostMessage WinWnd, WM_SETTEXT, 0, "hello" ' Type Mismatch Error
    28. PostMessageByStr& WinWnd, WM_SETTEXT, 0, "hello" ' Does absolutely nothing
    29. End Sub

  2. #2
    Fanatic Member TTn's Avatar
    Join Date
    Jul 2004
    Posts
    708

    Re: I need SendMessage or PostMessage Help

    It's been awhile, since I used VB5, but I think you need to use the FindWindowEx, to get the "edit" portion of the notepad.
    I seen an example doing this somewhere.

    Also, always double check quirky MsgBox areas, as they can interfere with execution flow. In VB.NET, MessageBox.Show works much better.

  3. #3
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: I need SendMessage or PostMessage Help

    as TTn said, you need to send it to the Edit window:
    VB Code:
    1. Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" ( _
    2.     ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
    3.  
    4. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" ( _
    5.     ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    6.  
    7. Private Const WM_SETTEXT = &HC
    8.  
    9. Private Sub Command1_Click()
    10.     Dim lhWnd As Long
    11.    
    12.     lhWnd = FindWindowEx(0&, 0&, vbNullString, "Untitled - Notepad")
    13.     lhWnd = FindWindowEx(lhWnd, 0&, "Edit", vbNullString)
    14.    
    15.     If lhWnd Then
    16.         SendMessage lhWnd, WM_SETTEXT, 0&, ByVal "Hello"
    17.     Else
    18.         MsgBox "No way"
    19.     End If
    20. End Sub

  4. #4

    Thread Starter
    New Member
    Join Date
    Oct 2006
    Posts
    8

    Re: I need SendMessage or PostMessage Help

    That worked wonderfully thanks...

    On to my next problem. The "Untitled - Notepad" was my test subject. The program I actualy want to send the text to obviously is designed to accept text, but apparently doesn't have the specified "Edit" field.

    I hear some handy program called spy++ can get the title of the desired field from windows. I just wanna know... Where do I get this spy++? I searched these forums and the web at large and find all kinds of info on spyware and spying. Could you please point me in the right direction to learn more?

  5. #5
    Fanatic Member TTn's Avatar
    Join Date
    Jul 2004
    Posts
    708

    Re: I need SendMessage or PostMessage Help

    Spy++ is an application that comes in Visual Studio 6, along with it's dll.

    You can always spy on your own windows, by iterating through, certain class names, like "Edit", or "ComboBox" etc.
    VB Code:
    1. Dim lhWndChild As Long
    2. Dim lhWndParent As Long
    3. Dim i As Integer
    4.  
    5. i = 1
    6. lhWndParent = FindWindow(vbNullString, "AppCaption")
    7.  
    8.    Do
    9.             lhWndChild = FindWindowEx(lhWndParent, lhWndChild, "Edit", vbNullString)
    10.             If i = 1 Then
    11.                   'do something to the first text
    12.                   SendMessage lhWndChild, WM_SETTEXT, 0&, ByVal "Hello"
    13.             End If
    14.             If i = 2 Then
    15.                   'do something to the second text
    16.                    SendMessage lhWndChild, WM_SETTEXT, 0&, ByVal "GoodBye"
    17.             End If
    18.             If i = 3 Then
    19.                   'do something to the third text
    20.                   SendMessage lhWndChild, WM_SETTEXT, 0&, ByVal "Exit"
    21.  
    22.                'exit loop if there are three text boxes.
    23.                 Exit Do
    24.             End If
    25.            i  = (i + 1)
    26.    Loop
    Last edited by TTn; Oct 12th, 2006 at 11:57 PM.

  6. #6

    Thread Starter
    New Member
    Join Date
    Oct 2006
    Posts
    8

    Re: I need SendMessage or PostMessage Help

    Quote Originally Posted by TTn
    Spy++ is an application that comes in Visual Studio 6, along with it's dll.

    You can always spy on your own windows, by iterating through, certain class names, like "Edit", or "ComboBox" etc.
    Damn... I guess I need to get a copy of visual studio 6.

    I tried your code, and it appears to have executed correctly, but there was no text showing up.

    What other kinds of class names are there? What I'm wanting to recieve the keystrokes may not even have a class name, as the app i'm trying to send the keystrokes to isn't a windows form. It is an area to enter a log in name for a video game. I've been using sendkeys and it works just fine, but I'm tired of having to AppActivate and loosing the focus on my other windows I may have open. Any other suggestions?

    On the plus side, I am learning alot =) Thanks so far !!
    Last edited by VB5Tony; Oct 12th, 2006 at 07:15 PM.

  7. #7
    Fanatic Member TTn's Avatar
    Join Date
    Jul 2004
    Posts
    708

    Re: I need SendMessage or PostMessage Help

    Send me your email in a private message here, so I can send you spy++ and it's dll. You might get lost at first, but I can give you basic instruction.

    It should be a window then, from how it sounds, and it should have class name/s.

    VB Code:
    1. Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
    2.  
    3. 'A few class names not used here
    4. Const gcClassnameMSWord = "OpusApp"
    5. Const gcClassnameMSExcel = "XLMAIN"
    6. Const gcClassnameMSIExplorer = "IEFrame"
    7. Const gcClassnameMSVBasic = "wndclass_desked_gsk"
    8. Const gcClassnameNotePad = "Notepad"
    9. Const gcClassnameMyVBApp = "ThunderForm"
    10.  
    11. Dim lpClassName As String
    12. Dim RetVal As Long
    13.  
    14.     'Create a buffer
    15.     lpClassName = Space(256)
    16.     'retrieve the class name
    17.     RetVal = GetClassName(lhWndParent, lpClassName, 256)
    18.   MsgBox "Classname: " + Left$(lpClassName, RetVal)



    Although, you can reset the focus and foreground window after the appactivate.
    Using the additional GetForegroundWindow, SetForegroundWindow, GetFocus, and SetFocus API.

    But then you're still using the inferior sendkeys, which may never be sent to where you want. *Crosses fingers* So SendMessage is best here. I think.
    Last edited by TTn; Oct 13th, 2006 at 12:10 AM.

  8. #8

    Thread Starter
    New Member
    Join Date
    Oct 2006
    Posts
    8

    Re: I need SendMessage or PostMessage Help

    Interesting. In my original code (at the top of the page) I dropped this in there, and it worked...
    VB Code:
    1. PostMessage WinWnd, WM_CHAR, Asc("Hello"), 0&
    but it only typed "H", as it is the first character and WM_CHAR only sees one character. So I'm thinking about some kind of loop that reads each character in a string variable one at a time then posts it to the desired location until it reaches the end of the string. Problem is,.. I don't know how to read one character at a time in a string.

    It would also be neat-o to send Control+Keys also.

    Any advice?

  9. #9
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: I need SendMessage or PostMessage Help

    It is in fact Asc that only sees one character, but you are right about the solution - you need to work with just one letter at a time. To do that you can use a loop, and the Mid function, eg:
    VB Code:
    1. Dim strHello as String
    2. Dim intCount as Integer
    3.   strHello = "Hello"
    4.  
    5.   For intCount = 1 To Len(strHello)
    6.     MsgBox Asc(  Mid(strHello, intCount, 1)  )
    7.   Next intCount

  10. #10
    Frenzied Member ice_531's Avatar
    Join Date
    Aug 2002
    Location
    Sitting w/ Bob Status: -Next -To- Null- Friend: Philip
    Posts
    1,152

    Re: I need SendMessage or PostMessage Help

    Just thought i'd chime in here.

    patorjk's api spy is a great utility, and i'd recommend it to anyone working with API.

    patorjk.com i think is the website, but i haven't checked in a few years sooo you might want to verify that.

    I think there should be a stickied thread in this forum with a list of useful tools and websites to find them.
    :::`DISCLAIMER`:::
    Do NOT take anything i have posted to be truthful in any way, shape or form.
    Thank You!

    --------------------------------
    "Never heard about "hiking" poles. I usualy just grab a stick from the nature, and use that as a pole." - NoteMe
    "Finaly I can look as gay as I want..." - NoteMe
    Languages: VB6, BASIC, Java, C#. C++

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