Skip to content

Commit

Permalink
Open processes without command window
Browse files Browse the repository at this point in the history
  • Loading branch information
srwi committed Nov 14, 2020
1 parent c85c8fa commit 3571fc1
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 41 deletions.
1 change: 1 addition & 0 deletions EverythingToolbar/EverythingToolbar.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
</Compile>
<Compile Include="EverythingSearch.cs" />
<Compile Include="Data\Rule.cs" />
<Compile Include="Helpers\ShellUtils.cs" />
<Compile Include="Rules.xaml.cs">
<DependentUpon>Rules.xaml</DependentUpon>
</Compile>
Expand Down
124 changes: 124 additions & 0 deletions EverythingToolbar/Helpers/ShellUtils.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
5 changes: 3 additions & 2 deletions EverythingToolbar/Rules.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using EverythingToolbar.Helpers;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
Expand Down Expand Up @@ -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)
Expand Down
42 changes: 3 additions & 39 deletions EverythingToolbar/SearchResultsView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 3571fc1

Please sign in to comment.