Files
mRemoteNG/mRemoteV1/Tools/ExternalTool.cs
Sean Kaim a792c98630 3G port-Avoid exceptions starting an empty ExtApp
Reference: https://github.com/kmscode/mRemote3G/issues/10

In my testing the crash was semi-random. The exception was thrown,
logged and put into Notifications (all as expected). But every now and
then, would crash (with no further info available). Could not reproduce
in a debug build.

b248849428
2016-05-18 21:56:39 -04:00

136 lines
3.7 KiB
C#

using System.Collections.Generic;
using System;
using System.Drawing;
using System.Diagnostics;
using mRemoteNG.App;
using System.IO;
using System.ComponentModel;
using mRemoteNG.Connection;
using mRemoteNG.Connection.Protocol;
using mRemoteNG.My;
namespace mRemoteNG.Tools
{
public class ExternalTool
{
#region Public Properties
public string DisplayName { get; set; }
public string FileName { get; set; }
public bool WaitForExit { get; set; }
public string Arguments { get; set; }
public bool TryIntegrate { get; set; }
public ConnectionInfo ConnectionInfo { get; set; }
public Icon Icon
{
get
{
if (File.Exists(FileName))
return MiscTools.GetIconFromFile(FileName);
else
return null;
}
}
public Image Image
{
get
{
if (Icon != null)
return Icon.ToBitmap();
else
return Resources.mRemote_Icon.ToBitmap();
}
}
#endregion
public ExternalTool(string displayName = "", string fileName = "", string arguments = "")
{
this.DisplayName = displayName;
this.FileName = fileName;
this.Arguments = arguments;
}
public void Start(ConnectionInfo startConnectionInfo = null)
{
try
{
if (string.IsNullOrEmpty(FileName))
{
Runtime.MessageCollector.AddMessage(Messages.MessageClass.ErrorMsg, "ExternalApp.Start() failed: FileName cannot be blank.", false);
return;
}
ConnectionInfo = startConnectionInfo;
if (TryIntegrate)
StartIntegrated();
else
StartExternalProcess();
}
catch (Exception ex)
{
Runtime.MessageCollector.AddExceptionMessage("ExternalApp.Start() failed.", ex);
}
}
private void StartExternalProcess()
{
Process process = new Process();
SetProcessProperties(process, ConnectionInfo);
process.Start();
if (WaitForExit)
{
process.WaitForExit();
}
}
private void SetProcessProperties(Process process, ConnectionInfo startConnectionInfo)
{
ArgumentParser argParser = new ArgumentParser(startConnectionInfo);
process.StartInfo.UseShellExecute = true;
process.StartInfo.FileName = argParser.ParseArguments(FileName);
process.StartInfo.Arguments = argParser.ParseArguments(Arguments);
}
public void StartIntegrated()
{
try
{
ConnectionInfo newConnectionInfo = BuildConnectionInfoForIntegratedApp();
Runtime.OpenConnection(newConnectionInfo);
}
catch (Exception ex)
{
Runtime.MessageCollector.AddExceptionMessage(message: "ExternalApp.StartIntegrated() failed.", ex: ex, logOnly: true);
}
}
private ConnectionInfo BuildConnectionInfoForIntegratedApp()
{
ConnectionInfo newConnectionInfo = GetAppropriateInstanceOfConnectionInfo();
SetConnectionInfoFields(newConnectionInfo);
return newConnectionInfo;
}
private ConnectionInfo GetAppropriateInstanceOfConnectionInfo()
{
ConnectionInfo newConnectionInfo = default(ConnectionInfo);
if (this.ConnectionInfo == null)
newConnectionInfo = new ConnectionInfo();
else
newConnectionInfo = this.ConnectionInfo.Copy();
return newConnectionInfo;
}
private void SetConnectionInfoFields(ConnectionInfo newConnectionInfo)
{
newConnectionInfo.Protocol = ProtocolType.IntApp;
newConnectionInfo.ExtApp = DisplayName;
newConnectionInfo.Name = DisplayName;
newConnectionInfo.Panel = Language.strMenuExternalTools;
}
}
}