Click to Rate and Give Feedback
MSDN
MSDN Library
User Interface
Windowing
Windows
Windows Reference
Functions
 EnumWindows Function
EnumWindows Function

The EnumWindows function 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 EnumWindows(      
    WNDENUMPROC lpEnumFunc,     LPARAM lParam );

Parameters

lpEnumFunc
[in] Pointer to an application-defined callback function. For more information, see EnumWindowsProc.
lParam
[in] Specifies an application-defined value to be passed to the callback function.

Return Value

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.

Function Information

Minimum DLL Versionuser32.dll
HeaderDeclared in Winuser.h, include Windows.h
Import libraryUser32.lib
Minimum operating systems Windows 95, Windows NT 3.1
UnicodeImplemented as Unicode version.

See Also

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Possible VB9 declaration      Đonny ... That Asian Guy   |   Edit   |   Show History
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
enum windows fails in a scheduled task...      bret_levycodev.com   |   Edit   |   Show History

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);

}

Tags What's this?: Add a tag
Flag as ContentBug
vb.net syntax      dmex   |   Edit   |   Show History
<DllImport("user32.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
Public Shared Function EnumWindows(ByVal callback As EnumThreadWindowsCallback, ByVal extraData As IntPtr) As Boolean End Function
Flag as ContentBug
C# syntax      dmex   |   Edit   |   Show History
[DllImport("user32.dll", CharSet=CharSet.Auto, SetLastError=true)]
public static extern bool EnumWindows(EnumThreadWindowsCallback callback, IntPtr extraData);
Tags What's this?: c# (x) syntax (x) Add a tag
Flag as ContentBug
Processing
© 2010 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker