2022-08-08 04:31:38 +00:00
|
|
|
|
using System;
|
2019-12-05 20:49:20 +00:00
|
|
|
|
using System.Collections.Generic;
|
2022-08-08 04:31:38 +00:00
|
|
|
|
using System.ComponentModel;
|
2019-12-05 20:49:20 +00:00
|
|
|
|
using System.Diagnostics;
|
2022-08-08 04:31:38 +00:00
|
|
|
|
using System.IO;
|
2019-12-05 20:49:20 +00:00
|
|
|
|
using System.Linq;
|
2019-12-10 11:01:12 +00:00
|
|
|
|
using System.Runtime.InteropServices;
|
2019-12-05 20:49:20 +00:00
|
|
|
|
using System.Text;
|
2019-12-10 11:01:12 +00:00
|
|
|
|
using System.Threading;
|
2019-12-05 20:49:20 +00:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
2020-04-21 09:12:17 +00:00
|
|
|
|
namespace Flow.Launcher.Plugin.SharedCommands
|
2019-12-05 20:49:20 +00:00
|
|
|
|
{
|
|
|
|
|
|
public static class ShellCommand
|
|
|
|
|
|
{
|
2019-12-10 11:01:12 +00:00
|
|
|
|
public delegate bool EnumThreadDelegate(IntPtr hwnd, IntPtr lParam);
|
|
|
|
|
|
[DllImport("user32.dll")] static extern bool EnumThreadWindows(uint threadId, EnumThreadDelegate lpfn, IntPtr lParam);
|
|
|
|
|
|
[DllImport("user32.dll")] static extern int GetWindowText(IntPtr hwnd, StringBuilder lpString, int nMaxCount);
|
|
|
|
|
|
[DllImport("user32.dll")] static extern int GetWindowTextLength(IntPtr hwnd);
|
|
|
|
|
|
|
|
|
|
|
|
private static bool containsSecurityWindow;
|
|
|
|
|
|
|
|
|
|
|
|
public static Process RunAsDifferentUser(ProcessStartInfo processStartInfo)
|
|
|
|
|
|
{
|
|
|
|
|
|
processStartInfo.Verb = "RunAsUser";
|
|
|
|
|
|
var process = Process.Start(processStartInfo);
|
|
|
|
|
|
|
|
|
|
|
|
containsSecurityWindow = false;
|
|
|
|
|
|
while (!containsSecurityWindow) // wait for windows to bring up the "Windows Security" dialog
|
|
|
|
|
|
{
|
|
|
|
|
|
CheckSecurityWindow();
|
|
|
|
|
|
Thread.Sleep(25);
|
|
|
|
|
|
}
|
|
|
|
|
|
while (containsSecurityWindow) // while this process contains a "Windows Security" dialog, stay open
|
|
|
|
|
|
{
|
|
|
|
|
|
containsSecurityWindow = false;
|
|
|
|
|
|
CheckSecurityWindow();
|
|
|
|
|
|
Thread.Sleep(50);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return process;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static void CheckSecurityWindow()
|
|
|
|
|
|
{
|
|
|
|
|
|
ProcessThreadCollection ptc = Process.GetCurrentProcess().Threads;
|
|
|
|
|
|
for (int i = 0; i < ptc.Count; i++)
|
|
|
|
|
|
EnumThreadWindows((uint)ptc[i].Id, CheckSecurityThread, IntPtr.Zero);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static bool CheckSecurityThread(IntPtr hwnd, IntPtr lParam)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (GetWindowTitle(hwnd) == "Windows Security")
|
|
|
|
|
|
containsSecurityWindow = true;
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static string GetWindowTitle(IntPtr hwnd)
|
|
|
|
|
|
{
|
|
|
|
|
|
StringBuilder sb = new StringBuilder(GetWindowTextLength(hwnd) + 1);
|
|
|
|
|
|
GetWindowText(hwnd, sb, sb.Capacity);
|
|
|
|
|
|
return sb.ToString();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-11-17 10:00:36 +00:00
|
|
|
|
public static ProcessStartInfo SetProcessStartInfo(this string fileName, string workingDirectory = "", string arguments = "", string verb = "", bool createNoWindow = false)
|
2019-12-05 20:49:20 +00:00
|
|
|
|
{
|
|
|
|
|
|
var info = new ProcessStartInfo
|
|
|
|
|
|
{
|
2019-12-09 21:23:34 +00:00
|
|
|
|
FileName = fileName,
|
|
|
|
|
|
WorkingDirectory = workingDirectory,
|
|
|
|
|
|
Arguments = arguments,
|
2021-11-17 10:00:36 +00:00
|
|
|
|
Verb = verb,
|
|
|
|
|
|
CreateNoWindow = createNoWindow
|
2019-12-05 20:49:20 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
return info;
|
|
|
|
|
|
}
|
2021-11-17 10:00:36 +00:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Runs a windows command using the provided ProcessStartInfo
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <exception cref="FileNotFoundException">Thrown when unable to find the file specified in the command </exception>
|
|
|
|
|
|
/// <exception cref="Win32Exception">Thrown when error occurs during the execution of the command </exception>
|
|
|
|
|
|
public static void Execute(ProcessStartInfo info)
|
|
|
|
|
|
{
|
|
|
|
|
|
Execute(Process.Start, info);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Runs a windows command using the provided ProcessStartInfo using a custom execute command function
|
|
|
|
|
|
/// </summary>
|
2022-08-08 04:31:38 +00:00
|
|
|
|
/// <param name="startProcess">allows you to pass in a custom command execution function</param>
|
|
|
|
|
|
/// <param name="info">allows you to pass in the info that will be passed to startProcess</param>
|
2021-11-17 10:00:36 +00:00
|
|
|
|
/// <exception cref="FileNotFoundException">Thrown when unable to find the file specified in the command </exception>
|
|
|
|
|
|
/// <exception cref="Win32Exception">Thrown when error occurs during the execution of the command </exception>
|
|
|
|
|
|
public static void Execute(Func<ProcessStartInfo, Process> startProcess, ProcessStartInfo info)
|
|
|
|
|
|
{
|
|
|
|
|
|
startProcess(info);
|
|
|
|
|
|
}
|
2019-12-05 20:49:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|