[RESOLVED] SendMessage fail VB6-VBForums
Results 1 to 12 of 12

Thread: [RESOLVED] SendMessage fail VB6

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2007
    Posts
    32

    Resolved [RESOLVED] SendMessage fail VB6

    This may be somewhat related to my previous post. However, after using FindWindow and FindWindowEX I have been unable to send any message to any window in the application (hummingbird terminal emulator). For now I've isolated everything to a simple code to insure that this function will work properly.
    In this exercise I am sending an "a" key and, god willing, maybe a paste function. For now though, just the "a". Maybe I have a paramater set incorrectly? To experiment, I have sent the messages to every available window, but the one I have identified in the code below is the one where the text should appear. I have also tested it and I am sure that all of the handles are being found.

    Code:
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
    (ByVal lpClassName As String, _
    ByVal lpWindowName As String) _
    As Long
    
    Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" _
        (ByVal hWndParent As Long, _
         ByVal hWndChildAfter As Long, _
         ByVal lpszClass As String, _
         ByVal lpszTitle As String) _
        As Long
    
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
        (ByVal hWnd As Long, _
         ByVal wMsg As Long, _
         ByVal wParam As Long, _
         ByRef lParam As Any) _
        As Long
    
    Public Const WM_KEYDOWN = &H100
    Public Const WM_KEYUP = &H101
    Public Const VK_A = &H41
    
    Sub text()
    Dim hWndTPX As Long
    Dim hWndMiddle As Long
    Dim hWndText As Long
    
    hWndTPX = FindWindow("Afx:400000:8", vbNullString)
    hWndMiddle = FindWindowEx(hWndTPX, 0&, "HETM3270", vbNullString)
    hWndText = FindWindowEx(hWndMiddle, 0&, "HEDR3270", vbNullString)
    
    SendMessage hWndText, WM_KEYDOWN, VK_A, vbNullString
    SendMessage hWndText, WM_KEYUP, VK_A, vbNullString
    
    End Sub
    FYI: I've never used the SendMessage command so I might need basic direction.

  2. #2
    PowerPoster Fazi's Avatar
    Join Date
    Aug 2005
    Location
    Underworld
    Posts
    2,525

    Re: SendMessage fail VB6

    Can you pls tell
    Code:
    hWndText = FindWindowEx(hWndMiddle, 0&, "HEDR3270", vbNullString)
    after executing above line of code, what is the value is look like of hWndText ?

  3. #3
    VBaholic & Loving It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    GetWindowRect()
    Posts
    12,052

    Re: SendMessage fail VB6

    2 things I think are worth exploring
    1. Don't recommend using vbNullString, rather use ByVal 0& for the keydown.
    2. Per documentation on MSDN, the lParam's 1st, 30th & 31st bit may need to be modified for the keyup message; therefore, suggest using for lParam, ByVal &HC0000001 (believe that is value for 1,30,31 bit being set, double check it)

    One final thing. Whenever dealing without outside apps, it is almost always wiser to use PostMessage vs SendMessage. SendMessage will hang your app if the target app is hung. PostMessage returns immediately. Alternatively, there are SendMessage-like apis too: SendMessageTimeOut for example.
    Last edited by LaVolpe; Mar 11th, 2008 at 03:44 PM.
    Insomnia is just a byproduct of, "It can't be done"

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum. Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts


    {Alpha Image Control} {Memory Leak FAQ} {GDI+ Classes/Samples} {Unicode Open/Save Dialog} {Icon Organizer/Extractor}
    {VBA Control Arrays} {XP/Vista Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  4. #4

    Thread Starter
    Member
    Join Date
    Aug 2007
    Posts
    32

    Cool Re: SendMessage fail VB6

    Thanks for the replies.

    Fazi, I get a true boolean if I watch the whole line. On hWndText I get a decimal such as 133096 in this instance if I hover the mouse.

    LaVolpe, thanks for documentation I will look through that. I have however already experimented with byVal &0. Though I will try messing with the parameter's numeric value as mentioned in the article in a bit here. I had looked into postmessage, and I think that's what I'm trying to avoid. I would actually like my app to hang if the Emulator is hanging. My app's sole purpose to send, retrieve, and manipulate data to and from the emulator. This is (one of many reasons) why sendkeys doesn't work all that reliably.

  5. #5
    PowerPoster Fazi's Avatar
    Join Date
    Aug 2005
    Location
    Underworld
    Posts
    2,525

    Re: SendMessage fail VB6

    There is another method. Add the constant WM_CHAR. Replace the last two lines with one single line of code.
    Code:
    public cost WM_CHAR = &H102
    'all your code 
    SendMessage hWndText, WM_CHAR, 115, 0&
    '115 = ascii s

  6. #6

    Thread Starter
    Member
    Join Date
    Aug 2007
    Posts
    32

    Re: SendMessage fail VB6

    Thank you. But still nothing. Does anyone know of a way to send text as a string maybe (in this method)?

  7. #7
    VBaholic & Loving It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    GetWindowRect()
    Posts
    12,052

    Re: SendMessage fail VB6

    A couple of ideas:
    1. Use Spy++, enter a letter into the emulator and look at the specific wparam & lparam values Spy++ shows for the WM_KeyUp/Down messages. Now use your SendMessages and pass the exact same wparam & lparam. Does Spy++ show it received them?

    2. Try using your sendmessage targeting NotePad. If it works, then the code should be good. All apps have option to use InSendMessage API to test whether or not the message is coming from their process or not. If not, the app may be rejecting the message. Anti-cheat routines in many apps use this. Another api you might consider is SendInput; though that is not guaranteed to work.

    3. Some windows may not process messages sent to them, rather they are processed by some hidden window or the parent window. Spy++ can help verify that. As suggested in para 1 above, if typing in the emulator shows receipt of keystrokes in Spy++, then window is processing its own. However, if it is not receiving those keystrokes or receiving something else (like wm_user messages instead), then you may be sending to the wrong window.
    Last edited by LaVolpe; Mar 12th, 2008 at 08:55 AM.
    Insomnia is just a byproduct of, "It can't be done"

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum. Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts


    {Alpha Image Control} {Memory Leak FAQ} {GDI+ Classes/Samples} {Unicode Open/Save Dialog} {Icon Organizer/Extractor}
    {VBA Control Arrays} {XP/Vista Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  8. #8

    Thread Starter
    Member
    Join Date
    Aug 2007
    Posts
    32

    Re: SendMessage fail VB6

    Ok, so by adjusting the parameters and using WM_CHAR I was able to send a few characters. However, using these methodes I was unable to paste anything. I pasted to notepad but not to the emulator.

    Thank you both for your suggestions. I had tried the spy++ method you described for each of the windows and that's where I got the parameters etc. for HEDR3270. What's odd is that HETM3270 was the window that eventually was able to recieve the message I sent while logging nothing when I tried typing in the window or moving the mouse over it.

    And Fazi the message blaster in your signature works very well for experimentation. Thank you!

    Now any ideas about pasting? This also poses another problem, eventually I'll need to send other messages such function keys and enter and to the emulator. Since I haven't figured out how to send virtual keys is there another method for this or should I continue to experiment VK_? ?

    I have posted the updated "partially" working code:

    Code:
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
    (ByVal lpClassName As String, _
    ByVal lpWindowName As String) _
    As Long
    
    Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" _
        (ByVal hWndParent As Long, _
         ByVal hWndChildAfter As Long, _
         ByVal lpszClass As String, _
         ByVal lpszTitle As String) _
        As Long
    
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
        (ByVal hWnd As Long, _
         ByVal wMsg As Long, _
         ByVal wParam As Long, _
         ByRef lParam As Any) _
        As Long
    
    Public Const WM_CHAR = &H102
    Public Const WM_CLOSE = &H10
    Public Const WM_PASTE = &H302
    Public Const WM_KEYDOWN = &H100
    Public Const WM_KEYUP = &H101
    Public Const VK_CONTROL = &H11
    Public Const VK_A = &H41
    Public Const VK_E = &H45
    Public Const VK_V = &H86
    
    Sub text()
    Dim hWndTPX As Long
    Dim hWndMiddle As Long
    Dim hWndText As Long
    
    hWndTPX = FindWindow("Afx:400000:8", vbNullString)
    hWndMiddle = FindWindowEx(hWndTPX, 0&, "HETM3270", vbNullString)
    hWndText = FindWindowEx(hWndMiddle, 0&, "HEDR3270", vbNullString)
    
    SendMessage hWndMiddle, WM_CHAR, 116, 0&
    SendMessage hWndMiddle, WM_CHAR, 112, 0&
    
    End Sub
    The paste method I used was:

    Code:
    SendMessage hWndMiddle, WM_PASTE, 0&, 0&

  9. #9
    VBaholic & Loving It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    GetWindowRect()
    Posts
    12,052

    Re: SendMessage fail VB6

    Can your app be doing this in the foreground and can the console be in the foreground also? There is another API you can use, but the target app must have the focus: keybd_event. Before it is called, you have to set focus to the console, send as many keystrokes as you wish, but read up on the api a little on msdn. It isn't difficult, just make sure you send a key up for evey key down.

    Here is an example of using the SendInput API.
    Last edited by LaVolpe; Mar 12th, 2008 at 06:11 PM.
    Insomnia is just a byproduct of, "It can't be done"

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum. Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts


    {Alpha Image Control} {Memory Leak FAQ} {GDI+ Classes/Samples} {Unicode Open/Save Dialog} {Icon Organizer/Extractor}
    {VBA Control Arrays} {XP/Vista Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  10. #10

    Thread Starter
    Member
    Join Date
    Aug 2007
    Posts
    32

    Re: SendMessage fail VB6

    Thank you to everyone who has posted. After a few days of experimentation, I finally got it to work. I think that hostexplorer made an override to the paste function and to the alpha section of the keyboard. I experimented and found that I could only send function key messages, enter, delete, and escape. So I re-assigned Escape to paste and I was pretty much good to go. A little weird that some keys worked and others didn't.
    If anyone knows more about this and would like to elaborate please feel free as I am curious. But, otherwise I consider the thread closed.

    THANKS!

  11. #11

    Thread Starter
    Member
    Join Date
    Aug 2007
    Posts
    32

    Re: SendMessage fail VB6

    After a few months of working on this project I discovered that Hummingbird HostExplorer comes with it's own keyboard drivers and several pre-set mappings. I'm not 100% positive that this is the cause, but I am nearly certain that it is based on other behaviors I have observed. It seems that when hostexplorer loads it takes over the keyboard whenever it has the focus. I noticed strange errors happening on non-standard keyboards and I when I did full system driver update I came across the "HostExplorer driver".

    I know very little about drivers, so if someone cares to comment on this that would be awesome otherwise I'll mark the thread resolved in a week or so since I worked around it.

  12. #12
    New Member
    Join Date
    Aug 2008
    Posts
    8

    Re: SendMessage fail VB6

    I had a problem like this once

    make sure you use byval and [integer]&

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.