Reputation: 4358
I am working on Window 7 and I have multiple desktops by using the Sysinternals Desktops utility. I just want to list all the processes running across all the desktops.
Process[] processlist = Process.GetProcesses();
foreach (Process p in processlist)
{
listBox1.Items.Add(p.ProcessName + " " + p.Id + " " + p.MainWindowTitle);
}
With this code I am able to get all the processes running on the current desktop, but I am not able to get the processes running inside different desktops.
Only Desktops is showing as a single process.
How can I get the child processes inside multiple desktops?
Upvotes: 4
Views: 927
Reputation: 4358
Yup i got it.. after doing RnD.
I am using user32.dll and some of these methods.
If you also want to achieve some thing use these functions.
[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);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool IsWindowVisible(IntPtr hWnd);
[DllImport("user32.dll")]
private static extern bool EnumDesktops(IntPtr hwinsta, EnumDesktopProc lpEnumFunc, IntPtr lParam);
[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr SetActiveWindow(IntPtr hWnd);
[DllImport("user32.dll", SetLastError = false)]
private static extern IntPtr GetDesktopWindow();
[DllImport("user32.dll")]
private static extern IntPtr GetProcessWindowStation();
[DllImport("user32.dll")]
private static extern IntPtr OpenDesktop(string lpszDesktop, uint dwFlags, bool fInherit, uint dwDesiredAccess);
[DllImport("user32.dll")]
private static extern bool SwitchDesktop(IntPtr hDesktop);
By using these methods I am able to list down all the running processes on all the virtual desktop.
Upvotes: 2