protected override void OnVisibleChanged(EventArgs e)
{
//Log.Debug("OnVisibleChanged");
if (!m_Created && !String.IsNullOrEmpty(ApplicationName)) // only allow one instance of the child
{
m_Created = true;
m_AppWin = IntPtr.Zero;
try
{
if(!File.Exists(ApplicationName))
{
MessageBox.Show("putty.exe not found in configured path, please go into tools->settings and set the correct path", "Application Not Found");
return;
}
m_Process = new Process();
m_Process.EnableRaisingEvents = true;
//m_Process.Exited += new EventHandler(p_Exited);
m_Process.StartInfo.FileName = ApplicationName;
m_Process.StartInfo.Arguments = ApplicationParameters;
if (!string.IsNullOrEmpty(this.ApplicationWorkingDirectory) &&
Directory.Exists(this.ApplicationWorkingDirectory))
{
m_Process.StartInfo.WorkingDirectory = this.ApplicationWorkingDirectory;
}
m_Process.Exited += delegate(object sender, EventArgs ev)
{
m_CloseCallback(true);
};
m_Process.Start();
// Wait for application to start and become idle
m_Process.WaitForInputIdle();
m_AppWin = m_Process.MainWindowHandle;
if (IntPtr.Zero == m_AppWin)
{
Log.WarnFormat("Unable to get handle for process on first try.{0}", LoopWaitForHandle ? " Polling 10 s for handle." : "");
if (LoopWaitForHandle)
{
DateTime startTime = DateTime.Now;
while ((DateTime.Now - startTime).TotalSeconds < 10)
{
System.Threading.Thread.Sleep(50);
// Refresh Process object's view of real process
m_Process.Refresh();
m_AppWin = m_Process.MainWindowHandle;
if (IntPtr.Zero != m_AppWin)
{
Log.Info("Successfully found handle via polling " + (DateTime.Now - startTime).TotalMilliseconds + " ms");
break;
}
}
}
}
}
catch (InvalidOperationException ex)
{
/* Possible Causes:
* No file name was specified in the Process component's StartInfo.
* -or-
* The ProcessStartInfo.UseShellExecute member of the StartInfo property is true while ProcessStartInfo.RedirectStandardInput,
* ProcessStartInfo.RedirectStandardOutput, or ProcessStartInfo.RedirectStandardError is true.
*/
MessageBox.Show(this, ex.Message, "Invalid Operation Error");
throw;
}
catch (Win32Exception ex)
{
/*
* Checks are elsewhere to ensure these don't occur, but incase they do we're gonna bail with a nasty exception
* which will hopefully send users kicking and screaming at me to fix this (And hopefully they will include a
* stacktrace!)
*/
if (ex.NativeErrorCode == NativeMethods.ERROR_ACCESS_DENIED)
{
throw;
}
else if (ex.NativeErrorCode == NativeMethods.ERROR_FILE_NOT_FOUND)
{
throw;
}
}
if ("PuTTY Command Line Error" == this.m_Process.MainWindowTitle)
{
// dont' try to capture or manipulate the window
Log.WarnFormat("Error while creating putty session: title={0}, handle={1}. Abort capture window", this.m_Process.MainWindowTitle, this.m_AppWin);
this.m_AppWin = IntPtr.Zero;
}
else
{
//Logger.Log("Process Handle: {0}", m_AppWin.ToString("X"));
// Set the application as a child of the parent form
NativeMethods.SetParent(m_AppWin, this.Handle);
// Show it! (must be done before we set the windows visibility parameters below
NativeMethods.ShowWindow(m_AppWin, NativeMethods.WindowShowStyle.Maximize);
// set window parameters (how it's displayed)
long lStyle = NativeMethods.GetWindowLong(m_AppWin, NativeMethods.GWL_STYLE);
lStyle &= ~(NativeMethods.WS_BORDER | NativeMethods.WS_THICKFRAME);
NativeMethods.SetWindowLong(m_AppWin, NativeMethods.GWL_STYLE, lStyle);
}
}
if (this.Visible && this.m_Created && this.ExternalProcessCaptured)
{
// Move the child so it's located over the parent
this.MoveWindow("OnVisChanged");
//MoveWindow(m_AppWin, 0, 0, this.Width, this.Height, true);
if (RefocusOnVisChanged && NativeMethods.GetForegroundWindow() != this.m_AppWin)
{
this.BeginInvoke(new MethodInvoker(delegate { this.ReFocusPuTTY("OnVisChanged"); }));
}
}
base.OnVisibleChanged(e);
}