|
| 1 | +# ShellExec |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +ShellExec is a console application that allows you to run programs (as a current user or administrator), open documents in associated applications, view web pages in your default browser, and more. |
| 6 | +It's a "console wrapper" for the [ShellExecuteEx](https://docs.microsoft.com/en-us/windows/win32/api/shellapi/nf-shellapi-shellexecutew) function. |
| 7 | + |
| 8 | + |
| 9 | + |
| 10 | +## Download |
| 11 | + |
| 12 | +Compiled binaries: |
| 13 | + |
| 14 | +[ShellExec_win64.zip](https://www.pazera-software.com/files/ShellExec_win64.zip) (~96 KB) |
| 15 | +[ShellExec_win32.zip](https://www.pazera-software.com/files/ShellExec_win32.zip) (~72 KB) |
| 16 | + |
| 17 | +## Usage |
| 18 | + |
| 19 | +``` |
| 20 | +ShellExec FILE [-o Operation] [-p Parameters] [-d Directory] [-s ShowCommand] |
| 21 | +[-w [finish|idle]] [-t TIME] [-hwnd WindowHandle] [-h] [-V] [--license] [--home] [--source] |
| 22 | +``` |
| 23 | + |
| 24 | +## Options |
| 25 | + |
| 26 | +Options are case-sensitive. Options in square brackets are optional. |
| 27 | + |
| 28 | +### Main options |
| 29 | + |
| 30 | +`-o`, `--operation=STR` |
| 31 | +Action to be performed. The default action is **open**. See description below. |
| 32 | + |
| 33 | +`-p`, `--params=STR` |
| 34 | +If **FILE** specifies an executable file, this parameter is a string that specifies the parameters to be passed to the application. |
| 35 | + |
| 36 | +`-d`, `--directory=DIR` |
| 37 | +Working directory for the action. |
| 38 | + |
| 39 | +`-s`, `--show-cmd=STR` |
| 40 | +This parameter specifies how an application is to be displayed when it is opened. The default show command is **SW_SHOWNORMAL**. See description below. |
| 41 | + |
| 42 | +`-w`, `--wait=[FOR]` |
| 43 | +**finish** or **idle**. Default: **finish**. If you do not specify a waiting time in the `-t` option, **INFINITE** will be used (`-t=infinite`). See description below. |
| 44 | + |
| 45 | +`-t`, `--wait-time=TIME` |
| 46 | +Time-out interval (for the `-w` option). Available time units: `ms` (milliseconds), `s` (seconds), `m` (minutes), `h` (hours), `d` (days). If no unit is specified, milliseconds will be used. The value **INFINITE** means no time limit. |
| 47 | + |
| 48 | +`-hwnd`, `--window-handle=INT` |
| 49 | +A handle to the parent window used for displaying an UI or error messages. |
| 50 | + |
| 51 | + |
| 52 | +### Info |
| 53 | + |
| 54 | +`-h`, `--help` |
| 55 | +Show help. |
| 56 | + |
| 57 | +`-V`, `--version` |
| 58 | +Show application version. |
| 59 | + |
| 60 | +`--license` |
| 61 | +Display program license. |
| 62 | + |
| 63 | +`--home` |
| 64 | +Opens program home page in the default browser. |
| 65 | + |
| 66 | +`--source` |
| 67 | +Opens the program page on GitHub with the program's source files. |
| 68 | + |
| 69 | + |
| 70 | +## FILE |
| 71 | +File name or directory or URL or object on which to execute the specified operation. |
| 72 | + |
| 73 | +## Operations |
| 74 | + |
| 75 | +**open** |
| 76 | +Opens the item specified by the FILE parameter. The item can be a file or folder or URL. |
| 77 | + |
| 78 | +**edit** |
| 79 | +Launches an editor and opens the document for editing. If FILE is not a document file, the ShellExec will fail. |
| 80 | + |
| 81 | +**explore** |
| 82 | +Explores a folder specified by FILE. |
| 83 | + |
| 84 | +**find** |
| 85 | +Initiates a search beginning in the directory specified by `-d` option. |
| 86 | + |
| 87 | +**print** |
| 88 | +Prints the file specified by FILE. If FILE is not a document file, the program fails. |
| 89 | + |
| 90 | +**runas** |
| 91 | +Launches an application as Administrator. User Account Control (UAC) will prompt the user for consent to run the application elevated or enter the credentials of an administrator account used to run the application. |
| 92 | + |
| 93 | +**properties** |
| 94 | +Displays the file or folder's properties. |
| 95 | + |
| 96 | +More information (lpOperation): <https://docs.microsoft.com/en-us/windows/win32/api/shellapi/nf-shellapi-shellexecutew> |
| 97 | +More information (lpVerb): <https://docs.microsoft.com/en-us/windows/win32/api/shellapi/ns-shellapi-shellexecuteinfow> |
| 98 | + |
| 99 | +## Show commands |
| 100 | + |
| 101 | +`0` | `SW_HIDE` | `Hide` |
| 102 | +Hides the window and activates another window. |
| 103 | + |
| 104 | +`1` | `SW_SHOWNORMAL` | `ShowNormal` | `SW_NORMAL` | `Normal` |
| 105 | +Activates and displays a window. If the window is minimized or maximized, the system restores it to its original size and position. An application should specify this flag when displaying the window for the first time. |
| 106 | + |
| 107 | +`2` | `SW_SHOWMINIMIZED` | `ShowMinimized` | `Minimized` |
| 108 | +Activates the window and displays it as a minimized window. |
| 109 | + |
| 110 | +`3` | `SW_SHOWMAXIMIZED` | `ShowMaximized` | `Maximized` | `Maximize` |
| 111 | +Activates the window and displays it as a maximized window. |
| 112 | + |
| 113 | +`4` | `SW_SHOWNOACTIVATE` | `ShowNoActivate` | `NoActivate` |
| 114 | +Displays a window in its most recent size and position. This value is similar to SW_SHOWNORMAL, except that the window is not activated. |
| 115 | + |
| 116 | +`5` | `SW_SHOW` | `Show` |
| 117 | +Activates the window and displays it in its current size and position. |
| 118 | + |
| 119 | +`6` | `SW_MINIMIZE` | `Minimize` |
| 120 | +Minimizes the specified window and activates the next top-level window in the Z order. |
| 121 | + |
| 122 | +`7` | `SW_SHOWMINNOACTIVE` | `ShowMinNoActive` | `MinNoActive` |
| 123 | +Displays the window as a minimized window. This value is similar to SW_SHOWMINIMIZED, except the window is not activated. |
| 124 | + |
| 125 | +`8` | `SW_SHOWNA` | `ShowNA` |
| 126 | +Displays the window in its current size and position. This value is similar to SW_SHOW, except that the window is not activated. |
| 127 | + |
| 128 | +`9` | `SW_RESTORE` | `Restore` |
| 129 | +Activates and displays the window. If the window is minimized or maximized, the system restores it to its original size and position. An application should specify this flag when restoring a minimized window. |
| 130 | + |
| 131 | +`10` | `SW_SHOWDEFAULT` | `ShowDefault` |
| 132 | +Sets the show state based on the SW_ value specified in the STARTUPINFO structure passed to the CreateProcess function by the program that started the application. |
| 133 | + |
| 134 | +`11` | `SW_FORCEMINIMIZE` | `ForceMinimize` |
| 135 | +Minimizes a window, even if the thread that owns the window is not responding. This flag should only be used when minimizing windows from a different thread. |
| 136 | + |
| 137 | +More information: <https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-showwindow#parameters> |
| 138 | + |
| 139 | +## Wait for |
| 140 | + |
| 141 | +**finish** |
| 142 | +Wait until the running program (specified in the FILE parameter) finishes its operation, or until the time-out interval (`-t` option) has elapsed. |
| 143 | + |
| 144 | +**idle** |
| 145 | +Waits until the specified program has finished processing its initial input and is waiting for user input with no input pending, or until the time-out interval (`-t` option) has elapsed. |
| 146 | + |
| 147 | +More information: |
| 148 | +https://docs.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-waitforsingleobject |
| 149 | +https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-waitforinputidle |
| 150 | + |
| 151 | +## Exit codes |
| 152 | + |
| 153 | +0 - Success |
| 154 | +Any other value - Error |
| 155 | + |
| 156 | +## Examples |
| 157 | + |
| 158 | +1. Starts the system calculator: |
| 159 | + `ShellExec calc` |
| 160 | + |
| 161 | +1. Opens the "hosts" file in the system text editor, Notepad: |
| 162 | + `ShellExec notepad -o runas -p "C:\Windows\System32\drivers\etc\hosts"` |
| 163 | + |
| 164 | +1. Displays the given website in the default browser: |
| 165 | + `ShellExec https://example.com` |
| 166 | + |
| 167 | +1. Opens the "win.ini" file in the text editor associated with the INI files: |
| 168 | + `ShellExec "C:\Windows\win.ini"` |
| 169 | + |
| 170 | +1. Opens the JPG file in the default graphic viewer: |
| 171 | + `ShellExec "D:\pictures\my picture.jpg"` |
| 172 | + |
| 173 | +1. Opens the BAT file for editing in the default editor and waits for the program to finish: |
| 174 | + `ShellExec "D:\batch_files\test.bat" -o edit -w=finish` |
| 175 | + |
| 176 | +## License |
| 177 | + |
| 178 | +Public domain. |
| 179 | + |
| 180 | +## Compilation |
| 181 | + |
| 182 | +You need [Embarcadero Delphi](https://www.embarcadero.com/products/delphi) XE2 or newer (the free [Community](https://www.embarcadero.com/products/delphi/starter/free-download) version is eough). |
| 183 | + |
| 184 | +You also need a few units from the [JPLib](https://github.com/jackdp/JPLib) library. |
0 commit comments