Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Error in listing open windows

Error in listing open windows

Scheduled Pinned Locked Moved C#
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    srikrishnathanthri
    wrote on last edited by
    #1

    Hello, I am trying to get the list of open windows using C#, here is my code..

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Runtime.InteropServices;

    namespace howto_list_desktop_windows
    {
    static class DesktopWindowsStuff
    {
    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    private static extern bool IsWindowVisible(IntPtr hWnd);
    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    private static extern bool IsIconic(IntPtr hWnd);
    [DllImport("user32.dll", EntryPoint = "GetWindowText",
    ExactSpelling = false, CharSet = CharSet.Auto, SetLastError = true)]
    private static extern int GetWindowText(IntPtr hWnd,
    StringBuilder lpWindowText, int nMaxCount);
    [DllImport("user32.dll", EntryPoint = "EnumDesktopWindows",
    ExactSpelling = false, CharSet = CharSet.Auto, SetLastError = true)]
    private static extern bool EnumDesktopWindows(IntPtr hDesktop,
    EnumDelegate lpEnumCallbackFunction, IntPtr lParam);
    // Define the callback delegate's type.
    private delegate bool EnumDelegate(IntPtr hWnd, int lParam);
    // Save window titles and handles in these lists.
    private static List WindowHandles;
    private static List WindowTitles;
    // Return a list of the desktop windows' handles and titles.
    public static void GetDesktopWindowHandlesAndTitles(
    out List handles, out List titles)
    {
    WindowHandles = new List();
    WindowTitles = new List();
    if (!EnumDesktopWindows(IntPtr.Zero, FilterCallback, IntPtr.Zero))
    {
    handles = null;
    titles = null;
    }
    else
    {
    // handles = null;
    handles = WindowHandles;
    titles = WindowTitles;
    }
    }
    // We use this function to filter windows.
    // This version selects visible windows that have titles.
    private static bool FilterCallback(IntPtr hWnd, int lParam)
    {
    // Get the window's title.
    StringBuilder sb_title = new StringBuilder(1024);
    int length = GetWindowText(hWnd, sb_title, sb_title.Capacity);
    string title = sb_title.ToString();
    // If the wind

    L K 2 Replies Last reply
    0
    • S srikrishnathanthri

      Hello, I am trying to get the list of open windows using C#, here is my code..

      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
      using System.Runtime.InteropServices;

      namespace howto_list_desktop_windows
      {
      static class DesktopWindowsStuff
      {
      [DllImport("user32.dll")]
      [return: MarshalAs(UnmanagedType.Bool)]
      private static extern bool IsWindowVisible(IntPtr hWnd);
      [DllImport("user32.dll")]
      [return: MarshalAs(UnmanagedType.Bool)]
      private static extern bool IsIconic(IntPtr hWnd);
      [DllImport("user32.dll", EntryPoint = "GetWindowText",
      ExactSpelling = false, CharSet = CharSet.Auto, SetLastError = true)]
      private static extern int GetWindowText(IntPtr hWnd,
      StringBuilder lpWindowText, int nMaxCount);
      [DllImport("user32.dll", EntryPoint = "EnumDesktopWindows",
      ExactSpelling = false, CharSet = CharSet.Auto, SetLastError = true)]
      private static extern bool EnumDesktopWindows(IntPtr hDesktop,
      EnumDelegate lpEnumCallbackFunction, IntPtr lParam);
      // Define the callback delegate's type.
      private delegate bool EnumDelegate(IntPtr hWnd, int lParam);
      // Save window titles and handles in these lists.
      private static List WindowHandles;
      private static List WindowTitles;
      // Return a list of the desktop windows' handles and titles.
      public static void GetDesktopWindowHandlesAndTitles(
      out List handles, out List titles)
      {
      WindowHandles = new List();
      WindowTitles = new List();
      if (!EnumDesktopWindows(IntPtr.Zero, FilterCallback, IntPtr.Zero))
      {
      handles = null;
      titles = null;
      }
      else
      {
      // handles = null;
      handles = WindowHandles;
      titles = WindowTitles;
      }
      }
      // We use this function to filter windows.
      // This version selects visible windows that have titles.
      private static bool FilterCallback(IntPtr hWnd, int lParam)
      {
      // Get the window's title.
      StringBuilder sb_title = new StringBuilder(1024);
      int length = GetWindowText(hWnd, sb_title, sb_title.Capacity);
      string title = sb_title.ToString();
      // If the wind

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Try with EnumWindows function (Windows)[^] instead.

      S 1 Reply Last reply
      0
      • L Lost User

        Try with EnumWindows function (Windows)[^] instead.

        S Offline
        S Offline
        srikrishnathanthri
        wrote on last edited by
        #3

        can you please edit my code..Sorry I am new to .net and I am not getting how to use EnumWindows

        P 1 Reply Last reply
        0
        • S srikrishnathanthri

          can you please edit my code..Sorry I am new to .net and I am not getting how to use EnumWindows

          P Offline
          P Offline
          Pete OHanlon
          wrote on last edited by
          #4

          There are plenty of examples here[^].

          This space for rent

          1 Reply Last reply
          0
          • S srikrishnathanthri

            Hello, I am trying to get the list of open windows using C#, here is my code..

            using System;
            using System.Collections.Generic;
            using System.Linq;
            using System.Text;
            using System.Runtime.InteropServices;

            namespace howto_list_desktop_windows
            {
            static class DesktopWindowsStuff
            {
            [DllImport("user32.dll")]
            [return: MarshalAs(UnmanagedType.Bool)]
            private static extern bool IsWindowVisible(IntPtr hWnd);
            [DllImport("user32.dll")]
            [return: MarshalAs(UnmanagedType.Bool)]
            private static extern bool IsIconic(IntPtr hWnd);
            [DllImport("user32.dll", EntryPoint = "GetWindowText",
            ExactSpelling = false, CharSet = CharSet.Auto, SetLastError = true)]
            private static extern int GetWindowText(IntPtr hWnd,
            StringBuilder lpWindowText, int nMaxCount);
            [DllImport("user32.dll", EntryPoint = "EnumDesktopWindows",
            ExactSpelling = false, CharSet = CharSet.Auto, SetLastError = true)]
            private static extern bool EnumDesktopWindows(IntPtr hDesktop,
            EnumDelegate lpEnumCallbackFunction, IntPtr lParam);
            // Define the callback delegate's type.
            private delegate bool EnumDelegate(IntPtr hWnd, int lParam);
            // Save window titles and handles in these lists.
            private static List WindowHandles;
            private static List WindowTitles;
            // Return a list of the desktop windows' handles and titles.
            public static void GetDesktopWindowHandlesAndTitles(
            out List handles, out List titles)
            {
            WindowHandles = new List();
            WindowTitles = new List();
            if (!EnumDesktopWindows(IntPtr.Zero, FilterCallback, IntPtr.Zero))
            {
            handles = null;
            titles = null;
            }
            else
            {
            // handles = null;
            handles = WindowHandles;
            titles = WindowTitles;
            }
            }
            // We use this function to filter windows.
            // This version selects visible windows that have titles.
            private static bool FilterCallback(IntPtr hWnd, int lParam)
            {
            // Get the window's title.
            StringBuilder sb_title = new StringBuilder(1024);
            int length = GetWindowText(hWnd, sb_title, sb_title.Capacity);
            string title = sb_title.ToString();
            // If the wind

            K Offline
            K Offline
            KumarArunR
            wrote on last edited by
            #5

            Have you found solution for this issue ? I am facing similar issue.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups