|
-
May 3rd, 2008, 05:44 PM
#1
Thread Starter
Frenzied Member
[2005] Findwindow API
how does the FindWindow API function work? how do i call it and use it?
-
May 3rd, 2008, 05:51 PM
#2
Re: [2005] Findwindow API
if you paste Findwindow into the search box on this forum, you will find stacks of examples what are you wanting to accomplish with Findwindow?
~
if a post is resolved, please mark it as [Resolved]
protected string get_Signature(){return Censored;}
[vbcode][php] please use code tags when posting any code [/php][/vbcode]
-
May 3rd, 2008, 06:08 PM
#3
Thread Starter
Frenzied Member
Re: [2005] Findwindow API
I wanted to use the FindWindow API to get a window of Another program and insert text into it if it was say a textbox.
-
May 4th, 2008, 02:07 AM
#4
Hyperactive Member
Re: [2005] Findwindow API
No problem:
Code:
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Integer
Declare Function SetWindowText Lib "user32.dll" Alias "SetWindowTextA" (ByVal hwnd As Int32, ByVal lpString As String) As Int32
Dim window as int32
window = Findwindow("Classname here", "Caption here")
SetWindowText(window, textbox1.text)
Textbox1.text is the text the new caption will be
-
May 4th, 2008, 02:25 AM
#5
Re: [2005] Findwindow API
you may notice they said they want to set the text to a textbox of another app. won't do that with SetWindowText, that requires the use of WM_SETTEXT with SendMessage.
~
if a post is resolved, please mark it as [Resolved]
protected string get_Signature(){return Censored;}
[vbcode][php] please use code tags when posting any code [/php][/vbcode]
-
May 4th, 2008, 02:36 AM
#6
Hyperactive Member
Re: [2005] Findwindow API
Oh woops my bad, thought he wanted to set the window caption.
Hmmm in that case you are going to have to use Findwindow to get the window handle then Findwindowex (maybe more than once) to get the textbox or whatever control you want's handle then as dynamic sysop said use SendMessage with the constant WM_SETTEXT to send it text. There are a number of constants you can use with sendmessage and they do a lot of different things.
Heres a handy tool for getting constants:
http://allapi.mentalis.org/agnet/apiviewer.shtml
Heres a handy tool for getting window captions, handles classnames etc.:
http://www.softpedia.com/get/Program...rs/WinID.shtml
-
May 4th, 2008, 11:54 AM
#7
Thread Starter
Frenzied Member
Re: [2005] Findwindow API
I have been using VB .NET for a while, i used a couple API functions.. here and there but i got them on different websites that just said this and that does this. I never really understood how to write a API so by the link you provided i completly dont know anything about API therefore i cannot do anything yet. Can someone explain the functions of API and how it works etc... (Should i put this in a new thread?)
-
May 4th, 2008, 01:06 PM
#8
Re: [2005] Findwindow API
i put a basic sample together for you, there are probably loads more on here also. open an instance of notepad & see what happens.
Code:
Public Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Int32
Public Declare Function FindWindowEx Lib "user32.dll" Alias "FindWindowExA" (ByVal hWnd1 As Int32, ByVal hWnd2 As Int32, ByVal lpsz1 As String, ByVal lpsz2 As String) As Int32
Public Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hwnd As Int32, ByVal wMsg As Int32, ByVal wParam As Int32, ByVal lParam As String) As Int32
Public Const WM_SETTEXT As Int32 = &HC
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim hWnd As Int32 = FindWindow("Notepad", vbNullString)
If hWnd > 0 Then
'/// we have the handle to an open instance of notepad
'/// now get the edit window ( the part that the text goes in )
Dim hWndChild As Int32 = FindWindowEx(hWnd, 0, "Edit", vbNullString)
'/// if the edit window returns as valid, we will attempt to add some text to notepad..
If hWndChild > 0 Then
Dim strText As String = "some text to add to notepad!"
SendMessage(hWndChild, WM_SETTEXT, 0, strText)
End If
End If
End Sub
hope it helps
~
if a post is resolved, please mark it as [Resolved]
protected string get_Signature(){return Censored;}
[vbcode][php] please use code tags when posting any code [/php][/vbcode]
-
May 4th, 2008, 06:39 PM
#9
Hyperactive Member
Re: [2005] Findwindow API
 Originally Posted by noahssite
