Wildcard Strings

  • Thread starter Thread starter Daniel N
  • Start date Start date
D

Daniel N

hWnd = FindWindow("CLASS NAME", *)



A string must be where the (*) is. Is there a way of making some kind of
wildcard so that function accepts every string sting?
 
Sorry, I meant:

A string must be where the (*) is. Is there a way of making some kind of
wildcard so that function accepts every string Instance?
 
Daniel said:
hWnd = FindWindow("CLASS NAME", *)

I presume that you are trying to find all windows with a certain class
name. You can accomplish this using the EnumWindows API along with the
GetClassName API.
 
Nathaniel said:
I googled and found :http://www.vbaccelerator.com/home/vb/code/Libraries/Windows/Enumerating_Windows/article.asp
But still have no idea what do.

Should it be something like this?

Public Declare Function EnumWindows Lib "user32" ( _
ByVal lpEnumFunc As Long, _
ByVal lparam As Long _
) As Long ???
hWnd = FindWindow("CLASS NAME", EnumWindowsLong)

When you call EnumWindows, it will call the lpEnumFunc that you provide
for every window. You must implement this function and inside it you
can check if the hWnd is of the classname that you want and react
accordingly.

Here is some code from www.pinvoke.net, translated to VB:

Imports System.Runtime.InteropServices
Imports System.Text

Public Class Form1

Public Delegate Function CallBackPtr(ByVal hwnd As IntPtr, ByVal
lParam As Integer) As Boolean
Declare Auto Function EnumWindows Lib "user32.dll" (ByVal callPtr
As CallBackPtr, ByVal lPar As Integer) As Integer
Declare Auto Sub GetClassName Lib "user32.dll" (ByVal hWnd As
IntPtr, ByVal className As StringBuilder, ByVal maxCount As Integer)

Private sb As StringBuilder

Public Function Report(ByVal hWnd As IntPtr, ByVal lParam As
Integer) As Boolean

Dim className As New StringBuilder("", 256)

GetClassName(hWnd, className, 256)

sb.AppendFormat("Window Handle: {0,-15} Class Name: {1}" &
vbCrLf, hWnd, className.ToString)

Return True
End Function

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
sb = New StringBuilder()

EnumWindows(AddressOf Report, 0)

If sb.Length > 0 Then
MessageBox.Show(sb.ToString)
End If
End Sub

End Class


In other words, when you call EnumWindows, for each window handle,
Windows will call your function (Report). Inside that function, you
use the GetClassName function to get the name of the window class. You
can then compare the class name with another string and react
accordingly.

Hope this helps.
 
Nathaniel said:
What do I put in:
AdressOfPointer

?

The code example I posted is complete. I tested it before I posted it.
'AddressOf' is a Visual Basic keyword.

The code works like this:

The first argument to EnumWindows is basically a pointer to a function.
We use the AddressOf keyword in Visual Basic to get this. So for this
line of code:

EnumWindows(AddressOf Report, 0)

We are telling Windows to get all the window handles and for each
window handle, call the function called "Report". So if there are 40
windows, then the Report function will be called 40 times.

The "Report" function is like this:

Public Function Report(ByVal hWnd As IntPtr, ByVal lParam As
Integer) As Boolean

Dim className As New StringBuilder("", 256)

GetClassName(hWnd, className, 256)

sb.AppendFormat("Window Handle: {0,-15} Class Name: {1}" &
vbCrLf, hWnd, className.ToString)

Return True
End Function

Every time Windows calls it, the hWnd parameter contains the window
handle to the window that is currently being operated on. So if we had
40 windows open at the time and they were numbered 1 - 40, then the
Report function would be called 40 times and each time a different hWnd
(1 - 40) would be passed in. In other words, Windows will call Report
once for each window in the system.

I suggest that you copy that code into a blank form and run it. Place
a breakpoint inside the Report function to see what it is doing.

Chris
 
Back
Top