|
-
Dec 27th, 2004, 01:02 AM
#1
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:
Private Const WM_COPY = &H301
Private Const WM_PASTE = &H302
Private Const WM_SETTEXT As Long = &HC
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
Private Declare Function GetActiveWindow Lib "user32" () As Long
Private Sub List1_Click(index As Integer)
Dim newtext As String
newtext = List1(index).Text
Debug.Print Me.hwnd
Me.Hide
wind = GetActiveWindow
Debug.Print wind
Me.Show
' hwind for text box on program i was trying to paste to, detected by apispy
'wind = 329952
SendMessage wind, WM_SETTEXT, 0&, List1(index).Text
List1(index).SetFocus
End Sub
Last edited by westconn1; Jan 23rd, 2005 at 05:27 AM.
-
Dec 27th, 2004, 01:07 AM
#2
Frenzied Member
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++
-
Dec 27th, 2004, 01:13 AM
#3
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Dec 27th, 2004, 01:14 AM
#4
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Dec 27th, 2004, 01:15 AM
#5
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.
-
Dec 27th, 2004, 01:20 AM
#6
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
-
Dec 27th, 2004, 01:33 PM
#7
Frenzied Member
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
-
Dec 27th, 2004, 10:24 PM
#8
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:
Public Sub Paste(ByVal hwnd As Long)
SendMessage hwnd, WM_PASTE, 0&, ByVal 0&
Debug.Print "hwnd "; hwnd; " gettickcount "; GetTickCount
End Sub
Private Function gethwnd()
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 Function
Private Sub List1_Click(index As Integer)
Dim newtext As String
newtext = List1(index).Text
Clipboard.Clear
Clipboard.SetText List1(index).Text
Me.Hide
gethwnd
Me.Show
List1(index).SetFocus
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.
-
Dec 31st, 2004, 02:01 AM
#9
-
Dec 31st, 2004, 07:56 PM
#10
Frenzied Member
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
-
Dec 31st, 2004, 08:30 PM
#11
-
Dec 31st, 2004, 09:07 PM
#12
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
-
Dec 31st, 2004, 09:08 PM
#13
Frenzied Member
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
-
Dec 31st, 2004, 09:40 PM
#14
-
Jan 1st, 2005, 12:25 AM
#15
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.
-
Jan 3rd, 2005, 06:48 PM
#16
-
Jan 3rd, 2005, 07:46 PM
#17
Hyperactive Member
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
-
Jan 3rd, 2005, 08:06 PM
#18
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
-
Jan 3rd, 2005, 08:36 PM
#19
Hyperactive Member
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.
-
Jan 3rd, 2005, 08:52 PM
#20
-
Jan 3rd, 2005, 09:41 PM
#21
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.
-
Jan 4th, 2005, 07:47 PM
#22
Hyperactive Member
Re: send message problem
Hmm, well this is the only way I found this to work.
VB Code:
newtext = "test"
SetForegroundWindow wind 'wind is your msword handle
For x = 1 To Len(newtext)
vk = VkKeyScan(Asc(Mid(newtext, x, 1))) And &HFF
keybd_event vk, 0, 0, 0
keybd_event vk, 0, 2, 0
Next x
-
Jan 23rd, 2005, 05:26 AM
#23
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|