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

Thread: how to use sendmessage to send string or keystroke to another application

  1. #1
    Join Date
    Jan 2008
    Posts
    4

    how to use sendmessage to send string or keystroke to another application

    i wanted to know to send string or keystroke to another application like an emulator from vb.net with sendmessage, i dont want to use the sendkeys because it is inaccurate and the application needs to be in focus all the time..need help badly

  2. #2
    Join Date
    Jun 2004
    Location
    NH
    Posts
    678

    Re: how to use sendmessage to send string or keystroke to another application

    Yes, you can use SendMessage, with the WM_KEYDOWN, WM_KEYUP, WM_SETTEXT, and VK_ constants.

    For a more complete solution to this, check out this article.
    SendKeys(With directive focus)

  3. #3
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: how to use sendmessage to send string or keystroke to another application

    To use SendMessage() you have to retrieve the window handle hWnd of the application you want to talk to.
    In fact I would try the following:
    Use FindWindow() or FindWindowEx() API to retrieve the hWnd,
    then, before any usage of SendKey() you set the focus to the app, using SendMessage()

    Code:
      Const WM_SETFOCUS As Long = &H7
      dim hWnd as Long
      hWnd = FindWindow("", "your window title")
      If hWnd then SendMessage hWnd, WM_SETFOCUS, 0, 0
    Certainly you have to declare FindWindow() and SendMessage() properly.

  4. #4
    Join Date
    Jun 2004
    Location
    NH
    Posts
    678

    Re: how to use sendmessage to send string or keystroke to another application

    wOf,

    You should be using integers instead of longs.
    Your solution as it is, doesn't really do what is intended.
    Focus can change at any moment, after you've set it.
    You could however use SendMessage or PostMessage to send those keys, instead of trying to set focus.

  5. #5
    Join Date
    Jan 2008
    Posts
    4

    Re: how to use sendmessage to send string or keystroke to another application

    i have the handle already, my main problem is how to send keys or data from a text.box or any sql data retrieved to the emulator or application with sendmessages. i can use sendkeys but the emulator has to be in focus and you have to set timer each procedures.

    the main idea was i will be getting some data from one emulator using screen scraping in vb.net then the value it will get will query to a db then the value retrieved will be passed back to another sessioned emulator using sendmessage.

    emulator (session 1) --screenscrape-->vb.net---retrieve data --> access
    access ---data--> vb.net --sends data---> emulator(session 2)

    why i have 2 session of emulator? because the 1st one will run continously and will queue data that needs to be scraped, the 2nd emulator will be used for the data retrieved that will be processed.

    please please..i need help..i just need a sample where you use sendmessage to send data or string or keystroke..thats it.

    thanks alot in advance

  6. #6
    Join Date
    Jun 2004
    Location
    NH
    Posts
    678

    Re: how to use sendmessage to send string or keystroke to another application

    So I'm guessing that you checked out the article, and couldn't see where SendMessage was used?

    Code:
        Const WM_SETTEXT As Int32 = 12
        Const WM_KEYDOWN As Int32 = 256
        Const WM_KEYUP As Int32 = 257
        Private Declare Function apiSendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Int32, ByVal wMsg As Int32, ByVal wParam As Int32, ByVal lParam As String) As Boolean
    
        apiSendMessage(hwnd, WM_SETTEXT, 0, "Hello")

  7. #7
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: how to use sendmessage to send string or keystroke to another application

    Sorry, TT(n), but you sample will set "Hello" to the titlebar of the application. It does not send the keys as intended.

    According to your article you convert characters to apropriate keystrokes and send them in a loop...
    Isn't there a direct way to send a string of characters?

    And thanks for pointing ou to use integers. I always forget that when in .NET. Im sooo much stuck to VB6.

  8. #8
    Join Date
    Jun 2004
    Location
    NH
    Posts
    678

    Re: how to use sendmessage to send string or keystroke to another application

    Sorry, TT(n), but you sample will set "Hello" to the titlebar of the application. It does not send the keys as intended.
    Only if you pass it the main handle.
    You should pass it the handle of the window you want to send text to.

    The article shows how to use WM_SETTEXT with SendMessage(SendKeys.Text), just like in this thread, but you pass the handle of the editable child window.

    The articles secondary recommendation converts characters to keystrokes, similar to the SendKeys class.
    Last edited by TT(n); January 24th, 2008 at 09:35 AM.

  9. #9
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: how to use sendmessage to send string or keystroke to another application

    Ah, I see. So that's easy with notepad which has 2 child windows and one of them is called "Edit" which is presumably the one where the text goes.
    So you have to find out what the name of the child window is...

    What if part of the keys are to be entered as text, and some of them are meant to go to buttons, and other child windows?

    I think sending the keystrokes to the main window is a safer method, because the application will decide, where the keys are to go.

  10. #10
    Join Date
    Jun 2004
    Location
    NH
    Posts
    678

    Re: how to use sendmessage to send string or keystroke to another application

    Yup, it's easy to find the title of the window, or class name(edit) with the SendKeys.GetWinFocus(True) function, as shown in the article.
    It will give you a path, to almost any desired child window.


    What if part of the keys are to be entered as text, and some of them are meant to go to buttons, and other child windows?
    Use the SendKeys.Text, for text, and SendKeys.Message for buttons and other non-editable windows.
    You'd reset the structure with the GetWinHandles function each time you change to a different window.


    I think sending the keystrokes to the main window is a safer method, because the application will decide, where the keys are to go
    I gather you mean using the sendkeys class to send to the main window. If so, then no not really.
    But you can still do that with the module using SendKeys.Send and pressing tab, to get around to the different windows.
    With the SendKeys class, if you've just opened the app window, then it's fairly safe, since you know as the developer, where the focus is by default, when it starts up, but otherwise the user could have moved the focus on purpose, or by accident during the sending.
    Windows has A.D.D.. lol
    Direct specification is definately better, and the module included in the article also ensures that the app remains in the foreground, during every key stroke with a keyboard hook.
    It also does not let key presses by the user interfere with sending, as the sendkeys class does, causing failure.
    This is done by attaching extra info(-11) to the keybrd_event, which is then recieved and identified by the hook before being dispatched to an application.

    I put alot of thought into this, and I believe the module goes above and beyond the call of duty.
    Last edited by TT(n); January 24th, 2008 at 03:29 PM. Reason: spelling

  11. #11
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: how to use sendmessage to send string or keystroke to another application

    Understood.
    You'd have to know the app you want to control by keystrokes, then. Or analyse it, so as to know the names of all the child windows you'd want to interact with. I agree, then, that it is safer to directly operate controls instead of sending keys to the main window.

    I can see where you really thought this through.

  12. #12
    Join Date
    Jan 2008
    Posts
    4

    Re: how to use sendmessage to send string or keystroke to another application

    hi!! thanks a lot for your time and help..unfortunately i wont be able to use the sendmessage for my problem..because the emulated window is under the main handle, i used spy++ just to check the handles under the application and the emulator window belongs to the main window, no wonder that whenever i use the handle or use the findwindowex i just wont work..i'll be using sendmessage and appactivate to send these data.

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




On-Demand Webinars (sponsored)