7 out of 26 rated this helpful - Rate this topic

EnumWindows function

Applies to: desktop apps only

Enumerates all top-level windows on the screen by passing the handle to each window, in turn, to an application-defined callback function. EnumWindows continues until the last top-level window is enumerated or the callback function returns FALSE.

Syntax

BOOL WINAPI EnumWindows(
  __in  WNDENUMPROC lpEnumFunc,
  __in  LPARAM lParam
);

Parameters

lpEnumFunc [in]

Type: WNDENUMPROC

A pointer to an application-defined callback function. For more information, see EnumWindowsProc.

lParam [in]

Type: LPARAM

An application-defined value to be passed to the callback function.

Return value

Type:

Type: BOOL

If the function succeeds, the return value is nonzero.

If the function fails, the return value is zero. To get extended error information, call GetLastError.

If EnumWindowsProc returns zero, the return value is also zero. In this case, the callback function should call SetLastError to obtain a meaningful error code to be returned to the caller of EnumWindows.

Remarks

The EnumWindows function does not enumerate child windows, with the exception of a few top-level windows owned by the system that have the WS_CHILD style.

This function is more reliable than calling the GetWindow function in a loop. An application that calls GetWindow to perform this task risks being caught in an infinite loop or referencing a handle to a window that has been destroyed.

Requirements

Minimum supported client

Windows 2000 Professional

Minimum supported server

Windows 2000 Server

Header

Winuser.h (include Windows.h)

Library

User32.lib

DLL

User32.dll

See also

Reference
EnumChildWindows
EnumWindowsProc
GetWindow
Conceptual
Windows

 

 

Send comments about this topic to Microsoft

Build date: 2/3/2012

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
C# syntax

      
private delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern bool EnumWindows(EnumWindowsProc callback, IntPtr extraData);

      
    

vb.net syntax
<DllImport("user32.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
Public Shared Function EnumWindows(ByVal callback As EnumThreadWindowsCallback, ByVal extraData As IntPtr) As Boolean End Function
enum windows fails in a scheduled task...

I have a program that is looking for another program to communicate with. It works if you run it from the desktop (explorer), but if I schedule it as a schedule task, enumwindows() only calls the callback function once, then no more.

In the below code, g_hProc is getting set correctly, regardless of the code running as a normal app or a scheduled task. However, g_hWnd never gets set, because enumWindowProc() only gets called once. It properly enumerates if run as a regular app???? (Note: g_appTitle is the name of the window we're looking for, and this object is basically a version of CString.)

If anybody has a fix, please email me at bret @ levycodev dot com.

Thanks,

Bret

BOOL CALLBACK enumWindowsProc (HWND hwnd, LPARAM lParam) {

DWORD procid;

GetWindowThreadProcessId (hwnd, &procid);

if ((HANDLE)procid == g_hProc) { staticchar module[1024];

module[0] = 0;

if (g_softPhoneTitle.Size() > 0) { int rc = GetWindowText (hwnd, module, 1023);

module[rc] = 0;

}

if (IsWindow(hwnd) && ((g_appTitle.Size() == 0) || (g_appTitle.EqualsNoCase(module)))) {

g_hWnd = hwnd;

return (false);

}

}

return (true);

}

int

findApplicationWindow (void) {

g_hWnd = NULL;

EnumWindows (enumWindowsProc, 0);

return (0);

}

Possible VB9 declaration
Friend Declare Function EnumWindows Lib "user32.dll" (ByVal lpEnumFunc As EnumWindowsProc, ByVal lParam As Int32) As Int32

Example:

''' <summary>Gets all the top-level windows</summary>
''' <returns>List of all top-level windows</returns>
''' <exception cref="API.Win32APIException">An error occured</exception>
Public Shared ReadOnly Property TopLevelWindows() As IReadOnlyList(Of Win32Window)
Get
Dim List As New List(Of Win32Window)
If API.EnumWindows(New API.EnumWindowsProc(Function(hWnd As Integer, lParam As Integer) AddToList(List, New Win32Window(hWnd))), 0) Then
Return New ReadOnlyListAdapter(Of Win32Window)(List)
Else
Dim ex As New API.Win32APIException
If ex.NativeErrorCode <> 0 Then
Throw ex
Else
Return New ReadOnlyListAdapter(Of Win32Window)(List)
End If
End If
End Get
End Property
''' <summary>Adds <paramref name="item"/> to <paramref name="List"/> and returns true</summary>
''' <param name="List"><see cref="List(Of T)"/> to add item to</param>
''' <param name="item">Item to be added</param>
''' <typeparam name="T">Type of <paramref name="item"/></typeparam>
''' <returns>True</returns>
Private Shared Function AddToList(Of T)(ByVal List As List(Of T), ByVal item As T) As Boolean
List.Add(item)
Return True
End Function