Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

FindWindowLike

Status
Not open for further replies.

Necromis

Technical User
Jun 15, 2007
14
US
Ok, I am lost on this function. I know it worked prior to vb.net. I currently am using VB Express 2005. I know how to use the normal findwindow and sendmessage API calls. However, I have an application that has various versions so the title bar has different version numbers after the Main title. I need to know what the proper structure is to use the findwindowlike function. I have searched high and low and cannot find any good examples of how to do this and how it works. If you can use notepad in the example you supply that would be great. Since I am using notepad to test prior to using on alternate application.
 
Look into the "System.Diagnostics.Process" class


________________________________________________________
Zameer Abdulla
Help to find Missing people
 
Here is the function:

Public Const GW_HWNDNEXT = 2
Public Const GW_CHILD = 5

Public Declare Function GetWindow Lib "user32" _
(ByVal hwnd As Integer, _
ByVal wCmd As Integer) As Integer

Public Declare Function GetClassName Lib "user32" (ByVal hwnd As IntPtr, ByVal lpClassName As String, ByVal nMaxCount As Integer) As Integer

Public Declare Function GetDesktopWindow Lib "user32" () As Integer

Public Declare Function GetWindowText Lib "user32" _
Alias "GetWindowTextA" _
(ByVal hwnd As Integer, _
ByVal lpString As String, _
ByVal cch As Integer) As Integer

Private Function FindWindowLike(ByVal hWndStart As Integer, _
ByVal WindowText As String, _
ByVal Classname As String) As Integer

Dim hwnd As Integer
Dim sWindowText As String
Dim sClassname As String
Dim r As Integer

'Hold the level of recursion and
'hold the number of matching windows
Static level As Integer

'Initialize if necessary. This is only executed
'when level = 0 and hWndStart = 0, normally
'only on the first call to the routine.
If level = 0 Then
If hWndStart = 0 Then hWndStart = GetDesktopWindow()
End If

'Increase recursion counter
level = level + 1

'Get first child window
hwnd = GetWindow(hWndStart, GW_CHILD)

Do Until hwnd = 0

'Search children by recursion
Call FindWindowLike(hwnd, WindowText, Classname)

'Get the window text and class name
sWindowText = Space$(255)
r = GetWindowText(hwnd, sWindowText, 255)
sWindowText = sWindowText.Substring(0, r)
'sWindowText = Left(sWindowText, r)

sClassname = Space$(255)
r = GetClassName(hwnd, sClassname, 255)
sClassname = sClassname.Substring(0, r)
'sClassname = Left(sClassname, r)

'Check if window found matches the search parameters
If (sWindowText Like WindowText) And _
(sClassname Like Classname) Then

'List1.AddItem(hwnd & vbTab & _
' sClassname & vbTab & _
' sWindowText)
FindWindowLike = hwnd

'uncommenting the next line causes the routine to
'only return the first matching window.
'Exit Do

End If

'Get next child window
hwnd = GetWindow(hwnd, GW_HWNDNEXT)

Loop

'Reduce the recursion counter
level = level - 1

End Function

To use it as follows:
FindWindowLike(0, "*nameurlookingfor*", "*")

This should give you a good start.
 
Thanks I will give that code a try. It is a simple gui I am trying to make, but the handle is a pain when the name can be "Program Name 1.x." Hopefully this will allow me to grab the right window.
 
running into an exception with one of your api calls. Note I have a class where I store all of the API functions called Win32API. Here is the exception thrown.

System.EntryPointNotFoundException was unhandled
Message="Unable to find an entry point named 'GetClassName' in DLL 'user32'."
 
ok, I found the issue with that. The API in your code was not complete and it seems to work fine so far. Now to do some testing with the hwnd value and sendmessage.
 
I would use PostMessage, not SendMessage; in that way your program does not wait for a response from the SendMessage. If you use SendMessage, your program will halt until a response is given back. If no response is given back, your program will be hung. PostMessage sends the message and puts it in a queue to be processed. Your program won't wait and it will prevent it from hanging.
 
I have a strange occurance with this when I ran it a second time. It seems to jump out of the function/loop w/o finding the window. I may just also use sendkeys with an appactivate.
 
What I was suggesting
Code:
If System.Diagnostics.Process.GetProcessesByName("Notepad").Length > 0 Then
      MessageBox.Show("Notepad is running")
Else
      MessageBox.Show("Notepad is NOT running")
End If

________________________________________________________
Zameer Abdulla
Help to find Missing people
 
well I was able to modify the code I got from a MSDN API example to do what I need. Essentially it fills a list box with all the running applications. I then run this list box thru a like comparision till I find the application I am wanting, and then assign that indexes value as a string to a variable which I then use for my findwindow and findwindowex calls for my sendmessage calls. It works like a charm with notepad and with minor tweaks should work fine with the other appliction I am using it for. Thanks for helping guys. You really have pointed me in the right direcions.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top