Enum Windows & SendMessage API

EnumWindows

EnumWindows enumerate (returns) handles of all the top-level windows. Now most of the times the enumeration, whether its of parent windows or child windows or fonts etc, is done with the help of a ‘callback’ function. You need not overwhelm yourself with the word ‘callback’, its not a big thing actually. It’s like this: the EnumWindows will pass the handles of all the top-level windows that it’ll find to the callback function which ‘you’ will write yourself. Its easy, in fact its declaration already is available to you in the API documentation in the MSDN library. Your program can have several callback functions, but how does EnumWindows know which callback function to pass the handles to? You manage it yourself by giving it the ‘address’ of the callback function that you want to use with EnumWindows, and you get the address of the function with the help of a especial operator called ‘AddressOf’ operator (it was first time introduced in VB5). Following is the declaration of EnumWindows as given in the API viewer:

Public Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long

The parameter ‘lpEnumFunc’ requires the address of the function that you’ll will be using as a callback.

The parameter ‘lParam’ is an application-defined value. This value is of no use to EnumWindows and it is just there for you to pass any value you want to the callback function. Although it is of type long here but it can be of any type you want, you’ll have to change the type in the declaration likewise, like I wanted to pass a listview control to the callback so I changed the type to ‘Any”, this will be explained later on.

Now the callback function. It is not declared with the ‘Declare’ statement, you would write it like any other function but you’ll have to observe the number of arguments it can receive which is also given in the MSDN library. It’s normally defined as:

Public Function WndEnumProc(ByVal hwnd As Long, ByVal lParam As Long) As Long
End Sub

hwnd is the handle of the window that EnumWindow passes it.

lParam is the user-defined value, but because I was passing it a ListView control so I changed the lParam type as ‘ListView’ so that it could accept a ListView object.

Note: you must place the callback function in the ‘Module’ i.e. the *.bas file because it’s a condition of the AddressOf operator that the function whose address is passed should be located in the *Bas file.

Also AddressOf can only be used as argument of any function like:

x = somefunction(AddressOf anyuserdefinedfunction)

you cant write it as:

x = addressof(anyvariableoruserdefinedfunction) ‘ this is illegal

Once you’ve called the EnumWindows function and given it the address of the callback function, it will start passing the handles of all the windows that it’ll find. When the execution reaches inside the callback function you can do whatever you want with the passed on data but in the last line of the function you must ‘tell’ the EnumWindows whether it should continue passing the handles or not. If you want EnumWindows to keep on passing you the handles you return 1 else you return 0. In EnumerationX I wanted EnumWindows to pass me all the handles it found so I kept returning 1 until it stopped itself.

In EnumerationX, when inside the WndEnumProc (callback) I took the handle, got the caption of the window through GetWindowText and filled the listview control with the window names.

EnumChildWindows is exactly the same function as EnumWindows except that it enumerates child window handles. I hope that through the explanation of EnumWindows you’ll be easily able to understand the working of EnumChildWindows.

You might also like...

Comments

About the author

Muhammad Abubakar United States

Nothing to say anything about me yet.

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“In order to understand recursion, one must first understand recursion.”