C# 尝试从父进程钩住子进程创建的对话框

C# 尝试从父进程钩住子进程创建的对话框,c#,.net,C#,.net,我编写了一个小实用程序,它使用MSRA.exe创建远程帮助邀请文件,并立即开始侦听帮助台环境的帮助请求。应用程序运行良好,线程MSRA.exe对用户完全不可见,但我的一个问题是,如果用户通过终止进程或其他方式不正确地终止连接,那么下次运行我的应用程序并调用MSRA.exe时,它会发送一个对话框,说明用户没有正确关闭上一个连接,桌面设置没有恢复,他们希望现在恢复桌面设置吗?既然我已经隐藏了MSRA,对话框也被隐藏了,并且在等待响应时停止MSRA程序,有没有办法检查这个对话框并强制回答 using

我编写了一个小实用程序,它使用MSRA.exe创建远程帮助邀请文件,并立即开始侦听帮助台环境的帮助请求。应用程序运行良好,线程MSRA.exe对用户完全不可见,但我的一个问题是,如果用户通过终止进程或其他方式不正确地终止连接,那么下次运行我的应用程序并调用MSRA.exe时,它会发送一个对话框,说明用户没有正确关闭上一个连接,桌面设置没有恢复,他们希望现在恢复桌面设置吗?既然我已经隐藏了MSRA,对话框也被隐藏了,并且在等待响应时停止MSRA程序,有没有办法检查这个对话框并强制回答

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Threading;
using System.Management;
using System.Runtime.InteropServices;
using System.Diagnostics;

namespace WindowsFormsApplication2
{

    public partial class Form1 : Form
    {
        System.Diagnostics.Process p = new System.Diagnostics.Process();
        public Form1()
        {
            InitializeComponent();
        }

        public void EndSession()
        {
            if (button1.Text != "Launch Connection")
            {
                button1.Text = "Closing Connection...";
                this.Refresh();
                if (p.HasExited == false)
                {
                    KillProcessAndChildren(p.Id);
                }
                if (File.Exists(System.IO.Path.GetTempPath() + "Invitation.msrcincident") == true)
                {
                    File.Delete(System.IO.Path.GetTempPath() + "Invitation.msrcincident");
                }
            }
            Application.Exit();
        }

        private void KillProcessAndChildren(int pid)
        {
            using (var searcher = new ManagementObjectSearcher("Select * From Win32_Process Where ParentProcessID=" + pid))
            using (ManagementObjectCollection moc = searcher.Get())
            {
                foreach (ManagementObject mo in moc)
                {
                    KillProcessAndChildren(Convert.ToInt32(mo["ProcessID"]));
                }
                try
                {
                    Process proc = Process.GetProcessById(pid);
                    proc.CloseMainWindow();
                    if (!proc.WaitForExit((int)TimeSpan.FromSeconds(3).TotalMilliseconds))
                    {
                        proc.Kill();
                    }
                }
                catch (ArgumentException)
                { /* process already exited */ }
            }
        }

        public void button1_Click(object sender, EventArgs e)
        {
            if (button1.Text == "Launch Connection")
            {
                button1.Text = "Opening Connection...";
                this.Refresh();
                if (File.Exists(System.IO.Path.GetTempPath() + "Invitation.msrcincident") == true)
                {
                    File.Delete(System.IO.Path.GetTempPath() + "Invitation.msrcincident");
                }

                string fileurl = System.IO.Path.GetTempPath() + "Invitation.msrcincident";
                p.StartInfo.UseShellExecute = true;
                p.StartInfo.RedirectStandardOutput = false;
                p.StartInfo.CreateNoWindow = true;
                p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
                p.StartInfo.FileName = "Msra.exe";
                p.StartInfo.Arguments = "/saveasfile " + fileurl + " MyPass";
                Console.WriteLine(p.StartInfo.Arguments);
                p.Start();

                while (File.Exists(System.IO.Path.GetTempPath() + "Invitation.msrcincident") == false)
                {
                    Thread.Sleep(1000);
                }
                FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create("ftp://ftp.MyFTP.net/" + System.Environment.UserName + "." + Dns.GetHostName() + ".msrcincident");
                request.Method = WebRequestMethods.Ftp.UploadFile;
                request.Credentials = new NetworkCredential("MyUser", "MyPass");
                request.UsePassive = true;
                request.UseBinary = true;
                request.KeepAlive = false;
                Stream ftpstream = request.GetRequestStream();


                FileStream stream = File.OpenRead(fileurl);
                int length = 1024;
                byte[] buffer = new byte[length];
                int bytesRead = 0;
                do
                {
                    bytesRead = stream.Read(buffer, 0, length);
                    ftpstream.Write(buffer, 0, bytesRead);
                } while (bytesRead != 0);

                stream.Close();
                ftpstream.Close();
                button1.Text = "Connection Open";
            }
            else
            {
                EndSession();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            EndSession();

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            textBox1.Text = System.Environment.UserName;
            textBox2.Text = Dns.GetHostName();

            Process[] processes = Process.GetProcessesByName("msra");

            foreach (Process process in processes)
            {
                process.Kill();
            }

        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            EndSession();
        }
    }
}

如果您询问是否可以检查某个特定对话框是否打开,答案当然是,您只需“查找”该对话框的标题即可。这意味着在一段时间内查看所有打开的窗口。实际上,我也必须钩住它,因为我想强制对话框响应,因为它是不可见的。如果不能够触发响应,就无法知道它是否存在。我认为答案在于将信息挂钩,但到目前为止,我找到的关于这一点的文档充其量也不完整。