babalae/better-genshin-impact · error · InvalidOperationException

当前没有可用的桌面分身,请先启动桌面分身。

Error message

当前没有可用的桌面分身,请先启动桌面分身。

What it means

Thrown by ChildSessionService.GetRequiredChildSessionId when ChildSessionNativeMethods.TryGetChildSessionId() returns null — no Windows Child Session (desktop clone) is currently active, so there is no target session ID to launch a process into.

Source

Thrown at BetterGenshinImpact/Service/ChildSession/ChildSessionService.cs:527

            RefreshState(isAutomatic
                ? "桌面分身已加载,正在自动以管理员权限启动 BetterGI"
                : "正在以管理员权限启动 BetterGI");
            await ChildSessionProcessLauncher.LaunchBetterGiAsync(childSessionId);
            RefreshState(
                $"已在桌面分身(会话 {childSessionId})中以管理员权限启动 BetterGI");
        }
        finally
        {
            _launchSemaphore.Release();
        }
    }

    private uint GetRequiredChildSessionId()
    {
        var childSessionId = ChildSessionNativeMethods.TryGetChildSessionId();
        if (childSessionId is null)
        {
            throw new InvalidOperationException("当前没有可用的桌面分身,请先启动桌面分身。");
        }

        return childSessionId.Value;
    }

    private void EnsureChildSessionsEnabled()
    {
        if (!ChildSessionNativeMethods.IsChildSessionsEnabled())
        {
            ChildSessionNativeMethods.EnableChildSessions();
        }
    }

    private void OnDesktopWindowVisibilityChanged(object sender, DependencyPropertyChangedEventArgs e)
    {
        RefreshState();
    }

View on GitHub (pinned to a7cb36712d)

Solutions

  1. Call StartAsync() first and wait for the Child Session to reach ConnectedState == 1 (logged in).
  2. Ensure Child Sessions are enabled on the OS (EnsureChildSessionsEnabled runs automatically during StartAsync).
  3. If the session was logged off, restart it before launching programs into it.
Defensive patterns

Strategy: validation

Validate before calling

// Ensure a Child Session is active before launching into it.
if (ChildSessionNativeMethods.TryGetChildSessionId() is null)
{
    throw new InvalidOperationException("当前没有可用的桌面分身,请先启动桌面分身。");
}

Try / catch

try
{
    await childSessionService.LaunchExecutableAsync(exePath);
}
catch (InvalidOperationException ex) when (ex.Message.Contains("桌面分身"))
{
    logger.LogError(ex, "No active Child Session; start one first");
}

Prevention

When it happens

Trigger: Calling LaunchBetterGiAsync() or LaunchExecutableAsync() before the RDP Child Session connection has been established and logged in. The Child Session only gets a session ID after Windows assigns one during the RDP logon sequence.

Common situations: User clicks "launch program into Child Session" before clicking "start desktop clone"; the Child Session was logged off or terminated out-of-band; the RDP connection is still in the connecting state (ConnectedState != 1).

Related errors


AI-assisted analysis of babalae/better-genshin-impact@a7cb36712d (2026-08-13). Data as JSON: /api/errors/d545c9336232520b. Report an issue: GitHub.