Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting a list of all applications

Tags:

c#

process

I'm trying to get the list of all the open applications. Specifically, if you open the task manager and go to the applications tab, that list.

I've tried using something like this:

foreach (var p in Process.GetProcesses())
{
    try
    {
        if (!String.IsNullOrEmpty(p.MainWindowTitle))
        {
            sb.Append("\r\n");
            sb.Append("Window title: " + p.MainWindowTitle.ToString());
            sb.Append("\r\n");
        }
    }
    catch
    {
    }
}

Like in a few examples I've found, but this doesn't pull all the applications for me. It's only grabbing about half the ones I can see in the task manager or that I know I have open. For example, this method doesn't pick up Notepad++ or Skype for some reason, but DOES pick up Google Chrome, Calculator, and Microsoft Word.

Does anyone know either why this isn't working correctly or how to do so?

Also, a friend suggested it might be a permissions issue, but I am running visual studio as administrator and it hasn't changed.

EDIT: The problem I'm getting is that most of the solutions I've been given just returns a list of ALL processes, which isn't what I want. I just want the open applications or windows, like the list that appears on the task manager. Not a list of every single process.

Also, I know there is bad code in here, including the empty catch block. This was a throwaway project just to figure out how this works in the first place.

like image 400
Chris Bacon Avatar asked Jan 08 '13 02:01

Chris Bacon


1 Answers

The code example here appears to give what you're asking for. Modified version:

public class DesktopWindow
{
    public IntPtr Handle { get; set; }
    public string Title { get; set; }
    public bool IsVisible { get; set; }
}

public class User32Helper
{
    public delegate bool EnumDelegate(IntPtr hWnd, int lParam);

    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool IsWindowVisible(IntPtr hWnd);

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

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

    public static List<DesktopWindow> GetDesktopWindows()
    {
        var collection = new List<DesktopWindow>();
        EnumDelegate filter = delegate(IntPtr hWnd, int lParam)
        {
            var result = new StringBuilder(255);
            GetWindowText(hWnd, result, result.Capacity + 1);
            string title = result.ToString();

            var isVisible = !string.IsNullOrEmpty(title) && IsWindowVisible(hWnd);

            collection.Add(new DesktopWindow { Handle = hWnd, Title = title, IsVisible = isVisible });

            return true;
        };

        EnumDesktopWindows(IntPtr.Zero, filter, IntPtr.Zero);
        return collection;
    }
}

With the above code, calling User32Helper.GetDesktopWindows() should give you a list of containing the Handle/Title for all open applications as well as whether or not they're visible. Note that true is returned regardless of the window's visibility, as the item would still show up in the Applications list of Task Manager as the author was asking.

You could then use the corresponding Handle property from one of the items in the collection to do a number of other tasks using other Window Functions (such as ShowWindow or EndTask).

like image 156
areyling Avatar answered Oct 24 '22 22:10

areyling