View on GitHub

SARAFF SOFTWARE

All products / Saraff.Twain.Aux

Service Hosting

TWAIN and the DSM UI require an interactive desktop. A Windows service, IIS application or other session-0 process cannot show TWAIN UI directly. Saraff.Twain.Aux solves this by starting the host as the interactive desktop user.

Launch modes

TwainAuxClientOptions.LaunchMode selects how the host process starts.

flowchart TB
    CONNECT["ConnectAsync"] --> FACTORY{"TransportFactory set?"}
    FACTORY -->|"yes"| CUSTOM["Use custom transport"]
    FACTORY -->|"no"| MODE{"LaunchMode"}
    MODE -->|"CurrentUser"| CURRENT["ProcessTransport.Start<br/>current process user"]
    MODE -->|"InteractiveUser"| INTERACTIVE["ImpersonatedProcessTransport.Start"]
    MODE -->|"Auto"| SESSION{"Interactive session?"}
    SESSION -->|"yes"| CURRENT
    SESSION -->|"no, session 0 / service"| INTERACTIVE
    INTERACTIVE -->|"failure"| FALLBACK{"FallbackToCurrentUser?"}
    FALLBACK -->|"true"| CURRENT
    FALLBACK -->|"false, default"| FAIL["Throw; do not start in session 0"]
Mode Behavior
Auto (default) Interactive session -> current user. Non-interactive/session 0 -> interactive desktop user
CurrentUser Always start as the current process user
InteractiveUser Always use the interactive desktop user (useful for testing from a console)

FallbackToCurrentUser is false by default. A service should fail clearly instead of starting a host in session 0 where TWAIN UI cannot appear. Set it to true only for non-UI scenarios.

The interactive-session check is:

Environment.UserInteractive && Process.GetCurrentProcess().SessionId != 0

Service path

flowchart LR
    subgraph Session0["Session 0: service / IIS"]
        SERVICE["Service process<br/>SYSTEM or service account"]
        CLIENT["TwainAuxClient<br/>LaunchMode = Auto"]
    end

    subgraph Desktop["Interactive session (winsta0\\default)"]
        HOST["Host process<br/>interactive user token"]
        TWAIN["Twain32 + DSM"]
        UI["TWAIN / scanner UI"]
    end

    SERVICE --> CLIENT
    CLIENT -->|"CreateProcessAsUser<br/>redirected stdio"| HOST
    HOST --> TWAIN --> UI

The service keeps the client side. The host starts on the interactive window station and desktop, so the DSM and scanner dialogs are visible to the logged-in user.

Impersonation sequence

ImpersonatedProcessTransport.Start uses CreateProcessAsUser with a primary token for the active console session.

sequenceDiagram
    participant Service as Service process
    participant WTS as WTS / explorer
    participant Transport as ImpersonatedProcessTransport
    participant Host as Host process
    participant Desktop as Interactive desktop

    Service->>Transport: Start(hostPath)
    Transport->>Transport: Enable SeAssignPrimaryTokenPrivilege<br/>and SeIncreaseQuotaPrivilege
    Transport->>WTS: WTSGetActiveConsoleSessionId
    alt WTSQueryUserToken succeeds
        WTS-->>Transport: session token
    else fallback
        Transport->>WTS: find explorer.exe in session
        WTS-->>Transport: process token
        Transport->>Transport: DuplicateTokenEx -> primary token
        Transport->>Transport: SetTokenInformation(session id)
    end
    Transport->>Transport: CreateEnvironmentBlock
    Transport->>Transport: create stdio pipes<br/>only child ends inheritable
    Transport->>Host: CreateProcessAsUser on winsta0\default
    Host->>Desktop: TWAIN UI can appear
    Host-->>Service: JSON-RPC over redirected stdio

Key details:

Security model

flowchart TB
    subgraph ServiceSide["Client process"]
        CUSER["Client user SID"]
        CSESSION["TwainAuxClientSession"]
        CPIPE["NativePipeServer<br/>PipeSecurity"]
        CDIR["Session transfer directory<br/>DirectorySecurity"]
    end

    subgraph HostSide["Host process"]
        HUSER["Host user SID"]
        HINFO["HostInfo.UserSid"]
        FPOLICY["FileTransferPolicy"]
    end

    CUSER -->|"HelloRequest.ClientUserSid"| HINFO
    HUSER -->|"HostInfo.UserSid"| CSESSION
    CSESSION --> CPIPE
    CSESSION --> CDIR
    FPOLICY --> CDIR
Resource Access
JSON-RPC stream Child-process stdin/stdout; no network listener
Named pipe server Client user full control; host user read/write when SIDs differ
Transfer directory Client user full control; host user modify when SIDs differ
File-transfer root Host user full control; client user modify when SIDs differ
File paths Must stay inside the allowed root; reparse points rejected

The client and host exchange their SIDs during HelloAsync. The client passes its SID to the host, and HostInfo.UserSid tells the client which SID the host runs as. If both processes run as the same user, the extra ACL entries are skipped.

File path policy

FileTransferPolicy is intentionally strict:

The extension is derived from the TWAIN file format (BMP, TIFF/TIF, JPEG/JPG, PNG) and falls back to .img.

Service checklist

Run an elevated PowerShell probe:

powershell -ExecutionPolicy Bypass -File .\scripts\test-service-impersonation.ps1

Expected result:

OK protocol=2.3 bitness=x64 userSid=S-1-5-21-... dsmOpen=False

userSid must be the interactive user’s SID, not S-1-5-18 (SYSTEM). The probe performs a handshake and a state request; it does not scan. Add an acquisition to the probe for full service UI validation.

Checklist:

Limitations

Next