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:
- SetWindowsHookEx: Catch windows events of window creation. I couldn't get it working.
- Since a batch is a text file, I could analyze it and find out what executable it starts.
- ManagementEventWatcher