I never really understood how to write a API so by the link you provided i completly dont know anything about API therefore i cannot do anything yet.
The program from the link i gave you above actually gives you the API declaration, you just need to know the API's name, search with it in the program and it will show you the declaration, copy-paste then done!
It probably comes with a readme.
-
May 4th, 2008, 07:12 PM
#10
Re: [2005] Findwindow API
 Originally Posted by noahssite
I have been using VB .NET for a while, i used a couple API functions.. here and there but i got them on different websites that just said this and that does this. I never really understood how to write a API so by the link you provided i completly dont know anything about API therefore i cannot do anything yet. Can someone explain the functions of API and how it works etc... (Should i put this in a new thread?)
The Windows API is a set of C/C++ DLLs that export various types, values and functions. Using the Declare key word or the DllImport attribute in VB.NET you are able to declare a function in your VB code that corresponds to the exported function in a C/C++ DLL, which may be part of the Windows API or not. When you call your method in code, at run time the system will reroute the call to the function in the unmanaged library.
Obviously the managed types available to you are different to the types used in unmanaged C/C++. You will always be able to find one, sometimes more, managed types to correspond to the unmanaged types in the original code. For instance, in dynamic_sysop's code he has used the Int32 type for all the hWnd parameters, which correspond to window handles. While this is quite acceptable, and was how it was always done in VB6, it is generally considered preferable to use the IntPtr type for handles in VB.NET. For instance, the Handle property of a form or control in VB.NET, which corresponds to the hWind property in VB6, is type IntPtr.
-
May 5th, 2008, 06:08 PM
#11
Thread Starter
Frenzied Member
Re: [2005] Findwindow API
 Originally Posted by cameron2
The program from the link i gave you above actually gives you the API declaration, you just need to know the API's name, search with it in the program and it will show you the declaration, copy-paste then done!
It probably comes with a readme.
So i use the program to get the API name. Say i want to insert text into the internet explore address bar to recieve the name of it i used the program..
-
May 5th, 2008, 06:45 PM
#12
Re: [2005] Findwindow API
 Originally Posted by noahssite
