Okay, so i have the handle of a every window that i need to use. How do i find a textbox on that window and send some text to it?
Printable View
Okay, so i have the handle of a every window that i need to use. How do i find a textbox on that window and send some text to it?
:)VB Code:
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" _ (ByVal hWndParent As Long, _ ByVal hWndChildAfter As Long, _ ByVal lpszClassName As String, _ ByVal lpszWindowText 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 Private Const WM_SETTEXT As Long = &HC ' Find text box ----------------- Dim hWndTextbox As Long hWndTextbox = FindWindowEx(hWndParent, 0&, [TextboxClassName], vbNullString) ' OR -- hWndTextbox = FindWindowEx(hWndParent, 0&, vbNullString, [TextboxText]) ' Send it some text ------------- SendMessage hWndTextbox, WM_SETTEXT, -1, ByVal CStr("Your text goes here")
thanks..but it isnt exactly working.
VB Code:
Private Sub Command_Click() Dim i As Integer For i = 0 To List.ListCount - 1 Dim hWndTextbox As Long hWndTextbox = FindWindowEx(Val(List.List(i)), 0&, "trillian display", vbNullString) SendMessage hWndTextbox, WM_SETTEXT, -1, ByVal CStr("Your text goes here") Next i End Sub
this listbox contains the hWnds from when i get them
(It is being found, hwndtextbox does get a value)
Sorry, I had the SendMessage wrong, it should be
VB Code:
SendMessage hWndTextbox, WM_SETTEXT, [hl]0&[/hl], ByVal CStr("Your text goes here")
still not working with that : /
It works with Notepad. Are you sure you have the class name right? Remember it's case sensitive.
well actually. what ive discovered is this:
when i open up Spy++ and look for the textbox, it has been changed from "trillian display" trillian display to "Your text goes here" trillian display
Now, im not sure whats going on!
But it seems that i have 2 textboxes on this form with the same name and same classname!How do i send a message to the other one?
yeah thats it, ive sent the message to the textbox that your not supposed to be able to write in i think..
Yeah that never really works :p
To find the other one your gonna have to enumerate the child windows. Unfortunately since that uses a callback function there's no real way to tell when the enumeration has finished, so it's a bit hard to incorporate that into a function. I post an example in asec anyway.
yeah rofl : )
hopefully that will work ^^
If you still the need help, check the application we stripped down for getting all the handles. It has the ability to place text in textboxes or change captions of command buttons etc...
did it really? how would i get to that option, ill just re-download it and find that function
Well for what its worth here's mine anyway.
Paste this into a module (Needs to be .bas cos of the callback)
Now use it like this:VB Code:
Option Explicit Private Declare Function EnumChildWindows Lib "user32" _ (ByVal hWndParent As Long, _ ByVal lpEnumFunc As Long, _ ByVal lParam As Long) _ As Long Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" _ (ByVal hWnd As Long, _ ByVal lpClassName As String, _ ByVal nMaxCount As Long) _ As Long Private mstrSearchClassName As String Private mahWndChildWindows() As Long ' Public Function FindChildWindows(ByVal hWndParent As Long, _ ByVal pstrChildClassName As String) _ As Long() ' Find all child windows of a particular class name ' IN: hWndParent: Handle of the window in which to search ' IN: pstrChildClassName: Class name to search for ' RETURNS: Array of Longs containing the handles to _ the child windows found ReDim mahWndChildWindows(0) mstrSearchClassName = pstrChildClassName EnumChildWindows hWndParent, AddressOf EnumChildProc, 0 FindChildWindows = mahWndChildWindows End Function ' Public Function EnumChildProc(ByVal hWnd As Long, _ ByVal lParam As Long) _ As Long Static sblnArrayHasZeroMember As Boolean Dim strClassName As String Dim strWindowText As String ' Get the class name of the child window strClassName = Space(255) GetClassName hWnd, strClassName, 255 strClassName = Trim$(Left$(strClassName, InStr(1, strClassName, vbNullChar) - 1)) ' Is it what we're looking for? If (strClassName = mstrSearchClassName) Then ' Add it to the array If (sblnArrayHasZeroMember) Then ReDim Preserve mahWndChildWindows(UBound(mahWndChildWindows) + 1) mahWndChildWindows(UBound(mahWndChildWindows)) = hWnd Else mahWndChildWindows(0) = hWnd sblnArrayHasZeroMember = True End If End If ' Continue enumeration if possible EnumChildProc = 1 End Function
Here's an example of using it, you need a listbox and a command box for this one. This will find the window handles of all the toolbars on the taskbar.Code:array_of_hWnds = FindChildWindows(hWndParent, [Child Classname])
VB Code:
Private Sub Command1_Click() Dim i As Long, ahWndbuf() As Long ahWndbuf = FindChildWindows(FindWindow("Shell_TrayWnd", vbNullString), "ToolbarWindow32") For i = 0 To UBound(ahWndbuf) List1.AddItem ahWndbuf(i) Next i End Sub
HTH :)
wow thanks penagate. I really appreciate it, but since last post ive figured it out with my own code. Im going to test both, see which one does better.
thanks alot
No worries :)
Can you post your code? if it doesnt use enumeration I'd like to see how you did it :)
man i gotta start over : (
i deleted how i got the classes!!iugh
:lol: I know the feeling :(
got it this time, let me clean it up a bit
Okay here:
In a form add a 2 command buttons, and a listbox
VB Code:
Option Explicit Private Sub Command_Click() Dim i As Integer, Num As Long, myLong As Long For i = 0 To List1.ListCount - 1 Num = (List1.List(i)) ICount = 1 myLong = EnumChildWindows(Num, AddressOf WndEnumChildProc, vbNullString) Next i End Sub Private Sub Command2_Click() On Error Resume Next Dim myLong As Long VCount = 1 myLong = EnumWindows(AddressOf WndEnumProc, List1) End Sub
In a module:
now im just going to send a message to each oneVB Code:
Option Explicit 'APIs : WHERE THE REAL POWER IS 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 Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long Public Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long Public Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Any) As Long Public Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long Public Declare Function EnumChildWindows Lib "user32" (ByVal hwndParent As Long, ByVal lpEnumFunc As Long, ByVal lParam As Any) As Long 'showing window Public Const WM_SETTEXT = &HC 'Setting text of child window Public Const WM_GETTEXT = &HD 'Getting text of child window Public Const WM_GETTEXTLENGTH = &HE Public VCount As Integer, ICount As Integer Public SpyHwnd As Long '''''''''''''''''''''''' Public Function WndEnumProc(ByVal hwnd As Long, ByVal lParam As ListView) As Long Dim WText As String * 512 Dim bRet As Long, WLen As Long Dim WClass As String * 50 WLen = GetWindowTextLength(hwnd) bRet = GetWindowText(hwnd, WText, WLen + 1) GetClassName hwnd, WClass, 50 If InStr(WClass, "icoPMsgAIM") Or InStr(WClass, "icoPMsgMSN") Or InStr(WClass, "icoPMsgYAHOO") Or InStr(WClass, "icoPMsgICQ") Then Form1.List1.AddItem hwnd End If WndEnumProc = 1 End Function '''''''''''''''''''''''' Public Function WndEnumChildProc(ByVal hwnd As Long, ByVal lParam As ListView) As Long Dim bRet As Long Dim myStr As String * 50 bRet = GetClassName(hwnd, myStr, 50) 'if you want the text for only Edit class then use the if statement: 'If (Left(myStr, 4) = "Edit") Then 'lParam.Sorted = False If InStr(myStr, "trillian display") Then MsgBox Str(hwnd) End If '''''''''MsgBox "HWND:" & Str(hwnd) & " " & "CLASSNAME:" & myStr & "TEXT:" & GetText(hwnd) ICount = ICount + 1 'lParam.Sorted = True 'End If WndEnumChildProc = 1 End Function Function GetText(iHwnd As Long) As String Dim Textlen As Long Dim Text As String Textlen = SendMessage(iHwnd, WM_GETTEXTLENGTH, 0, 0) If Textlen = 0 Then GetText = ">No text for this class<" Exit Function End If Textlen = Textlen + 1 Text = Space(Textlen) Textlen = SendMessage(iHwnd, WM_GETTEXT, Textlen, ByVal Text) 'The 'ByVal' keyword is necessary or you'll get an invalid page fault 'and the app crashes, and takes VB with it. GetText = Left(Text, Textlen) End Function
I see how it works, cool :)
Okay, im having trouble with sendmessage again :blush:
VB Code:
Option Explicit 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 Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long Public Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hWnd As Long) As Long Public Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Any) As Long Public Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hWnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long Public Declare Function EnumChildWindows Lib "user32" (ByVal hWndParent As Long, ByVal lpEnumFunc As Long, ByVal lParam As Any) As Long Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" _ (ByVal hWndParent As Long, _ ByVal hWndChildAfter As Long, _ ByVal lpszClassName As String, _ ByVal lpszWindowText As String) _ As Long 'showing window Public Const WM_SETTEXT = &HC 'Setting text of child window Public Const WM_GETTEXT = &HD 'Getting text of child window Public Const WM_GETTEXTLENGTH = &HE Public i As Integer Public VCount As Integer, ICount As Integer Public SpyHwnd As Long, hWndTextbox As Long '''''''''''''''''''''''' Public Function WndEnumProc(ByVal hWnd As Long, ByVal lParam As ListView) As Long Dim WText As String * 512 Dim bRet As Long, WLen As Long Dim WClass As String * 50 WLen = GetWindowTextLength(hWnd) bRet = GetWindowText(hWnd, WText, WLen + 1) GetClassName hWnd, WClass, 50 If InStr(WClass, "icoPMsgAIM") Or InStr(WClass, "icoPMsgMSN") Or InStr(WClass, "icoPMsgYAHOO") Or InStr(WClass, "icoPMsgICQ") Then Form1.List1.AddItem hWnd End If WndEnumProc = 1 End Function '''''''''''''''''''''''' Public Function WndEnumChildProc(ByVal hWnd As Long, ByVal lParam As ListView) As Long Dim bRet As Long Dim myStr As String * 50 bRet = GetClassName(hWnd, myStr, 50) 'if you want the text for only Edit class then use the if statement: 'If (Left(myStr, 4) = "Edit") Then 'lParam.Sorted = False If InStr(myStr, "trillian display") Then [B][COLOR=Red]hWndTextbox = FindWindowEx(hWnd, 0&, "trillian display", vbNullString)[/COLOR][/B] ' OR -- 'hWndTextbox = FindWindowEx(Form1.List1(i), 0&, vbNullString, [TextboxText]) ' Send it some text ------------- SendMessage hWndTextbox, WM_SETTEXT, 0&, ByVal CStr("Your text goes here") 'MsgBox Str(hWnd) End If '''''''''MsgBox "HWND:" & Str(hwnd) & " " & "CLASSNAME:" & myStr & "TEXT:" & GetText(hwnd) ICount = ICount + 1 'lParam.Sorted = True 'End If WndEnumChildProc = 1 End Function Function GetText(iHwnd As Long) As String Dim Textlen As Long Dim Text As String Textlen = SendMessage(iHwnd, WM_GETTEXTLENGTH, 0, 0) If Textlen = 0 Then GetText = ">No text for this class<" Exit Function End If Textlen = Textlen + 1 Text = Space(Textlen) Textlen = SendMessage(iHwnd, WM_GETTEXT, Textlen, ByVal Text) 'The 'ByVal' keyword is necessary or you'll get an invalid page fault 'and the app crashes, and takes VB with it. GetText = Left(Text, Textlen) End Function
hwndtextbox isnt getting a value, what am i doing wrong?
edit***i already have the hwnd
Well you're enumming through the child windows at that point, which means if you have successfully got to that line you have already found your textbox, therefore
;)VB Code:
hWndTextbox = hWnd
thankyou, that works!
but it isnt sending the text to the textbox, but it does change its name in the SPY++ ??i dont understand whats going on
aha, its changing the textboxs Caption, not its text?
where are all the api gods??
Right here, ya'll. (I'm not really, but I am pretty good with FindWindow and SendMessage)
I don't use Trillian, so I don't know any of the classnames, but I DO use AIM, so if you need to know anything about that PM me.
BTW, Trillian has a security flaw, the flaw allows people (the people who know how to exploit it) to look through all of your files. Try Gaim.
-Sir Loin
lol what are they going to do looking thru my files : )..i dont really care rofl..
I have the classname right, but instead of sending the text to the textbox its sending the text to the textboxes caption?? im not sure how to explain, if you want a screenshot ill send one
That doesn't make sense, it changes the classname? :confused:
Yep, a screenshot sounds good :D
im not sure what its changing :confused:
Before and after are the two pictures...
if you want the code again ill post it
Nothing seems to be changing so I guess the text isn't being set... But I doubt that the classname is changing... If you refresh Spy++ does it show "trillian display" or something else after you run the code? If you type something in and you try to get the text does it get it? Are you sure it's the right control you want?
my bad. i uploaded the wrong second picture. here it is
That's really odd... Sorry, but I have no clue :(
Oh, wait, it changes the title of the trillian display control... Do a FindWindowEx on the trillian display for the classname "edit"... Maybe there is a textbox inside the trillian display control (like in a frame)? :ehh:
i dont really understand what you mean...
can you explain what i should do a little more,or fix it in this rar for me..
like this?:
it is returning something, but nothing is happening :confused: :confused:VB Code:
If InStr(myStr, "trillian display") Then hWndTextbox = hWnd ' OR -- 'hWndTextbox = FindWindowEx(Form1.List1(i), hWnd, vbNullString, [TextboxText]) hWndTextbox = FindWindowEx(Form1.List1.List(i), 0&, "trillian display", vbNullString) ' Send it some text ------------- SendMessage hWndTextbox, WM_SETTEXT, 0&, ByVal CStr("Hi SMarterchild") 'MsgBox Str(hWnd)
Actually that wouldn't work now that I think of it again... If it had any child windows it would have a "+" sign beside it in Spy++ :(
My idea was that FindWindow gets you the hwnd of the main window and with FindWindowEx you can get the handle of a child in a specific window... In a child of a window, you can have another child. So I was thinking that maybe they had a textbox in the trillian display control of theirs and "edit" is the standard classname for a textbox (I think)... So doing a FindWindowEx with the hwnd of the trillian display and looking for a "edit" class, if there was a textbox as a child, you would get the handle of it.
I hope I didn't mix you up :)
But that won't work and I'm running out of ideas... Only other thing that comes to mind and is a completely other approach is SendKeys (or the keyb_event API) :(
What I was thinking of (and it won't work) was this :
VB Code:
hWndTextbox = FindWindowEx(hWndTextbox, 0&, "edit", vbNullString)
i cant believe it wont work, this seems like its all 100% right...im not really into sendkeys..but i guess as a last resort <_<...Is it possible it blocked access to it or something??
It's some sort of their own textbox I assume :( So it won't accept the same messages as the normal textbox that we're thinking of... I hope someone else can come up with something, although I'm afraid it's not possible... :(
what other things would be plausible? like what arguments could i try and pass to it?