0

I want to start a batch file, that itself starts an exe. The exe opens with a GUI window and I would like to get that window to put it into my own panel. WinApi is no problem. If I start the exe without a batch, it works fine. But my boss wants to use batches. The process for starting the batch is opening that black cmd window and terminates quickly and triggers my OnProcessExited-event. Then the actual GUI window from Test.exe starts. I need the handle of that one. Is there an easy way to achieve this?

C#:

string cmdLine = "test.bat";
Process p = new Process();
//Process p = Process.Start(cmdLine);
if (p != null)
{
    p.EnableRaisingEvents = true;
    p.StartInfo.FileName = Path.GetFullPath(cmdLine);
    p.Exited += new EventHandler(OnProcessExited);
    p.Start();
    if (p.WaitForInputIdle(15 * 1000))
    {
        //Thread.Sleep(100);
        IntPtr wnd = p.MainWindowHandle;
        RECT rect = WindowsUtils.GetWindowRect(wnd); // this is just WinApi library
        Windows.SetParent(wnd, panelMoni.Handle);
        Windows.SetWindowPos(wnd, new IntPtr(Windows.HWND_TOP), 0, 0, rect.Width, rect.Height, 0);
    }
    else
        p.Close();
}

Batch:

START Test.exe

EDIT:

Test.exe is a simple Forms application. And as the name suggests its name is unknown. In fact I need to register a bunch of batches that run whatever exe the administrator is up to.

Possible Solutions:

  1. SetWindowsHookEx: Catch windows events of window creation. I couldn't get it working.
  2. Since a batch is a text file, I could analyze it and find out what executable it starts.
  3. ManagementEventWatcher
Bitterblue
  • 13,162
  • 17
  • 86
  • 124
  • When the batch has ended, try `Process[] pTest = Process.GetProcessesByName("test.exe");` – Oguz Ozgul Mar 18 '20 at 14:10
  • @OguzOzgul And if I don't know the name of "test.exe" I'll have to extract it from the text of the batch file? Yes, that is plan Z. I wish there would be an easier way, – Bitterblue Mar 18 '20 at 14:28
  • You did not mention that. Can you redirect the standard output of the batch and get some information from there? The batch files themselves can be written in such a way (to provide you with the name of the exe) – Oguz Ozgul Mar 18 '20 at 14:37
  • Are you open to using powershell? – shadow2020 Mar 18 '20 at 15:24
  • 1
    Or to WMI (ManagementEventWatcher) and (unfortunately) administrative privileges. – Oguz Ozgul Mar 18 '20 at 17:55
  • I tried ManagementEventWatcher and it seems to work fine. I'm not sure why prople don't try to answer. [Non-Admin](https://stackoverflow.com/a/58738175/1442225) – Bitterblue Mar 19 '20 at 07:01
  • interesting, so basically: console app > Batch > GUI App,and you want the consoleapp to get the WindowHandle of GUIApp, is that right ? – Clint Mar 19 '20 at 18:01
  • @Clint No, it's GUIApp > Batch > GUIApp. See above, there is a WinForms panel. – Bitterblue Mar 20 '20 at 06:59
  • okay, and you want GUIApp1 to get the Window handle of GUIApp2, right ? – Clint Mar 20 '20 at 07:02

0 Answers0