---
title: "Inside Windows Remote Sessions: From TermService to Winlogon | PrimalDesk Journal"
description: "A reverse-engineering walkthrough of how Windows creates, initializes, and turns a remote session into a fully interactive desktop."
url: "https://primaldesk.com/blog/windows-remote-sessions"
---

[← All stories](https://primaldesk.com/blog.md)

Engineering

# Inside Windows Remote Sessions: From TermService to Winlogon

A reverse-engineering walkthrough of how Windows creates, initializes, and turns a remote session into a fully interactive desktop.

PrimalDesk team·October 6, 2026·12 min read

![Windows remote session architecture](https://primaldesk.com/assets/blog/thumb_win_sessions.png)

A remote desktop connection looks deceptively simple from the outside: a client connects, Windows authenticates a user, and a desktop appears.

Internally, the path is much more interesting.

A new remote session is not created by one service and it does not become interactive in a single step. The work is split across **TermService, Local Session Manager, SMSS, CSRSS, Win32k, Winlogon, LSA, and the display stack**, with several IPC layers connecting them.

I ran into this while working on the Windows session stack behind **PrimalDesk**. Public documentation explains individual pieces of RDS reasonably well, but it does not give a complete picture of how a remote session is created and brought from a non-interactive state to a working desktop.

This article reconstructs that path using public documentation, WinDbg, IDA Pro, RPCView, Microsoft symbols, and runtime analysis.

The goal is not to document every RDS feature. It is to answer a narrower question:

> What actually happens inside Windows between a remote connection request and a fully initialized interactive session?

\[IMAGE: final remote-session lifecycle diagram\]

## Sessions as an isolation boundary

Windows sessions provide isolated execution environments inside one operating system instance.

A machine can have:

-   Session 0 for services and background system processes
-   a console session attached to the physical machine
-   one or more remote interactive sessions

A session has its own Session ID, its own per-session object namespaces, and its own interactive Win32 environment.

If you inspect the Object Manager namespace, `\Sessions` contains separate subdirectories for active sessions. Some named kernel objects therefore exist in a session-local namespace rather than globally.

![Object Explorer showing \\Sessions](https://primaldesk.com/assets/blog/object_explorer.jpg)

The graphical side is equally important. Windows maps part of the Win32 subsystem state into **Session Space**. The code of `win32k.sys`, `win32kbase.sys`, and `win32kfull.sys` is shared system-wide, but some of their data is session-local.

That is why the same virtual address inside Win32k can refer to different underlying data depending on which session the current thread belongs to.

The session lifecycle crosses several Windows IPC layers.

At the lowest level, local communication commonly reaches **ALPC**. Above it, Windows uses **MSRPC** for marshaling and RPC semantics, and **COM** can use that RPC infrastructure for cross-process calls.

For this investigation, the useful mental model is:

```
COM
  ↓
MSRPC
  ↓
ALPC
  ↓
Kernel
```

Unlike shared memory or higher-level IPC mechanisms such as named pipes, ALPC is a native message-based IPC facility built directly around kernel transitions. Sending a request is a real syscall: the message is copied into kernel memory, where Windows mediates its delivery to the receiving process. One of the central primitives here is NtAlpcSendWaitReceivePort (ZwAlpcSendWaitReceivePort). It combines several operations in a single call: it can send a message, receive a message, and block the calling thread while waiting for a reply.

![IDA ALPC](https://primaldesk.com/assets/blog/ida_alpc.jpg)

Not every component uses the full stack. Some low-level Windows processes communicate through raw ALPC directly.

## TermService coordinates the lifecycle

The Windows Terminal Services service is implemented primarily in `termsrv.dll`, hosted inside `svchost.exe`.

TermService owns the high-level remote-session lifecycle:

-   incoming remote connections
-   protocol providers
-   reconnect and disconnect handling
-   timeouts
-   session state
-   interaction with the rest of the RDS stack

It can also load third-party protocol providers through the Remote Desktop Services Protocol Provider API. A useful correction, however, is that **TermService does not directly create a Windows session in the kernel**. It coordinates the operation and asks other system components to perform the privileged work. The call chain becomes much easier to understand once this distinction is clear.

## How a Windows session is actually created

At first, it is natural to look for an API resembling:

```
NtCreateSession(...)
NtOpenSession(...)
```

There is no equivalent public NT system call for creating an interactive Windows session. Instead, session creation is tied to process creation. The native process creation flags include:

```
#define PROCESS_CREATE_FLAGS_CREATE_SESSION 0x00000080
```

[`PROCESS_CREATE_FLAGS_CREATE_SESSION - ntpsapi.h`](https://github.com/winsiderss/systeminformer/blob/master/phnt/include/ntpsapi.h)

When a process is created with this flag, the kernel creates a new session and attaches the process to it: ![Kernel-side session creation](https://primaldesk.com/assets/blog/kernel_session_creation.jpg) This is not available to arbitrary callers. The operation is privileged, and the only process trusted to do it is **SMSS - Session Manager Subsystem**.

## SMSS and the first process of the session

`smss.exe` is one of the earliest user-mode processes started by Windows. It is a native subsystem process with a deliberately small dependency set.

![SMSS dependencies](https://primaldesk.com/assets/blog/smss_deps.jpg)

If we inspect its ALPC endpoints, one port stands out:

```
SmApiPort
```

SMSS uses it for session-management requests.

Opening `smss.exe` in IDA and following references to `SmApiPort` leads to the initialization and dispatch path for this interface.

The dispatch table exposes operations corresponding to functions such as:

```
SmpExecPgm
SmpLoadDeferredSubsystem
SmpStartCsr
SmpStopCsr
SmpStartServerSilo
SmpAdoptJob
```

![SMSS dispatch algo](https://primaldesk.com/assets/blog/smss_dispatch.jpg)

The relevant operation for the remote-session path is `SmpStartCsr`. Older public material gives a useful approximation of the request shape:

```
typedef struct _SMSTARTCSR {
    ULONG MuSessionId;
    ULONG InitialCommandLength;
    WCHAR InitialCommand[SMP_MAXIMUM_INITIAL_COMMAND];
    ULONG_PTR InitialCommandProcessId;
    ULONG_PTR WindowsSubSysProcessId;
} SMSTARTCSR, *PSMSTARTCSR;
```

SMSS is asked to start the Windows subsystem for a new session and returns identifiers for the processes it created. Creating `csrss.exe` with the session-creation flag is the point where the new session itself comes into existence.

## Local Session Manager

The interesting question is how the request reaches SMSS. Among the clients connected to `SmApiPort`, one is a service-host process containing **LSM - Local Session Manager**.

![SMSS ALPC ports](https://primaldesk.com/assets/blog/smss_alpc.jpg)

At this point, the next question was straightforward: which module inside the LSM service host actually talks to SMSS? The obvious approach was to search `lsm.dll` for the `SmApiPort` string. Surprisingly, there was no such string bytes it there. So it’s better to search the address space of the running `svchost.exe` process hosting LSM. The string was present - but its address belonged to `ntdll.dll`, not `lsm.dll`.

![Port string LSM](https://primaldesk.com/assets/blog/port_string_lsm.jpg)

Following the references inside `ntdll.dll` leads to exported helper functions specifically meant for communicating with the Session Manager. One of them is `RtlConnectToSm`, which establishes the connection to SMSS, while `RtlSendMsgToSm` is used to send SMAPI requests over that connection. Internally, these helpers eventually reach the native ALPC primitives such as `NtAlpcConnectPort` and `NtAlpcSendWaitReceivePort`.

At runtime, the relevant path can be found by breaking on the `RtlSendMsgToSm` in `ntdll.dll`. A representative stack looks like this:

```
lsm!SmStartCsr
lsm!CCsrMgr::StartCsr
lsm!CTSSession::CCommonSessionData::Start
lsm!CTSSession::Start
RPCRT4!Invoke
RPCRT4!Ndr64StubWorker
combase!CStdStubBuffer_Invoke
...
```

The lower part of the stack is important: this is an incoming COM/RPC call. LSM did not spontaneously decide to create a session. It is servicing a request from another process. Because this is a **local RPC call**, the transport eventually goes through ALPC, as discussed earlier. That gives us a practical way to identify the caller. If we keep the server-side suspended, the client-side thread that issued the synchronous RPC request cannot complete either. It remains blocked while waiting for the reply, typically under `NtAlpcSendWaitReceivePort`. So we can inspect the caller process (TermSrv) directly.

In this case, attach a second debugger to the TermService process and walked through its threads, looking for one whose stack was blocked in NtAlpcSendWaitReceivePort. Once such a thread is found, the frames above it reveal the code path that issued the RPC call:

![TermSrv client-side call to LSM](https://primaldesk.com/assets/blog/term_call_to_lsm.jpg)

So the high-level creation chain is:

```
Protocol provider
    ↓
TermService
    ↓ COM/RPC
LSM
    ↓ native SM API / ALPC
SMSS
    ↓
CSRSS
```

This split makes architectural sense. TermService owns policy and lifecycle logic. LSM bridges that logic into lower-level session management. SMSS performs the privileged native operation that actually creates the new session.

The SMSS request returns two particularly useful values:

```
WindowsSubSysProcessId
InitialCommandProcessId
```

The first is the PID of the new session’s `csrss.exe`. The second is the PID of the **initial command** started after CSRSS.

For an interactive session, that initial process is normally `winlogon.exe`.

```
TermService
    ↓
LSM
    ↓
SMSS
    ↓
CSRSS created in a new session
    ↓
Winlogon started as the initial command
```

At this point, however, the graphical environment is still incomplete.

While tracing `CRCMConnDispatchManager::StartSession`, there is an interesting detail: the initial command is not necessarily hard-coded to Winlogon. TermService can supply an initial command to LSM, which then passes it to SMSS. In the default case the command is empty and SMSS uses the normal Winlogon path. But TermService also checks an `Execute` registry value and can pass an alternative command.

![Initial command override](https://primaldesk.com/assets/blog/initial_command_override.jpg)

This behavior is not part of the documented RDS extension model.

When the new `csrss.exe` starts, it initializes the Win32 subsystem for its session by calling `NtUserInitialize` through the Win32 user-mode syscall layer (`win32u.dll`). For a newly created non-console session, the graphical initialization is only partial. The session now has Win32k state, but some remote-specific pieces are deliberately not initialized yet. In particular, video-related and monitor-related state is not complete.

This intermediate state is important because **creating the session and connecting it as a remote graphical session are separate operations**.

## The remote transition

The remote-specific transition happens through `NtUserRemoteConnect`. The kernel validates that the caller is the CSRSS process associated with the target session. A random process cannot perform this transition on behalf of the session. This makes CSRSS the execution point for remote Win32k initialization inside the correct Session Space. `NtUserRemoteConnect` completes the remote-specific setup, including pieces of video and input configuration that were skipped during the initial Win32k startup.

This is also why the ordering matters.

Winlogon cannot safely assume that the whole interactive graphics environment is ready immediately after it starts. Parts of monitor and input-related Win32k state are not valid until the remote-connect phase has completed.

The control path between LSM and the per-session CSRSS is also ALPC-based. After startup, CSRSS connects to an LSM-owned port `SmSsWinStationApiPort`. This channel is commonly referred to as the **CSR pipe** in the LSM code.

![Initial command override](https://primaldesk.com/assets/blog/csrss_pipe_alpc.jpg)

Once that channel exists, LSM can send session-specific commands to CSRSS. One of those commands is `DoConnect`. This command is the point where the remote-session initialization initiated by LSM. CSRSS has already started inside the new session and established the reverse connection to LSM through the CSR pipe. Handling `DoConnect` on the CSRSS side eventually reaches `winsrvext!W32WinStationDoConnect` and ultimately invokes `NtUserRemoteConnect`.

The chain therefore becomes:

```
TermService
    ↓
LSM
    ↓ CSR pipe
CSRSS in target session
    ↓
NtUserRemoteConnect
    ↓
Win32k remote initialization
```

## Connection parameters crossing the stack

`W32WinStationDoConnect` does more than issue a syscall. It receives a block of connection parameters from LSM and forwards relevant state into Win32k.

Historical definitions make the shape of this data easier to understand:

```
typedef struct _WINSTATIONDOCONNECTMSG {
    BOOLEAN ConsoleShadowFlag;
    BOOLEAN fMouse;
    BOOLEAN fINetClient;
    BOOLEAN fInitialProgram;
    BOOLEAN fHideTitleBar;
    BOOLEAN fMaximize;
    HANDLE hIcaVideoChannel;
    HANDLE hIcaMouseChannel;
    HANDLE hIcaKeyboardChannel;
    HANDLE hIcaBeepChannel;
    HANDLE hIcaCommandChannel;
    HANDLE hIcaThinwireChannel;
    HANDLE hDisplayChangeEvent;
    WINSTATIONNAME WinStationName;
    WCHAR DisplayDriverName[9];
    WCHAR ProtocolName[9];
    WCHAR AudioDriverName[9];
    USHORT HRes;
    USHORT VRes;
    USHORT ColorDepth;
    USHORT ProtocolType;
    BOOLEAN fClientDoubleClickSupport;
    BOOLEAN fEnableWindowsKey;
    ULONG KeyboardType;
    ULONG KeyboardSubType;
    ULONG KeyboardFunctionKey;
} WINSTATIONDOCONNECTMSG;
```

The connect operation carries infrastructure-level data for the remote session. That includes input-related handles, display-related configuration, and protocol-derived state. LSM itself should not need to understand every protocol provider. Instead, it requests the current connection information from TermService, then converts that representation into the form expected by CSRSS:

![Do connect convert](https://primaldesk.com/assets/blog/do_connect_convert.jpg)

In other words, the protocol provider supplies the connection-specific state to TermService, LSM adapts that data into the format expected by CSRSS, and CSRSS finally passes the relevant parameters into Win32k during the remote-connect phase.

## Modern remote display initialization

Modern remote-session graphics use the **Indirect Display Driver** model. From the point of view of the operating system, the remote display eventually becomes a display device associated with the session. Tracing `winsrvext!W32WinStationDoConnect` shows that after the remote Win32k transition, the stack creates the remote display device node. The creation path uses a software device using `SwDeviceCreate`.

## Winlogon completes the interactive environment

Winlogon is often described primarily as the process responsible for interactive logon. It also performs a set of actions required to finish building the interactive environment.

Among them:

-   interacting with LSA during logon
-   establishing the user’s security context
-   registering itself as the logon process
-   creating the primary window station and desktops
-   participating in Secure Attention Sequence handling
-   starting components such as DWM and the font driver host
-   starting the user initialization chain

During initialization, Winlogon starts a local RPC server whose endpoint name is derived from randomy-generated id. The endpoint follows a pattern similar to WMsgKRpc…

![WinLogon starts RPC server](https://primaldesk.com/assets/blog/winlogon_server.jpg)

RPCView shows several interfaces exposed from that endpoint. Combining RPCView with Microsoft symbols in WinDbg makes it possible to map procedure addresses back to implementation names.

Some of the recovered methods are associated with:

-   secure desktop prompting
-   Secure Attention Sequence behavior
-   credential-related operations
-   message delivery between Winlogon and supporting components

This deserves a deeper article of its own, but it reinforces the same architectural theme: remote-session creation is not one monolithic function. It is a coordinated sequence across multiple privileged processes.

## Why I looked into this

This investigation came out of work on **PrimalDesk**, where I needed a more precise understanding of the Windows remote-session lifecycle than the public documentation provides.

The practical reason was simple: when building a remote desktop stack that integrates with Windows session infrastructure, “roughly how RDS works” is not enough. You eventually need to know exactly which process owns a transition, which IPC boundary it crosses, and which state already exists at that moment.

Questions or corrections are welcome: [info@primaldesk.com](mailto:info@primaldesk.com).

[← Explore more stories](https://primaldesk.com/blog.md)

## Explore PrimalDesk

- [PrimalDesk: all pages](https://primaldesk.com/llms.txt)
- [PrimalDesk Docs: all pages](https://docs.primaldesk.com/llms.txt)
- [Product overview](https://primaldesk.com/index.md)
- [Documentation](https://docs.primaldesk.com/docs.md)
- [Help center](https://docs.primaldesk.com/help-center.md)

## Other pages on this site

- [Remote access to your Windows PC | PrimalDesk](https://primaldesk.com/index.md)
- [Your Windows programs, individual windows | PrimalDesk Apps](https://primaldesk.com/apps.md)
- [Blog | PrimalDesk](https://primaldesk.com/blog.md)
- [PrimalDesk open beta test: remote access to your Windows PC](https://primaldesk.com/blog/primaldesk-beta-is-open.md)
- [What makes a remote desktop feel responsive? | PrimalDesk Journal](https://primaldesk.com/blog/what-makes-a-remote-desktop-responsive.md)
- [Inside Windows Remote Sessions: From TermService to Winlogon | PrimalDesk Journal](https://primaldesk.com/blog/windows-remote-sessions.md)
- [Download remote access software | PrimalDesk](https://primaldesk.com/download.md)
- [Multi-user Windows remote desktop features | PrimalDesk](https://primaldesk.com/features.md)
- [Stream PC games remotely | PrimalDesk](https://primaldesk.com/gaming.md)
- [Privacy notice | PrimalDesk](https://primaldesk.com/legal/privacy.md)
- [Terms of service | PrimalDesk](https://primaldesk.com/legal/terms.md)
