2014-05-09 11:08:30 +12:00
|
|
|
|
#region License Information (GPL v3)
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
ShareX - A program that allows you to take screenshots and share any file type
|
2019-01-02 20:43:52 +13:00
|
|
|
|
Copyright (c) 2007-2019 ShareX Team
|
2014-05-09 11:08:30 +12:00
|
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
|
|
|
|
|
Optionally you can also view the license at <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#endregion License Information (GPL v3)
|
|
|
|
|
|
|
|
|
|
using System;
|
2014-05-08 21:06:53 +12:00
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.IO;
|
2014-08-29 17:09:31 +12:00
|
|
|
|
using System.Text;
|
2014-05-08 21:06:53 +12:00
|
|
|
|
|
2014-12-11 09:25:20 +13:00
|
|
|
|
namespace ShareX.HelpersLib
|
2014-05-08 21:06:53 +12:00
|
|
|
|
{
|
|
|
|
|
public abstract class ExternalCLIManager : IDisposable
|
|
|
|
|
{
|
2014-05-09 11:08:30 +12:00
|
|
|
|
public event DataReceivedEventHandler OutputDataReceived;
|
|
|
|
|
public event DataReceivedEventHandler ErrorDataReceived;
|
2014-05-08 21:06:53 +12:00
|
|
|
|
|
2015-09-15 19:00:38 +12:00
|
|
|
|
protected Process process;
|
2015-09-23 06:50:41 +12:00
|
|
|
|
protected bool processRunning;
|
2014-05-08 21:06:53 +12:00
|
|
|
|
|
2014-05-09 13:16:43 +12:00
|
|
|
|
public virtual int Open(string path, string args = null)
|
2014-05-08 21:06:53 +12:00
|
|
|
|
{
|
2014-05-09 13:16:43 +12:00
|
|
|
|
if (File.Exists(path))
|
2014-05-09 03:17:42 +12:00
|
|
|
|
{
|
2015-06-03 23:32:34 +12:00
|
|
|
|
using (process = new Process())
|
|
|
|
|
{
|
2018-12-07 07:51:41 +13:00
|
|
|
|
ProcessStartInfo psi = new ProcessStartInfo()
|
|
|
|
|
{
|
|
|
|
|
FileName = path,
|
|
|
|
|
WorkingDirectory = Path.GetDirectoryName(path),
|
|
|
|
|
Arguments = args,
|
|
|
|
|
UseShellExecute = false,
|
|
|
|
|
CreateNoWindow = true,
|
|
|
|
|
RedirectStandardInput = true,
|
|
|
|
|
RedirectStandardOutput = true,
|
|
|
|
|
RedirectStandardError = true,
|
|
|
|
|
StandardOutputEncoding = Encoding.UTF8,
|
|
|
|
|
StandardErrorEncoding = Encoding.UTF8
|
|
|
|
|
};
|
2014-05-09 01:28:46 +12:00
|
|
|
|
|
2015-06-03 23:32:34 +12:00
|
|
|
|
process.EnableRaisingEvents = true;
|
|
|
|
|
if (psi.RedirectStandardOutput) process.OutputDataReceived += cli_OutputDataReceived;
|
|
|
|
|
if (psi.RedirectStandardError) process.ErrorDataReceived += cli_ErrorDataReceived;
|
|
|
|
|
process.StartInfo = psi;
|
2018-12-07 07:51:41 +13:00
|
|
|
|
|
|
|
|
|
DebugHelper.WriteLine($"CLI: \"{psi.FileName}\" {psi.Arguments}");
|
2015-06-03 23:32:34 +12:00
|
|
|
|
process.Start();
|
2018-12-07 07:51:41 +13:00
|
|
|
|
|
2015-06-03 23:32:34 +12:00
|
|
|
|
if (psi.RedirectStandardOutput) process.BeginOutputReadLine();
|
|
|
|
|
if (psi.RedirectStandardError) process.BeginErrorReadLine();
|
2015-09-23 06:50:41 +12:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
processRunning = true;
|
|
|
|
|
process.WaitForExit();
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
processRunning = false;
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-03 23:32:34 +12:00
|
|
|
|
return process.ExitCode;
|
|
|
|
|
}
|
2014-05-09 11:08:30 +12:00
|
|
|
|
}
|
2014-05-09 03:17:42 +12:00
|
|
|
|
|
2014-05-09 11:08:30 +12:00
|
|
|
|
return -1;
|
|
|
|
|
}
|
2014-05-08 21:06:53 +12:00
|
|
|
|
|
2014-05-09 11:08:30 +12:00
|
|
|
|
private void cli_OutputDataReceived(object sender, DataReceivedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (e.Data != null)
|
|
|
|
|
{
|
|
|
|
|
if (OutputDataReceived != null)
|
|
|
|
|
{
|
|
|
|
|
OutputDataReceived(sender, e);
|
|
|
|
|
}
|
2014-05-08 21:06:53 +12:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-09 11:08:30 +12:00
|
|
|
|
private void cli_ErrorDataReceived(object sender, DataReceivedEventArgs e)
|
2014-05-08 21:06:53 +12:00
|
|
|
|
{
|
2014-05-09 11:08:30 +12:00
|
|
|
|
if (e.Data != null)
|
2014-05-08 21:06:53 +12:00
|
|
|
|
{
|
2014-05-09 11:08:30 +12:00
|
|
|
|
if (ErrorDataReceived != null)
|
|
|
|
|
{
|
|
|
|
|
ErrorDataReceived(sender, e);
|
|
|
|
|
}
|
2014-05-08 21:06:53 +12:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-09 11:08:30 +12:00
|
|
|
|
public void WriteInput(string input)
|
2014-05-08 21:06:53 +12:00
|
|
|
|
{
|
2015-09-23 06:50:41 +12:00
|
|
|
|
if (processRunning && process != null && process.StartInfo != null && process.StartInfo.RedirectStandardInput)
|
2014-05-09 11:08:30 +12:00
|
|
|
|
{
|
|
|
|
|
process.StandardInput.WriteLine(input);
|
|
|
|
|
}
|
2014-05-08 21:06:53 +12:00
|
|
|
|
}
|
|
|
|
|
|
2014-05-09 11:08:30 +12:00
|
|
|
|
public virtual void Close()
|
2014-05-08 21:06:53 +12:00
|
|
|
|
{
|
2015-09-23 06:50:41 +12:00
|
|
|
|
if (processRunning && process != null)
|
2014-05-08 21:06:53 +12:00
|
|
|
|
{
|
2014-05-09 11:08:30 +12:00
|
|
|
|
process.CloseMainWindow();
|
2014-05-08 21:06:53 +12:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
2014-05-09 11:08:30 +12:00
|
|
|
|
if (process != null)
|
2014-05-08 21:06:53 +12:00
|
|
|
|
{
|
2014-05-09 11:08:30 +12:00
|
|
|
|
process.Dispose();
|
2014-05-08 21:06:53 +12:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|