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

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

问题描述

这是我的code:

            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。我需要找到RAN过程IntPtr的确认,该游戏是由发射器起家,所以我做游戏发送它的IntPtr,让发射器响应,如果它没关系。但对于这一点,我必须知道跑过程中的具体的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.

在此先感谢!

推荐答案

主窗口是由当前具有焦点(顶层形式)的过程中打开了窗子。您必须使用刷新方法来刷新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天全站免登陆