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 10
Windows 10
A Microsoft operating system that runs on personal computers and tablets.
11,033 questions
Windows Server
Windows Server
A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.
12,495 questions
C#
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.
10,551 questions
Windows 11
Windows 11
A Microsoft operating system designed for productivity, creativity, and ease of use.
8,744 questions
0 comments No comments
{count} votes

Accepted answer
  1. RLWA32 42,286 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