babalae/better-genshin-impact · error · ArgumentException

通过句柄获取游戏进程失败

Error message

通过句柄获取游戏进程失败

What it means

Thrown by the SystemInfo constructor when SystemControl.GetProcessByHandle(hWnd) returns null, meaning the Win32 handle could not be resolved to a Process object. This is an ArgumentException because the handle argument is effectively invalid or the process is inaccessible.

Source

Thrown at BetterGenshinImpact/GameTask/Model/SystemInfo.cs:62

        public RECT CaptureAreaRect { get; set; }

        /// <summary>
        /// 捕获窗口区域 大于1080P则为1920x1080
        /// </summary>
        public Rect ScaleMax1080PCaptureRect { get; set; }

        public Process GameProcess { get; }

        public string GameProcessName { get; }

        public int GameProcessId { get; }

        public DesktopRegion DesktopRectArea { get; }

        public SystemInfo(IntPtr hWnd)
        {
            var p = SystemControl.GetProcessByHandle(hWnd);
            GameProcess = p ?? throw new ArgumentException("通过句柄获取游戏进程失败");
            GameProcessName = GameProcess.ProcessName;
            GameProcessId = GameProcess.Id;

            DisplaySize = PrimaryScreen.WorkingArea;
            DesktopRectArea = new DesktopRegion();

            // 判断最小化
            if (User32.IsIconic(hWnd))
            {
                throw new ArgumentException("游戏窗口不能最小化");
            }

            // 注意截图区域要和游戏窗口实际区域一致
            // todo 窗口移动后?
            GameScreenSize = SystemControl.GetGameScreenRect(hWnd);
            if (GameScreenSize.Width < 800 || GameScreenSize.Height < 600)
            {
                throw new ArgumentException("游戏窗口分辨率不得小于 800x600 !");

View on GitHub (pinned to a7cb36712d)

Solutions

  1. Verify the game is running and the window handle is valid before constructing SystemInfo.
  2. Re-acquire the window handle immediately before use, rather than caching it for long periods.
  3. Run the application as administrator if process access is denied.
  4. Catch ArgumentException at the call site and prompt the user to restart the game.

Example fix

// before
var p = SystemControl.GetProcessByHandle(hWnd);
GameProcess = p ?? throw new ArgumentException("通过句柄获取游戏进程失败");

// after
var p = SystemControl.GetProcessByHandle(hWnd);
if (p == null)
{
    throw new ArgumentException(
        $"通过句柄获取游戏进程失败,句柄:{hWnd},游戏可能已退出或句柄无效");
}
GameProcess = p;
Defensive patterns

Strategy: try-catch

Validate before calling

var process = SystemControl.GetProcessByHandle(hWnd);
if (process == null)
{
    logger.LogError("无法通过句柄获取游戏进程,请确认游戏正在运行");
    return;
}

Try / catch

try { var info = new SystemInfo(hWnd); }
catch (ArgumentException ex) when (ex.Message.Contains("游戏进程"))
{ logger.LogError(ex, "游戏可能已退出,请重新启动游戏"); }

Prevention

When it happens

Trigger: Constructing SystemInfo with an IntPtr hWnd that does not correspond to a running process, or where the process has already exited. Also occurs when the process lacks sufficient permissions to be queried, or the handle is stale.

Common situations: The game (Genshin Impact) was closed between the time the handle was obtained and the SystemInfo construction; the handle was captured from the wrong window; running without sufficient OS privileges to inspect the target process.

Related errors


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