Sysinternals Desktops - Programmatically get Desktop Number of a running program

Francesco Paolo La Gamba 21 Reputation points
2023-06-23T09:38:17.5933333+00:00

Hi forum,

I would like to know if it is possible, through process enumeration, to know on which desktop number

(Using SysInternals.Desktops)

they are running.

All possibly in C#.

Best regards

Francesco Paolo La Gamba

Windows for business | Windows Server | User experience | Other
Windows for business | Windows Client for IT Pros | User experience | Other
Developer technologies | C#
Developer technologies | C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
0 comments No comments
{count} votes

Answer accepted by question author
  1. RLWA32 51,536 Reputation points
    2023-06-25T11:51:19.7833333+00:00

    @Francesco Paolo La Gamba First, its important to understand that Windows desktop objects are associated with threads, not processes. So its possible for different threads of a process to create windows on more than one desktop contained in a session's interactive window stations (i.e., WinSta0). Refer to Thread Connection to a Desktop.

    Presumably you are interested in those processes that have created windows on any desktops created by Sysinternals' Desktops application. It is possible to identify these processes using various Windows API functions. By observation, Sysinternals Desktops associates desktop 1 with the Default desktop. For its desktops 2 through 4 it creates new desktop objects that are named "Sysinternals Desktop 1", "Sysinternals Desktop 2" and "Sysinternals Desktop 3", respectively.

    The EnumDesktops function can be used to identify which of the above desktops 2-4 have been created by the Desktops application. Once you have identified the created desktops you can obtain a handle to them using the OpenDesktop function. Then you can enumerate the top level windows on that desktop using the EnumDesktopWindows function. The process id associated with each enumerated top-level window can be identified by calling the GetWindowThreadProcessId function. With the process id you can obtain a handle to the process with the OpenProcess function and obtain the fully qualfied path to the module by calling the QueryFullProcessImageName function.

    So its certainly possible to associate Sysinternals numbered desktops with running processes. My test was written in C++.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.