C# Process.MainWindowHandle 总是返回 IntPtr 零 [英] C# Process.MainWindowHandle always returns IntPtr Zero

查看:30
本文介绍了C# Process.MainWindowHandle 总是返回 IntPtr 零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

            using (Process game = Process.Start(new ProcessStartInfo() { 
        FileName="DatabaseCheck.exe",
        RedirectStandardOutput = true,
        CreateNoWindow = true,
        UseShellExecute = false }))
        {
            lblLoad.Text = "Loading";
            int Switch = 0;

            while (game.MainWindowHandle == IntPtr.Zero)
            {
                Switch++;
                if (Switch % 1000 == 0)
                {
                    lblLoad.Text += ".";
                    if (lblLoad.Text.Contains("...."))
                        lblLoad.Text = "Loading.";

                    lblLoad.Update();
                    game.Refresh();
                }
            }

问题是,game.MainWindowHandle 总是 IntPtr.Zero.我需要找到运行进程的 IntPtr 以确认游戏是由启动器启动的,所以我让游戏发送它的 IntPtr 并让启动器响应(如果可以的话).但为此,我必须具体知道run进程的IntPtr.

Problem is, that game.MainWindowHandle is always IntPtr.Zero. I need to find the IntPtr of the ran process to confirm that the game was started by the launcher, so I made the game send it's IntPtr and let the launcher respond if it's okay. But for that, I must know the IntPtr of the ran process specifically.

提前致谢!

推荐答案

主窗口是当前具有焦点的进程(TopLevel 窗体)打开的窗口.您必须使用 Refresh 方法来刷新 Process 对象,以获取当前主窗口句柄(如果它已更改).

The main window is the window opened by the process that currently has the focus (the TopLevel form). You must use the Refresh method to refresh the Process object to get the current main window handle if it has changed.

您只能为在本地计算机上运行的进程获取 MainWindowHandle 属性.MainWindowHandle 属性是唯一标识与进程关联的窗口的值.

You can get the MainWindowHandle property only for processes that are running on the local computer. The MainWindowHandle property is a value that uniquely identifies the window that is associated with the process.

仅当进程具有图形界面时,该进程才具有与其关联的主窗口.如果关联进程没有主窗口,则 MainWindowHandle 值为零.对于已隐藏的进程,即在任务栏中不可见的进程,该值也为零.对于在任务栏最右侧的通知区域中显示为图标的进程,可能就是这种情况.

A process has a main window associated with it only if the process has a graphical interface. If the associated process does not have a main window, the MainWindowHandle value is zero. The value is also zero for processes that have been hidden, that is, processes that are not visible in the taskbar. This can be the case for processes that appear as icons in the notification area, at the far right of the taskbar.

如果您刚刚启动了一个进程并想使用它的主窗口句柄,请考虑使用 WaitForInputIdle 方法让进程完成启动,确保主窗口句柄已创建.否则会抛出异常.

这篇关于C# Process.MainWindowHandle 总是返回 IntPtr 零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