4

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?

Paul Turner
  • 38,949
  • 15
  • 102
  • 166
SK.
  • 4,174
  • 4
  • 30
  • 48
  • possible duplicate http://stackoverflow.com/questions/599663/retrieve-a-complete-processes-list-using-c-sharp – Liam Jun 26 '13 at 13:32
  • 2
    @Liam - if you follow the link, you'll see that Desktops Utility creates virtual machines. The topic is a little more complicated than what is covered in the "duplicate" question. – JDB Jun 26 '13 at 13:35
  • @Liam I believe it is not a duplicate since the other question does not relate to virtual desktop(s) at all. – Alvin Wong Jun 26 '13 at 13:49
  • @Cyborgx37 Where do you find the word "machine" in that link? lol – Alvin Wong Jun 26 '13 at 13:51
  • @AlvinWong - Dang it... you're right. I'm not really in my element here... signing off. ;) – JDB Jun 26 '13 at 13:54
  • When you run the code with a virtual desktop, what output do you get? – Paul Turner Jun 26 '13 at 15:15
  • 1
    @Liam Hi, This is not duplicate question. – SK. Jun 27 '13 at 12:27
  • 1
    @Cyborgx37 Hi which function you are telling that is `Process.GetProcessesByName("remoteMachineName Or remoteMachine IP")` [Go to ProcessesByName](http://msdn.microsoft.com/en-us/library/system.diagnostics.process.getprocessesbyname.aspx) but that function is for accessing the processes on remote machine. – SK. Jun 27 '13 at 12:30
  • @SimpalChaudhary - I read the linked article and misunderstood, thinking that the Desktop utility created actual virtual machines. If that were true, it would have a name and be accessible as a remote machine, but it's not true so that is irrelevant. – JDB Jun 27 '13 at 12:45
  • 1
    @Cyborgx37 - its ok bro... now i got it. Actually Desktop utility creates virtual desktop only on local machine. Thanks Bro – SK. Jun 27 '13 at 12:54
  • Is there anyway we can do this using python? – user3319471 Jul 27 '23 at 15:49

1 Answers1

2

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.

JDB
  • 25,172
  • 5
  • 72
  • 123
SK.
  • 4,174
  • 4
  • 30
  • 48
  • Thanks for posting an answer. It might help future visitors if you demonstrated how you used these functions to accomplish your goal, but +1 for answering your own question. – JDB Jun 27 '13 at 13:07