简体   繁体   English

通过Win形式C#通过批处理文件执行.exe时,Process.MainWindowHandle为零

[英]Process.MainWindowHandle is zero when executing .exe through batch file in win form c#

exe application in winform window. exe应用程序在Winform窗口中。 when i call the .exe directly using process class, i got the handle and application get attached with form window. 当我直接使用流程类调用.exe时,我得到了句柄,并且应用程序与窗体窗口连接在一起。 but when i call the same .exe through .bat file using the process class, MainWindowhandle is always zero and .exe opens as separate window. 但是当我使用进程类通过.bat文件调用相同的.exe时,MainWindowhandle始终为零,并且.exe​​作为单独的窗口打开。 Also WaitForInputIdle is not working thus i am using thread sleep which is working fine. 另外,WaitForInputIdle无法正常工作,因此我正在使用正常工作的线程睡眠。 the part of code is shown:- 代码部分如下所示:

              Process p = null;                
              p = Process.Start(@"E:\hude\test.bat");

//test.bat has following text:- START "" "E:\\hude\\PROJECTION.exe" //test.bat具有以下文本:-START“”“ E:\\ hude \\ PROJECTION.exe”

           // p = Process.Start(@"E:\hude\PROJECTION.exe");

            //wait for the process to be created and enter idle condition
            //p.WaitForInputIdle(1000);
            if (p.HasExited == false)
            {
                string name = p.ProcessName;
                MessageBox.Show("Name = " + name);
                System.Threading.Thread.Sleep(5000);

            }
            //wait for the process to be created and enter idle condition
            //p.WaitForInputIdle(1000);

            //p.Refresh(); 
            // Get the main handle
            appWin = p.MainWindowHandle;
            if (appWin == IntPtr.Zero)
                MessageBox.Show("handle not created ");
            else
                MessageBox.Show("handle created ");

The process object you are holding on to is the process that represents the .BAT file process. 您要保留的流程对象是代表.BAT文件流程的流程。 Since the bat file spawns another process, you need to get at that process in order to check the main window handle. 由于bat文件产生了另一个进程,因此您需要进入该进程才能检查主窗口句柄。 You could use GetProcesses or GetProcessesByName to find the process spawned by the batch file. 您可以使用GetProcessesGetProcessesByName查找批处理文件产生的进程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM