Type a page name and press Enter. You'll jump to the page if it exists, or you can create it if it doesn't.
To create a page in a module other than shell32, prefix the name with the module name and a period.
<DllImport("Shell32", CharSet:=CharSet.Auto, SetLastError:=True)> _
Public Function ShellExecuteEx(ByRef lpExecInfo As SHELLEXECUTEINFO) As Boolean
End Function
User-Defined Types:
(VB.NET)
Public Structure SHELLEXECUTEINFO
Public cbSize As Integer
Public fMask As Integer
Public hwnd As IntPtr
<MarshalAs(UnmanagedType.LPTStr)> Public lpVerb As String
<MarshalAs(UnmanagedType.LPTStr)> Public lpFile As String
<MarshalAs(UnmanagedType.LPTStr)> Public lpParameters As String
<MarshalAs(UnmanagedType.LPTStr)> Public lpDirectory As String
Dim nShow As Integer
Dim hInstApp As IntPtr
Dim lpIDList As IntPtr
<MarshalAs(UnmanagedType.LPTStr)> Public lpClass As String
Public hkeyClass As IntPtr
Public dwHotKey As Integer
Public hIcon As IntPtr
Public hProcess As IntPtr
End Structure
(C#)
[StructLayout(LayoutKind.Sequential)]
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;
}
Check the SW constants at to see what value to pass in the nShow member. Typically you'll pass SW_SHOW.
Be careful with the string structure fields: UnmanagedType.LPTStr will be marshalled as unicode string so only the first character will be recognized by the function. Use UnmanagedType.LPStr instead. [Tested on Win7]
lpVerb member can be used for a varity of actions like "properties", "find", "openas", "print"..etc depending on the file type you're dealing with.Actions available for a specific file type are stored in registry, setting lpVerb to null results in the default action of that file type to be executed.
Someone previously thought "the truth is in the headers - in this case ShellAPI.h, which declares them all as LPCSTR i.e. they're always ANSI not Unicode.". On the contrary, shellapi.h defines two versions of the structure, SHELLEXECUTEINFOA and SHELLEXECUTEINFOW, and ShellExecuteExW expects a SHELLEXECUTEINFOW structure... so although "LPCTSTR" never appears in the definition the effect is the same.
Tips & Tricks:
(C#)
using System.Runtime.InteropServices;
(VB.NET)
Imports System.Runtime.InteropServices
Sample Code:
VB.NET
Demonstrates how to open an HTML file in the browser:
Dim info As SHELLEXECUTEINFO
info.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(info)
info.lpVerb = "open"
info.lpFile = "somefile.html"
info.nShow = SW_SHOW
If Not ShellExecuteEx(info) Then
Dim ex As New System.ComponentModel.Win32Exception(System.Runtime.InteropServices.Marshal.GetLastWin32Error())
MessageBox.Show(ex.Message, "Error")
End If
Dim p As New Process
p.StartInfo.FileName = "somefile.html"
p.StartInfo.ErrorDialog = True ' this opens the "Open With" dialog for unknown file types
p.Start()
public static void ShowFileProperties(string Filename) {
SHELLEXECUTEINFO info = new SHELLEXECUTEINFO();
info.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(info);
info.lpVerb = "properties";
info.lpFile = Filename;
info.nShow = SW_SHOW;
info.fMask = SEE_MASK_INVOKEIDLIST;
ShellExecuteEx(ref info);
}
VB.NET Example of Property Dialog
Public Const SW_SHOW As Short = 5
Public Const SEE_MASK_INVOKEIDLIST As Short = 12
<DllImport("Shell32", CharSet:=CharSet.Auto, SetLastError:=True)> _
Public Shared Function ShellExecuteEx(ByRef lpExecInfo As SHELLEXECUTEINFO) As Boolean
End Function
Dim sei As New SHELLEXECUTEINFO
sei.cbSize = Marshal.SizeOf(sei)
sei.lpVerb = "properties"
sei.lpFile = "FilePath"
sei.nShow = SW_SHOW
sei.fMask = SEE_MASK_INVOKEIDLIST
If Not ShellExecuteEx(sei) Then
Dim ex As New System.ComponentModel.Win32Exception(System.Runtime.InteropServices.Marshal.GetLastWin32Error())
MessageBox.Show(ex.Message, Me.TITLE, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End If
Verbs for lpVerb:
"open" - Opens a file or a application
"openas" - Opens dialog when no program is associated to the extension
"opennew" - see MSDN
"runas" - In Windows 7 and Vista, opens the UAC dialog and in others, open the Run as... Dialog
"null" - Specifies that the operation is the default for the selected file type.
"edit" - Opens the default text editor for the file.
"explore" - Opens the Windows Explorer in the folder specified in lpDirectory.
"properties" - Opens the properties window of the file.
"copy" - see MSDN
"cut" - see MSDN
"paste" - see MSDN
"pastelink" - pastes a shortcut
"delete" - see MSDN
"print" - Start printing the file with the default application.
"printto" - see MSDN
"find" - Start a search
*NOTE*
With the above code (VB), I have had problems on Win 7 x64. By changing the build type to x86 in the advanced compile options, the code works... (Specifically, on Win7 x64, ShellExecuteEx returned NO error, but the file's property page was not displayed)
The ShellExecuteEx API
8/15/2015 8:03:05 PM - Nige-81.171.173.35
Constants were adapted directly from ShellAPI.h
3/16/2007 7:36:44 AM - redcrystal@msn.com-192.55.52.4
Click to read this page
5/16/2017 3:56:58 AM - anonymous
The ShellExecuteEx API
8/15/2015 8:03:05 PM - Nige-81.171.173.35
The ShellExecuteEx API
8/15/2015 8:03:05 PM - Nige-81.171.173.35
Please edit this page!
Do you have...
helpful tips or sample code to share for using this API in managed code?
corrections to the existing content?
variations of the signature you want to share?
additional languages you want to include?
Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).