Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


Show Recent Changes
Subscribe (RSS)
Misc. Pages
Comments
FAQ
Helpful Tools
Playground
Suggested Reading
Website TODO List
Download Visual Studio Add-In

EnumDesktopWindows (user32)
 
.
Summary

C# Signature:

[DllImport("user32.dll")]
static extern bool EnumDesktopWindows(IntPtr hDesktop,
   EnumDesktopWindowsDelegate lpfn, IntPtr lParam);

User-Defined Types:

None.

Notes:

None.

Tips & Tricks:

Please add some!

Sample Code:

using System;

using System.Text;

using System.Collections;

using System.Runtime.InteropServices;

/// <summary>

/// EnumDesktopWindows Demo - shows the caption of all desktop windows.

/// Authors: Svetlin Nakov, Martin Kulov

/// Bulgarian Association of Software Developers - http://www.devbg.org/en/

/// </summary>

public class EnumDesktopWindowsDemo

{

    const int MAXTITLE = 255;

    private static ArrayList mTitlesList;

    private delegate bool EnumDelegate(IntPtr hWnd, int lParam);

    [DllImport("user32.dll", EntryPoint="EnumDesktopWindows",
     ExactSpelling=false, CharSet=CharSet.Auto, SetLastError=true)]
    private static extern bool _EnumDesktopWindows(IntPtr hDesktop,
    EnumDelegate lpEnumCallbackFunction, IntPtr lParam);

    [DllImport("user32.dll", EntryPoint="GetWindowText",
     ExactSpelling=false, CharSet=CharSet.Auto, SetLastError=true)]
    private static extern int _GetWindowText(IntPtr hWnd,
    StringBuilder lpWindowText, int nMaxCount);

    private static bool EnumWindowsProc(IntPtr hWnd, int lParam)
    {
    string title = GetWindowText(hWnd);
    mTitlesList.Add(title);
    return true;
    }

    /// <summary>
    /// Returns the caption of a windows by given HWND identifier.
    /// </summary>
    public static string GetWindowText(IntPtr hWnd)
    {
    StringBuilder title = new StringBuilder(MAXTITLE);
    int titleLength = _GetWindowText(hWnd, title, title.Capacity + 1);
    title.Length = titleLength;

    return title.ToString();
    }

    /// <summary>
    /// Returns the caption of all desktop windows.
    /// </summary>
    public static string[] GetDesktopWindowsCaptions()
    {
    mTitlesList = new ArrayList();
    EnumDelegate enumfunc = new EnumDelegate(EnumWindowsProc);
    IntPtr hDesktop = IntPtr.Zero; // current desktop
    bool success = _EnumDesktopWindows(hDesktop, enumfunc, IntPtr.Zero);

    if (success)
    {
        // Copy the result to string array
        string[] titles = new string[mTitlesList.Count];
        mTitlesList.CopyTo(titles);
        return titles;
    }
    else
    {
        // Get the last Win32 error code
        int errorCode = Marshal.GetLastWin32Error();

        string errorMessage = String.Format(
        "EnumDesktopWindows failed with code {0}.", errorCode);
        throw new Exception(errorMessage);
    }
    }

    static void Main()
    {
    string[] desktopWindowsCaptions = GetDesktopWindowsCaptions();
    foreach (string caption in desktopWindowsCaptions)
    {
        Console.WriteLine(caption);
    }
    }

}

Alternative Managed API:

Do you know one? Please contribute it!

Documentation

Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

 
Access PInvoke.net directly from VS:
Terms of Use
Find References
Show Printable Version
Revisions