So i use the program to get the API name. Say i want to insert text into the internet explore address bar to recieve the name of it i used the program..
No. The API Viewer will give you the method signature, i.e. the declaration for the method that you add to your code that, when called, will be mapped to the actual function in the Windows API. It's still up to you to call that method and pass it the appropriate values in order to get the information you need. For instance, to get a top-level window handle you need to call FindWindow and pass the window title, which you can see in the title bar, and the class name, which you will have to use a tool like Spy++, WinID or Winspector to find. Also, have you done as suggested and searched the forum for examples? FindWindow is probably the most common API function because getting a top-level window handle is often the first step in any operation.
-
May 5th, 2008, 07:17 PM
#13
Thread Starter
Frenzied Member
Re: [2005] Findwindow API
vb Code:
Public Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Int32
Public Declare Function FindWindowEx Lib "user32.dll" Alias "FindWindowExA" (ByVal hWnd1 As Int32, ByVal hWnd2 As Int32, ByVal lpsz1 As String, ByVal lpsz2 As String) As Int32
Public Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hwnd As Int32, ByVal wMsg As Int32, ByVal wParam As Int32, ByVal lParam As String) As Int32
Public Const WM_SETTEXT As Int32 = &HC
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim hWnd As Int32 = FindWindow("Notepad", vbNullString)
If hWnd > 0 Then
'/// we have the handle to an open instance of notepad
'/// now get the edit window ( the part that the text goes in )
Dim hWndChild As Int32 = FindWindowEx(hWnd, 0, "Edit", vbNullString)
'/// if the edit window returns as valid, we will attempt to add some text to notepad..
If hWndChild > 0 Then
Dim strText As String = textbox1.text
SendMessage(hWndChild, WM_SETTEXT, 0, strText)
End If
End If
End Sub
Ok i looked at one of the programs and it seems extremly usefull. So where it says "notepad" in the above code i put the parent class name..? and where it says edit i put the other class. So i tried to do that with internet explorer, but it didnt seem to work heres the code i used tell me if i misunderstood it or did any errors.
vb Code:
Public Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Int32
Public Declare Function FindWindowEx Lib "user32.dll" Alias "FindWindowExA" (ByVal hWnd1 As Int32, ByVal hWnd2 As Int32, ByVal lpsz1 As String, ByVal lpsz2 As String) As Int32
Public Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hwnd As Int32, ByVal wMsg As Int32, ByVal wParam As Int32, ByVal lParam As String) As Int32
Public Const WM_SETTEXT As Int32 = &HC
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim hWnd As Int32 = FindWindow("ComboBox", vbNullString)
If hWnd > 0 Then
'/// we have the handle to an open instance of notepad
'/// now get the edit window ( the part that the text goes in )
Dim hWndChild As Int32 = FindWindowEx(hWnd, 0, "Edit", vbNullString)
'/// if the edit window returns as valid, we will attempt to add some text to notepad..
If hWndChild > 0 Then
Dim strText As String = textbox1.text
SendMessage(hWndChild, WM_SETTEXT, 0, strText)
End If
End If
End Sub
-
May 5th, 2008, 07:19 PM
#14
Thread Starter
Frenzied Member
Re: [2005] Findwindow API
Or could anyone edit the code and make it so textbox1.text is what will be displayed in the IE address bar....
-
May 5th, 2008, 07:51 PM
#15
Re: [2005] Findwindow API
First up, FindWindow finds a top-level window with a specific name and a specific class. You're not going to find an IE window like this:
Code:
FindWindow("ComboBox", vbNullString)
I'm fairly certain that "ComboBox" is not the class name for an IE window. Like I said, you should use a tool like WinID to determine the class names of the windows you want to access.
-
May 6th, 2008, 07:21 PM
#16
Thread Starter
Frenzied Member
Re: [2005] Findwindow API
I used WinID and under class it said a few things..ill check it again and reply in a few min.
-
May 6th, 2008, 07:42 PM
#17
Thread Starter
Frenzied Member
Re: [2005] Findwindow API
i dont understand it...probally.. When i put my cursor over the address bar it says the parent class is ComboBox and the class is Edit i think im doing it wrong since it doesnt work can someone give me an example of Entering text into IE Address bar? i wanna compare it to the notepad one and see what ive been doing wrong.
-
May 6th, 2008, 07:56 PM
#18
Re: [2005] Findwindow API
FindWindow will give you the handle of a top-level window only. Obviously a ComboBox can't be a top-level window. If you want to know the class name of the IE window itself then you have to put the cursor over the IE window itself, not one of its child windows. Make sure you have enabled the WinID feature that draws a border around the window that it's currently displaying information for. You then move the cursor until the border is drawn around the ENTIRE top-level window. You then get the class name of that window and that's what you need to pass to FindWindow.
Now, once that's done you need to call FindWindowEx as many times as is required to get from the top-level window down to the child window of interest. If the top-level window contains a ComboBox window which contains an Edit window, you need to call FindWindowEx twice. The first time you pass the top-level window's handle that you got from FindWindow and the second time you pass the ComboBox window's handle you got from the first call.
-
May 8th, 2008, 06:41 AM
#19
Thread Starter
Frenzied Member
Re: [2005] Findwindow API
Code:
Dim hWndChild As Int32 = FindWindowEx(hWnd, 0, "Edit", vbNullString)
If hWndChild > 0 Then
Dim strText As String = textbox1.text
SendMessage(hWndChild, WM_SETTEXT, 0, strText)
End If
So basicly i put that code in its self and keep on putting the code in that satement like i do in a If statement like this:
Code:
If a = true then
if b = true then
if c = true then
end if
end if
end if
So basicly like my if statement just with the proper commands Thankyou.
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
|