• 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

    • The list of Mac devices rumored to get macOS 26 by Hamid Ganji Apple will announce macOS 26 or macOS Tahoe at its Monday Worldwide Developers Conference. As has been reported multiple times over the past weeks, all Apple operating systems will receive a UI overhaul, a touch of AI, and new names at this year's WWDC. The logic behind the "26" in the new macOS version is that Apple aims to align its naming schedule with its 2025-2026 release cycle. The same version number will also appear across iOS, iPadOS, watchOS, and tvOS. While skipping multiple software versions raises many questions, it might help maintain consistency in the Apple ecosystem. With just two days away from the WWDC kick-off, many Mac owners might wonder if the upcoming macOS 26 is compatible with their devices. Here's the rumored list of compatible Mac devices with the macOS 26: MacBook Air (M1 and later) MacBook Pro (2019 and later) iMac (2020 and later) Mac Mini (M1 and later) Mac Studio (all models) Mac Pro (2019 and later) This report comes from MacRumors, which cites a private account on X as the source. However, the interesting thing about this list is that, according to the leaker, Apple might end software support for the MacBook Pro 13-inch (2020 model, two Thunderbolt 3 ports). For now, we should take this claim with a pinch of salt. The 2020 MacBook Pro 13-inch launched with both Intel and Apple Silicon M1 processors. It also comes with two port configurations. Apple is expected to end support for more Intel-based Mac devices this year, and this specific MacBook Pro variant might also be on Apple's kill list. WWDC 2025 kicks off on June 9, and Apple will unveil the latest version of its operating systems with an overhauled UI and a slew of AI-related features. So far, we know what Apple Watch models might get watchOS 26. Apple will announce the compatibility list of various devices at Monday's event.
    • It's a separate question to the thread, but I have VLC and qBitorrent working on W11 without any issue. Download->install->job done.
    • Currently I am using Display Port connection to monitor.   If I change the setting you show above it does make text larger, but ALL text larger not just icon text on desktop. Also is doesn't change the text weight at all. That setting leaves it very thing test. I want bold, or semibold.
    • Patch My PC - Home Updater 5.2.3.0 by Razvan Serea Patch My PC Free is a reliable tool which can quickly check your PC for outdated software. The supported third-party programs include a large number of widely-used applications, including Adobe Reader, Mozilla Firefox, Java, 7-Zip, BleachBit, Google Chrome and many more. Patch My PC Home updater features: Updates over 500 common apps check including portable apps Ability to cache updates for use on multiple machines No bloatware during installations Applications install/update silently by default no install wizard needed Optionally, disable silent install to perform a manual custom install Easy to use user interface Change updated and outdated apps color for color blindness Option to automatically kill programs before updating it Create a baseline of applications if installing on new PC’s Quickly uninstall multiple programs Scan time is usually less than 1 second Set updates to happen on a schedule Skip updates for any application you don’t want to update Suppresses restarts when performing application updates Patch My PC - Home Updater 5.2.3.0 changelog: Startup Manager New tab to manage which apps launch at startup. This helps speed up your boot time and gives you control over what runs in the background. Generate Diagnostic ZIP You can now create a diagnostic ZIP file from the About page. This helps if you need to send logs on our support forum for Home Updater. Remove Portable Apps Right-click any portable app in the App Catalog or Uninstaller page to remove it directly. Applications Added FFmpeg (Full Shared) – Portable Fing G-Helper – Portable IntelliJ IDEA Community Edition K-Lite Basic Codec Pack K-Lite Full Codec Pack K-Lite Standard Codec Pack KeePass Password Safe v1 LibreOffice Help Pack MemTest86 – Portable Nexus Vortex Nvidia Profile Inspector – Portable Pale Moon – Portable ViVeTool – Portable WinCDEmu Windows PC Health Check Wise Video Converter Applications Removed Driver Easy Download: Patch My PC 5.2.3.0 | 54.8 MB (Freeware) Download: Patch My PC Portable | 31.0 MB (Portable) View: Patch My PC Free Homepage | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • "For starters, Microsoft Edge is getting a media control center. This feature is intended to let you control multiple media sources from any website in a single place." Oh, I've got this Media Control and couldn't find how to disable it. I hate it when a button appears on a toolbar where there was none just before I press Play. I probably would find it at least somewhat useful if I could start playing media from any opened tab, but now it only shows controls for media I've already started playing. If anyone knows how to disable it - I'd appreciate a hint.
  • Recent Achievements

    • Week One Done
      abortretryfail earned a badge
      Week One Done
    • First Post
      Mr bot earned a badge
      First Post
    • First Post
      Bkl211 earned a badge
      First Post
    • One Year In
      Mido gaber earned a badge
      One Year In
    • One Year In
      Vladimir Migunov earned a badge
      One Year In
  • Popular Contributors

    1. 1
      +primortal
      491
    2. 2
      +FloatingFatMan
      258
    3. 3
      snowy owl
      249
    4. 4
      ATLien_0
      223
    5. 5
      +Edouard
      190
  • Tell a friend

    Love Neowin? Tell a friend!