Results 1 to 23 of 23

Thread: send message problem

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Resolved send message problem

    i am doing a small project that i would like to send a string from a list box and an option "enter" to the next top window, similar to the program in http://www.vbforums.com/showthread.php?t=316373.

    my problems are, when i try to get the hwnd for the target program several have a zreo value, ones that i get a value for do not get the value for the textbox or other control where i want the message to go (i have been using apispy to check the correct value).

    if i hard code the hwnd for the control i can send message, but it is not correct

    i have an array of list boxes wish to send the listbox text to the control that has focus on the applicaton then return to select another item.


    VB Code:
    1. Private Const WM_COPY = &H301
    2. Private Const WM_PASTE = &H302
    3. Private Const WM_SETTEXT As Long = &HC
    4.  
    5.  
    6. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA"_
    7.  (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long,_
    8.  lparam As Any) As Long
    9. Private Declare Function GetActiveWindow Lib "user32" () As Long
    10.  
    11.  
    12.  
    13. Private Sub List1_Click(index As Integer)
    14. Dim newtext As String
    15. newtext = List1(index).Text
    16. Debug.Print Me.hwnd
    17.  
    18. Me.Hide
    19. wind = GetActiveWindow
    20. Debug.Print wind
    21. Me.Show
    22.  
    23.  ' hwind for text box on program i was trying to paste to, detected by apispy
    24. 'wind = 329952
    25.  SendMessage wind, WM_SETTEXT, 0&, List1(index).Text
    26.  
    27. List1(index).SetFocus
    28. End Sub
    Last edited by westconn1; Jan 23rd, 2005 at 05:27 AM.

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

    Re: send message problem

    Hello westconn,

    First off, let me just say that window handles change ( they are not normally static ) so you setting wind to 329952 might not work unless when you found the window you used classname AND window caption to make sure it's getting the correct handle every time.

    I can't tell if you did from the code you've posted thus far.

    Let me know if i am not understanding you
    :::`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++

  3. #3
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: send message problem

    Usually its best to use the FindWindow and FindWindowEx APIs to drill down
    the window structure to your target window. You can't hard code handles
    either because they are dynamic and can change during window state
    changes (min/max/normal) and instance runs of the third party program.

    HTH
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: send message problem

    Doh! Sorry Ice, I'm typing to slooow because I'm getting a cold.
    Guess I'll go to bed now.

    later.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: send message problem

    i realise that the handle changes, and just put that in to test what happen when i did the sendmessage, as i said it did not produce the right message, but at least it went to the right place.

    i am trying to detect which application i want to send to using getactive window.

    but when that does return a value other than zero, it is not for the control with focus.

  6. #6

    Thread Starter
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: send message problem

    i got the getactive window code from an example posted by tec-nico in the thread i mentioned before


    rgds peter

  7. #7
    Frenzied Member Tec-Nico's Avatar
    Join Date
    Jun 2002
    Location
    México
    Posts
    1,192

    Re: send message problem

    Did you try Manavo's code?
    Option Explicit

    Private Declare Function AttachThreadInput Lib "user32" (ByVal idAttach As Long, _
    ByVal idAttachTo As Long, _
    ByVal fAttach As Long) _
    As Long
    Private Declare Function GetForegroundWindow Lib "user32" () As Long
    Private Declare Function GetFocus Lib "user32" () As Long
    Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, _
    lpdwProcessId As Long) _
    As Long
    Private Declare Function GetCurrentThreadId Lib "kernel32" () As Long

    Private Const WM_COPY = &H301
    Private Const WM_PASTE = &H302

    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

    Public Sub Paste(ByVal hwnd As Long)
    SendMessage hwnd, WM_PASTE, 0&, ByVal 0&
    End Sub

    Private Sub Timer1_Timer()
    Dim hFore As Long, hFocus As Long

    hFocus = GetFocus

    If hFocus = 0 Then
    hFore = GetForegroundWindow()
    Call AttachThreadInput(GetWindowThreadProcessId(hFore, 0&), GetCurrentThreadId, True)
    hFocus = GetFocus
    Call AttachThreadInput(GetWindowThreadProcessId(hFore, 0&), GetCurrentThreadId, False)
    End If
    Paste hFocus
    Me.Caption = hFocus

    End Sub
    We miss you, friend... Rest in Peace, we will take care of the rest of it.

    [vbcode]
    On Error Me.Fault = False
    [/vbcode]
    - Silence is the human way to share ignorance
    Tec-Nico

  8. #8

    Thread Starter
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: send message problem

    tec-nico,

    i set up the code you posted, without the timer, so that when i select the list item it runs the code, the results were very erratic, if i kept just clicking the list box item i got the results from debug.print pasted from the immediate window

    Code:
    hwnd   460454   gettickcount  8737640              '460454 application vbide
    hwnd   460454   gettickcount  8738953               'nothing pasted, ide was 
    hwnd   460454   gettickcount  8739593                 'running so it wouldn't
    hwnd   460454   gettickcount  8740156 
    hwnd   460454   gettickcount  8741328 
    hwnd   460454   gettickcount  8742046 
    hwnd   460454   gettickcount  8742640 
    hwnd   460454   gettickcount  8743265 
    hwnd   460454   gettickcount  8744015 
    hwnd   460454   gettickcount  8744656 
    hwnd   460454   gettickcount  8745265 
    hwnd   460454   gettickcount  8745890 
    hwnd   460454   gettickcount  8746484 
    hwnd   0   gettickcount  8773218                        '67388 application
    hwnd   67388   gettickcount  8773796                  'word document
    hwnd   0   gettickcount  8774312                         ' nothing pasted 
    hwnd   0   gettickcount  8774828 
    hwnd   67388   gettickcount  8775890 
    hwnd   0   gettickcount  8776359 
    hwnd   0   gettickcount  8777312 
    hwnd   67388   gettickcount  8777734 
    hwnd   0   gettickcount  8778203 
    hwnd   0   gettickcount  8779125 
    hwnd   0   gettickcount  8780000 
    hwnd   67388   gettickcount  8780890 
    hwnd   0   gettickcount  8781328 
    hwnd   0   gettickcount  8782062 
    hwnd   0   gettickcount  8782890 
    hwnd   0   gettickcount  8783734 
    hwnd   67388   gettickcount  8784171 
    hwnd   67388   gettickcount  8784625 
    hwnd   67388   gettickcount  8785093 
    hwnd   0   gettickcount  8785531 
    hwnd   67388   gettickcount  8786406 
    hwnd   67388   gettickcount  8786843 
    hwnd   67388   gettickcount  8787281 
    hwnd   0   gettickcount  8787750 
    hwnd   0   gettickcount  8788750 
    hwnd   67388   gettickcount  8789296 
    hwnd   0   gettickcount  8789843 
    hwnd   0   gettickcount  8790453 
    hwnd   67388   gettickcount  8791031 
    hwnd   0   gettickcount  8791734 
    hwnd   67388   gettickcount  8792312 
    hwnd   67388   gettickcount  8792906 
    hwnd   67388   gettickcount  8793515 
    hwnd   67388   gettickcount  8794046 
    hwnd   67388   gettickcount  8794593 
    hwnd   0   gettickcount  8795156 
    hwnd   0   gettickcount  8795687 
    hwnd   67388   gettickcount  8796703 
    hwnd   67388   gettickcount  8797203 
    hwnd   1246594   gettickcount  9059625         '1246594 application, previous
    hwnd   0   gettickcount  9060109                   'exe of this program,
    hwnd   0   gettickcount  9060921                   'pasted to textbox when 
    hwnd   0   gettickcount  9061500                   'hwnd not 0
    hwnd   0   gettickcount  9062468 
    hwnd   0   gettickcount  9063437 
    hwnd   0   gettickcount  9064015 
    hwnd   0   gettickcount  9065062 
    hwnd   1246594   gettickcount  9065890 
    hwnd   1246594   gettickcount  9066562 
    hwnd   1246594   gettickcount  9067015 
    hwnd   1246594   gettickcount  9067421 
    hwnd   1246594   gettickcount  9067953 
    hwnd   1246594   gettickcount  9068515 
    hwnd   1246594   gettickcount  9069046 
    hwnd   1246594   gettickcount  9069625 
    hwnd   0   gettickcount  9070203 
    hwnd   1246594   gettickcount  9070765 
    hwnd   1246594   gettickcount  9071203 
    hwnd   1246594   gettickcount  9071671 
    hwnd   1246594   gettickcount  9072171 
    hwnd   1246594   gettickcount  9072625 
    hwnd   0   gettickcount  9073093 
    hwnd   0   gettickcount  9074046 
    hwnd   0   gettickcount  9074578 
    hwnd   0   gettickcount  9075109 
    hwnd   1246594   gettickcount  9076281
    here is the code i am using

    VB Code:
    1. Public Sub Paste(ByVal hwnd As Long)
    2. SendMessage hwnd, WM_PASTE, 0&, ByVal 0&
    3. Debug.Print "hwnd  "; hwnd; "  gettickcount "; GetTickCount
    4.  
    5. End Sub
    6.  
    7. Private Function gethwnd()
    8. Dim hFore As Long, hFocus As Long
    9.  
    10. hFocus = GetFocus
    11.  
    12. If hFocus = 0 Then
    13. hFore = GetForegroundWindow()
    14. Call AttachThreadInput(GetWindowThreadProcessId(hFore, 0&), GetCurrentThreadId, True)
    15. hFocus = GetFocus
    16. Call AttachThreadInput(GetWindowThreadProcessId(hFore, 0&), GetCurrentThreadId, False)
    17. End If
    18. Paste hFocus
    19. Me.Caption = hFocus
    20. End Function
    21.  
    22. Private Sub List1_Click(index As Integer)
    23. Dim newtext As String
    24. newtext = List1(index).Text
    25. Clipboard.Clear
    26. Clipboard.SetText List1(index).Text
    27. Me.Hide
    28. gethwnd
    29. Me.Show
    30. List1(index).SetFocus
    31. End Sub

    this program will be very useful to me if can get it to work properly, but also i am trying to learn a lot more about api calls and just to make them work.

    thanks for your help peter

    edit: anyone, how do you make the quote boxs have sroll bar to reduce size??

    figured that out now...........
    Last edited by westconn1; Dec 29th, 2004 at 04:58 AM.

  9. #9

    Thread Starter
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: send message problem

    i am still floundering on this problem is anyone able to offer any further suggestions or alternatives





    peter

  10. #10
    Frenzied Member Tec-Nico's Avatar
    Join Date
    Jun 2002
    Location
    México
    Posts
    1,192

    Re: send message problem

    Don't get desperate... I have been trying to make it work as well. So sooner or later I might find a solution.
    We miss you, friend... Rest in Peace, we will take care of the rest of it.

    [vbcode]
    On Error Me.Fault = False
    [/vbcode]
    - Silence is the human way to share ignorance
    Tec-Nico

  11. #11
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: send message problem

    Hey Tec

    This seems to work

    VB Code:
    1. Me.Hide
    2. [b]DoEvents[/b]
    3. gethwnd
    4. Me.Show

    It doesn't have time to hide your application and set focus to the notepad or whatever application you expect to have focus. I think


    Has someone helped you? Then you can Rate their helpful post.

  12. #12

    Thread Starter
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: send message problem

    Hey Tec

    This seems to work

    visual basic code:Me.Hide
    DoEvents
    gethwnd
    Me.Show



    It doesn't have time to hide your application and set focus to the notepad or whatever application you expect to have focus. I think

    Thanks manavo, this cretainly fixes the reliability problem for using to notepad or to my copiled vb program.
    if MS word or a different project vbide window nothing pastes, but i will compile this now and test a lot more.

    thanks again

  13. #13
    Frenzied Member Tec-Nico's Avatar
    Join Date
    Jun 2002
    Location
    México
    Posts
    1,192

    Re: send message problem

    Hey Manavo! I owe you one
    We miss you, friend... Rest in Peace, we will take care of the rest of it.

    [vbcode]
    On Error Me.Fault = False
    [/vbcode]
    - Silence is the human way to share ignorance
    Tec-Nico

  14. #14
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: send message problem

    Quote Originally Posted by westconn1
    Thanks manavo, this cretainly fixes the reliability problem for using to notepad or to my copiled vb program.
    if MS word or a different project vbide window nothing pastes, but i will compile this now and test a lot more.

    thanks again
    You're welcome

    Actually I just tried Word and I couldn't get it to work either... Weird though, I expected it would


    Quote Originally Posted by Tec-Nico
    Hey Manavo! I owe you one


    Has someone helped you? Then you can Rate their helpful post.

  15. #15

    Thread Starter
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: send message problem

    hahaha

    the program i mainly wanted to use this with (which isn't on my computer) doesn't get the paste, like Word, can i try to sendmessage the listbox text, instead of using sendmessage paste, (bypass the clipboard) and what would be the sendmessage command to do it.

    p.

  16. #16
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: send message problem

    Well, the other option would be to use the WM_SETTEXT although that would cause all the data in that textbox (if it is a simple textbox or richtextbox at least) to be lost... I'll have to see if there is another way. SendKeys somes to mind although I don't think it's the best way...


    Has someone helped you? Then you can Rate their helpful post.

  17. #17
    Hyperactive Member Dmitri K's Avatar
    Join Date
    Sep 2002
    Location
    West Palm Beach, FL
    Posts
    444

    Re: send message problem

    Your sendmessage declaration is wrong, that's why it doesn't work, but the syntax is right.

    If you're using WM_, you must change sendmessage declaration. Last parameter (lParam as any) must be ByVal lParam As String.

    The modified sendmessage is often refered to as SendMessageByString

    Original sendmessage:
    Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

    Modified sendmessage:
    Public 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

  18. #18

    Thread Starter
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: send message problem

    dmitri

    Modified sendmessage:
    Public 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
    i changed my program to use the modified send message,

    it still pastes fine into notepad, but nothing pastes to msword

    thanks peter

  19. #19
    Hyperactive Member Dmitri K's Avatar
    Join Date
    Sep 2002
    Location
    West Palm Beach, FL
    Posts
    444

    Re: send message problem

    Hmm, maybe wm_settext doesn't work with msword.

    As far as I can think right now, keyb_event is your only option, but someone may know a way.

    Post the code if not a problem. I'll tinker with it.

  20. #20
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: send message problem

    Quote Originally Posted by Dmitri K
    Hmm, maybe wm_settext doesn't work with msword.

    As far as I can think right now, keyb_event is your only option, but someone may know a way.

    Post the code if not a problem. I'll tinker with it.
    Problem is you need the window to be active


    Has someone helped you? Then you can Rate their helpful post.

  21. #21

    Thread Starter
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: send message problem

    dmitri, the code is the same as i posted earlier, with 2 changes
    1 change to sendmessagebystring
    2 set the form to hide during the paste process


    as i am starting the process by clicking a listbox item, so i don't know if a k_board event would help.

    i initially wrote this for a specific program, but now i would like it to work with as any as possible including visual basic ide.

    thanks p.

  22. #22
    Hyperactive Member Dmitri K's Avatar
    Join Date
    Sep 2002
    Location
    West Palm Beach, FL
    Posts
    444

    Re: send message problem

    Hmm, well this is the only way I found this to work.


    VB Code:
    1. newtext = "test"
    2. SetForegroundWindow wind 'wind is your msword handle
    3. For x = 1 To Len(newtext)
    4.   vk = VkKeyScan(Asc(Mid(newtext, x, 1))) And &HFF
    5.   keybd_event vk, 0, 0, 0
    6.   keybd_event vk, 0, 2, 0
    7. Next x

  23. #23

    Thread Starter
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: send message problem

    thanks to all for help on this

    i finally found why it didn't work

    the sendmessage of any type sent to MsWord and some other programs set the window text (that is the caption). in word it doesn't show at all, but in vb6 you could actually see caption of the code window reset.

    i now have the program working exact how i wanted, no api at all, just using sendkeys

    rgds pete

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