From 3571fc1ce228648f6c8bec8ea955057efdc647c9 Mon Sep 17 00:00:00 2001 From: stnkl Date: Sat, 14 Nov 2020 16:01:48 +0100 Subject: [PATCH] Open processes without command window --- EverythingToolbar/EverythingToolbar.csproj | 1 + EverythingToolbar/Helpers/ShellUtils.cs | 124 ++++++++++++++++++++ EverythingToolbar/Rules.xaml.cs | 5 +- EverythingToolbar/SearchResultsView.xaml.cs | 42 +------ 4 files changed, 131 insertions(+), 41 deletions(-) create mode 100644 EverythingToolbar/Helpers/ShellUtils.cs diff --git a/EverythingToolbar/EverythingToolbar.csproj b/EverythingToolbar/EverythingToolbar.csproj index e9e41c1bd..2b4fb638d 100644 --- a/EverythingToolbar/EverythingToolbar.csproj +++ b/EverythingToolbar/EverythingToolbar.csproj @@ -73,6 +73,7 @@ + Rules.xaml diff --git a/EverythingToolbar/Helpers/ShellUtils.cs b/EverythingToolbar/Helpers/ShellUtils.cs new file mode 100644 index 000000000..91c2a1d02 --- /dev/null +++ b/EverythingToolbar/Helpers/ShellUtils.cs @@ -0,0 +1,124 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.IO; +using System.Linq; +using System.Runtime.InteropServices; +using System.Text; +using System.Threading.Tasks; +using System.Windows; + +namespace EverythingToolbar.Helpers +{ + class ShellUtils + { + [DllImport("shell32.dll", CharSet = CharSet.Auto)] + static extern bool ShellExecuteEx(ref SHELLEXECUTEINFO lpExecInfo); + + [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] + public struct SHELLEXECUTEINFO + { + public int cbSize; + public uint fMask; + public IntPtr hwnd; + [MarshalAs(UnmanagedType.LPTStr)] + public string lpVerb; + [MarshalAs(UnmanagedType.LPTStr)] + public string lpFile; + [MarshalAs(UnmanagedType.LPTStr)] + public string lpParameters; + [MarshalAs(UnmanagedType.LPTStr)] + public string lpDirectory; + public int nShow; + public IntPtr hInstApp; + public IntPtr lpIDList; + [MarshalAs(UnmanagedType.LPTStr)] + public string lpClass; + public IntPtr hkeyClass; + public uint dwHotKey; + public IntPtr hIcon; + public IntPtr hProcess; + } + + public static void ShowFileProperties(string path) + { + SHELLEXECUTEINFO info = new SHELLEXECUTEINFO(); + info.cbSize = Marshal.SizeOf(info); + info.lpVerb = "properties"; + info.lpFile = path; + info.nShow = 5; + info.fMask = 12; + ShellExecuteEx(ref info); + } + + [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] + private struct STARTUPINFO + { + public Int32 cb; + public string lpReserved; + public string lpDesktop; + public string lpTitle; + public Int32 dwX; + public Int32 dwY; + public Int32 dwXSize; + public Int32 dwYSize; + public Int32 dwXCountChars; + public Int32 dwYCountChars; + public Int32 dwFillAttribute; + public Int32 dwFlags; + public Int16 wShowWindow; + public Int16 cbReserved2; + public IntPtr lpReserved2; + public IntPtr hStdInput; + public IntPtr hStdOutput; + public IntPtr hStdError; + } + + [StructLayout(LayoutKind.Sequential)] + private struct PROCESS_INFORMATION + { + public IntPtr hProcess; + public IntPtr hThread; + public int dwProcessId; + public int dwThreadId; + } + + [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)] + private static extern bool CreateProcess( + string lpApplicationName, + string lpCommandLine, + IntPtr lpProcessAttributes, + IntPtr lpThreadAttributes, + bool bInheritHandles, + uint dwCreationFlags, + IntPtr lpEnvironment, + string lpCurrentDirectory, + [In] ref STARTUPINFO lpStartupInfo, + out PROCESS_INFORMATION lpProcessInformation); + + public static void CreateProcessFromCommandLine(string commandLine) + { + var si = new STARTUPINFO(); + var pi = new PROCESS_INFORMATION(); + + CreateProcess( + null, + commandLine, + IntPtr.Zero, + IntPtr.Zero, + false, + 0, + IntPtr.Zero, + null, + ref si, + out pi); + } + + public static void OpenWithDialog(string path) + { + var args = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "shell32.dll"); + args += ",OpenAs_RunDLL " + path; + Process.Start("rundll32.exe", args); + } + } +} diff --git a/EverythingToolbar/Rules.xaml.cs b/EverythingToolbar/Rules.xaml.cs index 213f34538..16a44f430 100644 --- a/EverythingToolbar/Rules.xaml.cs +++ b/EverythingToolbar/Rules.xaml.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using EverythingToolbar.Helpers; +using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics; using System.IO; @@ -178,7 +179,7 @@ public static bool HandleRule(SearchResult searchResult, string command="") command = command.Replace("%path%", "\"" + searchResult.Path + "\""); try { - Process.Start("cmd.exe", "/S /C \"" + command + "\""); + ShellUtils.CreateProcessFromCommandLine(command); return true; } catch(Win32Exception) diff --git a/EverythingToolbar/SearchResultsView.xaml.cs b/EverythingToolbar/SearchResultsView.xaml.cs index 807f70c21..6efc7c115 100644 --- a/EverythingToolbar/SearchResultsView.xaml.cs +++ b/EverythingToolbar/SearchResultsView.xaml.cs @@ -5,12 +5,12 @@ using System.ComponentModel; using System.Diagnostics; using System.IO; -using System.Runtime.InteropServices; using System.Windows; using System.Windows.Controls; using System.Windows.Input; using System.Windows.Media; using System.Windows.Threading; +using EverythingToolbar.Helpers; namespace EverythingToolbar { @@ -154,9 +154,7 @@ private void CopyPathToClipBoard(object sender, RoutedEventArgs e) private void OpenWith(object sender, RoutedEventArgs e) { string path = (SearchResultsListView.SelectedItem as SearchResult).FullPathAndFileName; - var args = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "shell32.dll"); - args += ",OpenAs_RunDLL " + path; - Process.Start("rundll32.exe", args); + ShellUtils.OpenWithDialog(path); } private void ShowInEverything(object sender, RoutedEventArgs e) @@ -182,43 +180,9 @@ private void Open(object sender, MouseEventArgs e) OpenSelectedSearchResult(); } - [DllImport("shell32.dll", CharSet = CharSet.Auto)] - static extern bool ShellExecuteEx(ref SHELLEXECUTEINFO lpExecInfo); - - [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] - public struct SHELLEXECUTEINFO - { - public int cbSize; - public uint fMask; - public IntPtr hwnd; - [MarshalAs(UnmanagedType.LPTStr)] - public string lpVerb; - [MarshalAs(UnmanagedType.LPTStr)] - public string lpFile; - [MarshalAs(UnmanagedType.LPTStr)] - public string lpParameters; - [MarshalAs(UnmanagedType.LPTStr)] - public string lpDirectory; - public int nShow; - public IntPtr hInstApp; - public IntPtr lpIDList; - [MarshalAs(UnmanagedType.LPTStr)] - public string lpClass; - public IntPtr hkeyClass; - public uint dwHotKey; - public IntPtr hIcon; - public IntPtr hProcess; - } - public void ShowFileProperties(object sender, RoutedEventArgs e) { - SHELLEXECUTEINFO info = new SHELLEXECUTEINFO(); - info.cbSize = Marshal.SizeOf(info); - info.lpVerb = "properties"; - info.lpFile = (SearchResultsListView.SelectedItem as SearchResult).FullPathAndFileName; - info.nShow = 5; - info.fMask = 12; - ShellExecuteEx(ref info); + ShellUtils.ShowFileProperties((SearchResultsListView.SelectedItem as SearchResult).FullPathAndFileName); } private void OnOpenWithMenuLoaded(object sender, RoutedEventArgs e)