• 0

[C#] List running Applications


Question

14 answers to this question

Recommended Posts

  • 0

Ack! My old thread! It lives again?

For anyone interested, here's how I ended up doing it.

private const int GWL_EXSTYLE = (-20);
        private const int WS_EX_TOOLWINDOW = 0x80;
        private const int WS_EX_APPWINDOW = 0x40000;
        private const int GW_OWNER = 4;
        public delegate int EnumWindowsProcDelegate(int hWnd, int lParam);
        [DllImport("user32")]
        private static extern int EnumWindows(Pop.EnumWindowsProcDelegate lpEnumFunc, int lParam);
        [DllImport("User32.Dll")]
        public static extern void GetWindowText(int h, StringBuilder s, int nMaxCount);
        [DllImport("user32", EntryPoint = "GetWindowLongA")]
        private static extern int GetWindowLongPtr(int hwnd, int nIndex);
        [DllImport("user32")]
        private static extern int GetParent(int hwnd);
        [DllImport("user32")]
        private static extern int GetWindow(int hwnd, int wCmd);
        [DllImport("user32")]
        private static extern int IsWindowVisible(int hwnd);
        [DllImport("user32")]
        private static extern int GetDesktopWindow();

        private static bool IsTaskbarWindow(int hWnd)
        {
            int lExStyle;
            int hParent;
            lExStyle = GetWindowLongPtr(hWnd, GWL_EXSTYLE);
            hParent = GetParent(hWnd);
            bool fTaskbarWindow = ((IsWindowVisible(hWnd) != 0) & (GetWindow(hWnd, GW_OWNER) == 0) & (hParent == 0 | hParent == GetDesktopWindow()));
            if ((lExStyle & WS_EX_TOOLWINDOW) == WS_EX_TOOLWINDOW)
            {
                fTaskbarWindow = false;
            }
            if ((lExStyle & WS_EX_APPWINDOW) == WS_EX_APPWINDOW)
            {
                fTaskbarWindow = true;
            }
            return fTaskbarWindow;
        }
        public static int EnumWindowsProc(int hWnd, int lParam)
        {
            if (IsTaskbarWindow(hWnd))
            {
                StringBuilder sb = new StringBuilder(1024);
                GetWindowText(hWnd, sb, sb.Capacity);
                String xMsg = sb.ToString();
                {
                    if (xMsg.Length > 0)
                    {
                        //Do whatever.
                    }
                }
            }
            return 1;
        }

Call it like so:

EnumWindows(EnumWindowsProc, 0);

  • 0
  Dayon said:
Ack! My old thread! It lives again?

For anyone interested, here's how I ended up doing it.

private const int GWL_EXSTYLE = (-20);
        private const int WS_EX_TOOLWINDOW = 0x80;
        private const int WS_EX_APPWINDOW = 0x40000;
        private const int GW_OWNER = 4;
        public delegate int EnumWindowsProcDelegate(int hWnd, int lParam);
        [DllImport("user32")]
        private static extern int EnumWindows(Pop.EnumWindowsProcDelegate lpEnumFunc, int lParam);
        [DllImport("User32.Dll")]
        public static extern void GetWindowText(int h, StringBuilder s, int nMaxCount);
        [DllImport("user32", EntryPoint = "GetWindowLongA")]
        private static extern int GetWindowLongPtr(int hwnd, int nIndex);
        [DllImport("user32")]
        private static extern int GetParent(int hwnd);
        [DllImport("user32")]
        private static extern int GetWindow(int hwnd, int wCmd);
        [DllImport("user32")]
        private static extern int IsWindowVisible(int hwnd);
        [DllImport("user32")]
        private static extern int GetDesktopWindow();

        private static bool IsTaskbarWindow(int hWnd)
        {
            int lExStyle;
            int hParent;
            lExStyle = GetWindowLongPtr(hWnd, GWL_EXSTYLE);
            hParent = GetParent(hWnd);
            bool fTaskbarWindow = ((IsWindowVisible(hWnd) != 0) & (GetWindow(hWnd, GW_OWNER) == 0) & (hParent == 0 | hParent == GetDesktopWindow()));
            if ((lExStyle & WS_EX_TOOLWINDOW) == WS_EX_TOOLWINDOW)
            {
                fTaskbarWindow = false;
            }
            if ((lExStyle & WS_EX_APPWINDOW) == WS_EX_APPWINDOW)
            {
                fTaskbarWindow = true;
            }
            return fTaskbarWindow;
        }
        public static int EnumWindowsProc(int hWnd, int lParam)
        {
            if (IsTaskbarWindow(hWnd))
            {
                StringBuilder sb = new StringBuilder(1024);
                GetWindowText(hWnd, sb, sb.Capacity);
                String xMsg = sb.ToString();
                {
                    if (xMsg.Length > 0)
                    {
                        //Do whatever.
                    }
                }
            }
            return 1;
        }

Call it like so:

EnumWindows(EnumWindowsProc, 0);

586592243[/snapback]

This looks great. But I'm confused about how exactly to use it.

I've got a form set up, and added another class to it: WindowScraper - which has the following code:

using System;
using System.Text;
using System.Runtime.InteropServices;

namespace WindowScraper
{
	/// <summary>
	/// Summary description for WindowScraper.
	/// </summary>
	public class WindowScraper
	{
  public WindowScraper()
  {
  	//
  	// TODO: Add constructor logic here
  	//
  }

  /// <summary>
  /// Imported user32 functions.
  /// </summary>

  private const int GWL_EXSTYLE = (-20);
  private const int WS_EX_TOOLWINDOW = 0x80;
  private const int WS_EX_APPWINDOW = 0x40000;
  private const int GW_OWNER = 4;

  public delegate int EnumWindowsProcDelegate(int hWnd, int lParam);

  [DllImport("user32")]
  private static extern int EnumWindows(EnumWindowsProcDelegate lpEnumFunc, int lParam);

  [DllImport("User32.Dll")]
  public static extern void GetWindowText(int h, StringBuilder s, int nMaxCount);

  [DllImport("user32", EntryPoint = "GetWindowLongA")]
  private static extern int GetWindowLongPtr(int hwnd, int nIndex);

  [DllImport("user32")]
  private static extern int GetParent(int hwnd);

  [DllImport("user32")]
  private static extern int GetWindow(int hwnd, int wCmd);

  [DllImport("user32")]
  private static extern int IsWindowVisible(int hwnd);

  [DllImport("user32")]
  private static extern int GetDesktopWindow();

  /// <summary>
  /// Functions
  /// </summary>

  private static bool IsTaskbarWindow(int hWnd)
  {
  	int lExStyle;
  	int hParent;
  	lExStyle = GetWindowLongPtr(hWnd, GWL_EXSTYLE);
  	hParent = GetParent(hWnd);
  	bool fTaskbarWindow = ((IsWindowVisible(hWnd) != 0) & (GetWindow(hWnd, GW_OWNER) == 0) & (hParent == 0 | hParent == GetDesktopWindow()));
  	if ((lExStyle & WS_EX_TOOLWINDOW) == WS_EX_TOOLWINDOW)
  	{
    fTaskbarWindow = false;
  	}
  	if ((lExStyle & WS_EX_APPWINDOW) == WS_EX_APPWINDOW)
  	{
    fTaskbarWindow = true;
  	}
  	return fTaskbarWindow;
  }

  /// <summary>
  /// 
  /// </summary>
  /// <param name="hWnd"></param>
  /// <param name="lParam"></param>
  /// <returns></returns>

  public static int EnumWindowsProc(int hWnd, int lParam)
  {
  	if (IsTaskbarWindow(hWnd))
  	{
    StringBuilder sb = new StringBuilder(1024);
    GetWindowText(hWnd, sb, sb.Capacity);
    String xMsg = sb.ToString();
  	{
    if (xMsg.Length > 0)
    {
    	//Do whatever.
    }
  	}
  	}
  	return 1;
  }

	}
}

Now, In my form, I want to do the following: Create a function which Clears the listbox and adds an item for each found window (text). I know manipulate the listbox, obviously, but I don't know how to return a list of the window names.

I also want to return a list of the window handles, so that I can create bitmap's of them to save.

Any idea how to do this?

Thanks.

Oh, and one more thing, In the original code, this:

        [DllImport("user32")]
        private static extern int EnumWindows(Pop.EnumWindowsProcDelegate lpEnumFunc, int lParam);

Will not compile because pop is not a valid namespace. I deleted "pop." and it compiled fine, though i don't know if that will affect it working.

  • 0
  Bi0haZarD said:
Sep 7 2005 -> Dec 30 2005.

:blink:

lols.. :D

that was a stupid question !! I admit ! now dont gimme scary looks..

i was an open source guy thrown in this .net stuff by organization, so got stuck up that time.. lols

well for rest, it worked for me too, if anyone need help, post.

  • 0
  aby said:
just one problem, how did you invoke "EnumWindowsProc"?

You don't. The window manager does. EnumWindowsProc is a callback function. You define the function, pass a function pointer (delegate) in the EnumWindows call, and then EnumWindows will call you back by calling that function once for every window in its list.

  Goalie_CA said:
WEll, i believe the terminology in windows is a "job". A "job" contains several processes which in turn contains several threads.

That is not correct. Well, it is correct that Windows has a concept called "job" that groups processes together. But it has nothing to do with what shows up in the Applications tab of Task Manager.

That list uses a similar mechanism to what the taskbar uses. It enumerates top-level windows and decides whether to display each one based on specific properties (WS_EX_APPWINDOW, etc).

  • 0

hey thanks Brandon,

but I've already found the solution. yes the same way I did.

  Brandon Live said:
You don't. The window manager does. EnumWindowsProc is a callback function. You define the function, pass a function pointer (delegate) in the EnumWindows call, and then EnumWindows will call you back by calling that function once for every window in its list.

That is not correct. Well, it is correct that Windows has a concept called "job" that groups processes together. But it has nothing to do with what shows up in the Applications tab of Task Manager.

That list uses a similar mechanism to what the taskbar uses. It enumerates top-level windows and decides whether to display each one based on specific properties (WS_EX_APPWINDOW, etc).

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Reddit takes legal action, says Anthropic trained Claude on Reddit posts without permission by David Uzondu Reddit has filed a complaint against Anthropic, alleging the AI company straight-up stole its content to train AI models, including the Claude chatbot, without paying a dime. The lawsuit, lodged on June 4, 2025, in San Francisco, accuses Anthropic of repeatedly violating Reddit's User Agreement, which explicitly prohibits unauthorized commercial exploitation and automated scraping of its platform. The data on Reddit seems to be very valuable, seeing as the platform is already making bank licensing its content to other big AI players like Google and OpenAI, as mentioned in its complaint. These deals are reportedly worth tens of millions annually, so it's understandable why Reddit would be ###### if Anthropic was just taking the goods for free. Reddit has been quite clear that while its platform is open for community, it has rules, and commercial outfits cannot just waltz in and use user-generated content to build billion-dollar enterprises without permission or compensation. According to Reddit, Anthropic has been scraping its content since at least December 2021, ignoring technical measures like robots.txt designed to prevent such automated access. Reddit claims that in July 2024, Anthropic falsely stated it had stopped its bots from accessing Reddit, when audit logs allegedly showed Anthropic's bots hit Reddit's servers over a hundred thousand more times in the following months. The complaint on page 5 even includes a screenshot of Reddit's lawyers chatting with Claude, where the AI "confirms" it was trained on Reddit data. Now, we don't know how true this is, given the fact that LLMs hallucinate a lot. Reddit's legal filing paints Anthropic as a company with "two faces": one that publicly preaches about ethical AI and respecting boundaries, and another that privately ignores rules to line its pockets. Reddit is not holding back in what it is asking the court to do to Anthropic. The company demands significant monetary compensation, aiming to recover any profits Anthropic made from using Reddit's data, get repaid for its own financial losses, and it is also seeking punitive damages, looking to punish Anthropic for what Reddit describes as willful and malicious conduct. In addition to that, the company is also seeking an injunction designed to permanently stop Anthropic from using any Reddit data. This order would also compel Anthropic to delete all Reddit content from its systems and pull any AI technology, like its Claude chatbot, from commercial use if it was developed using this disputed data. On top of all that, Reddit wants Anthropic to cover all its legal expenses, including attorneys' fees and court costs.
    • I'm just thinking out loud for a second...could it be the cookie prompt that kicks up the adblock message? The reason I ask is that on Firefox (ad-block enabled but not for Neowin) I don't get the cookie consent option. But if I open Chrome (ad-block disabled) and go to the Neowin I get the cookie banner and then it's all fine. Some form of conflict of interest between the banners? It's probably nothing, but that's what I have just noticed.
    • SoundSwitch 6.14.1 by Razvan Serea SoundSwitch is a Windows app that makes switching your sound devices super easy. Normally, changing speakers or microphones means clicking through annoying menus. With SoundSwitch, you just press a shortcut key (like Ctrl + Alt + F1) — and it switches to the device you want. You can set different keys for speakers, headphones, microphones, or even groups of devices. It also lets you mute your mic with a hotkey and shows a clear banner so you know it's muted. It runs in the background, shows up in your taskbar, and starts with Windows if you want. It’s perfect if you use multiple audio devices and get tired of clicking around every time you want to change one. SoundSwitch features: Customizable Hotkeys: Assign specific key combinations to switch between audio devices quickly. ​ Playback and Recording Device Switching: Toggle between selected playback and recording devices without navigating through system menus. ​ Microphone Mute Toggle: Use hotkeys to mute or unmute the default microphone. ​ Persistent Mute Notification: Displays a compact banner indicating the microphone's mute state, which remains visible until the microphone is unmuted. ​ Profile Management: Create profiles to switch between specific combinations of playback and recording devices using designated hotkeys. ​ Command Line Interface (CLI): Control SoundSwitch through command-line commands for device switching, microphone mute control, and profile management. ​ Auto-Start with Windows: Option to launch SoundSwitch automatically upon system startup. ​ Multi-Language Support: Includes translations for various languages, such as Tamil. ​ Notification Customization: Choose the type of notifications displayed for device switching and mute status. ​ Support for Various Hotkey Combinations: Accepts single keys like PrintScreen, Pause, Home, End, and function keys as hotkeys. ​ System Tray Integration: Access settings and perform device switching directly from the system tray icon. ​ Device Grouping: Organize multiple devices into groups for streamlined switching. ​ User-Friendly Interface: Provides an intuitive setup and configuration process for users. ​ Open-Source Development: Available on GitHub for community contributions and transparency. ​ Regular Updates: Actively maintained with new features and bug fixes. ​ SoundSwitch 6.14.1 changelog: Bug Fixes settings: fix opening settings crashing the application when using CLI or opening SoundSwitch again (b3dca74) Languages Amharic: Added About translation using Weblate (8a40dab) Japanese: Translated About using Weblate (3541994) Japanese: Translated Settings using Weblate (ca5b2fe) Japanese: Translated Settings using Weblate (39a2340) Japanese: Translated Tray Icon using Weblate (1286b92) Japanese: Translated Update Download using Weblate (1c2c658) Norwegian Bokmål: Translated Settings using Weblate (5aaf243) Portuguese: Translated Settings using Weblate (e11f18d) Swedish: Translated Settings using Weblate (8b7b738) Download: SoundSwitch 6.14.1 | 45.4 MB (Open Source) View: SoundSwitch Website | Github | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • Wow, the usual crowd is out in full force again — the trolls who think sarcasm is insight, the doom prophets who scream 'web apps = surveillance', and the armchair devs who still think Outlook 2003 was peak tech. Here’s a wild idea: maybe evaluate an app on what it does instead of what your paranoia imagines it’s doing. The new Outlook is fast, clean, and tightly integrated. No, it’s not perfect — what app is? But if your main tech critique is 'It’s different and Microsoft is evil', you’re not reviewing software. You’re just rehearsing your trust issues. Don't like it? Cool. But at least bring something to the table besides tired one-liners and Chicken Little routines. Some of us actually use this stuff and prefer practical feedback over pointless whining.
    • And they will only last thousands of years underground we don’t know where.
  • Recent Achievements

    • Reacting Well
      James courage Tabla earned a badge
      Reacting Well
    • Apprentice
      DarkShrunken went up a rank
      Apprentice
    • Dedicated
      CHUNWEI earned a badge
      Dedicated
    • Collaborator
      DarkShrunken earned a badge
      Collaborator
    • Rookie
      Pat-Garrett went up a rank
      Rookie
  • Popular Contributors

    1. 1
      +primortal
      341
    2. 2
      snowy owl
      167
    3. 3
      +FloatingFatMan
      163
    4. 4
      ATLien_0
      161
    5. 5
      Xenon
      128
  • Tell a friend

    Love Neowin? Tell a friend!