Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I use ShellExecute?

Execute (spawn) a program

How do I use ShellExecute?

by  2ffat  Posted    (Edited  )
Declaration
Code:
HINSTANCE ShellExecute(
    HWND hwnd,
    LPCTSTR lpOperation,
    LPCTSTR lpFile,
    LPCTSTR lpParameters,
    LPCTSTR lpDirectory,
    INT nShowCmd);

hwnd is the handle of the parent window.

lpOperation is the action that should be performed. The following actions are valid:
[tab][color blue]open[/color] or [color blue]NULL[/color] -- The function opens the file specified by lpFile. The file can be an executable file or a document file. The file can be a folder to open.
[tab][color blue]print[/color] -- The function prints the file specified by lpFile. The file should be a document file. If the file is an executable file, the function opens the file, as if "open" had been specified.
[tab][color blue]explore[/color] -- The function explores the folder specified by lpFile.

lpFile is the file to open or print, an executable file, or folder to explore.

lpParameters specifies any addition command line arguments.

lpDirectory specifies the default directory.

nShowCmd tells how to open an executable file. nShowCmd can be:
[tab][color blue]SW_HIDE[/color] -- Hides the window and activates another window.
[tab][color blue]SW_MAXIMIZE[/color] -- Maximizes the specified window.
[tab][color blue]SW_MINIMIZE[/color] -- Minimizes the specified window and activates the next top-level window in the Z order.
[tab][color blue]SW_RESTORE[/color] -- Activates and displays the window. If the window is minimized or maximized, Windows restores it to its original size and position. An application should specify this flag when restoring a minimized window.
[tab][color blue]SW_SHOW[/color] -- Activates the window and displays it in its current size and position.
[tab][color blue]SW_SHOWDEFAULT[/color] -- Sets the show state based on the SW_ flag specified in the STARTUPINFO structure passed to the CreateProcess function by the program that started the application.
[tab][color blue]SW_SHOWMAXIMIZED[/color] -- Activates the window and displays it as a maximized window.
[tab][color blue]SW_SHOWMINIMIZED[/color] -- Activates the window and displays it as a minimized window.
[tab][color blue]SW_SHOWMINNOACTIVE[/color] -- Displays the window as a minimized window. The active window remains active.
[tab][color blue]SW_SHOWNA[/color] -- Displays the window in its current state. The active window remains active.
[tab][color blue]SW_SHOWNOACTIVATE[/color] -- Displays a window in its most recent size and position. The active window remains active.
[tab][color blue]SW_SHOWNORMAL[/color] -- Activates and displays a window. If the window is minimized or maximized, Windows restores it to its original size and position. An application should specify this flag when displaying the window for the first time.
[tab] If lpFile is a document then nSHowCmd should be 0.

[tab]If the call to ShellExecute succeeds, the return value is greater than 31. Otherwise, the value can be:
[tab][color blue]0[/color] -- The operating system is out of memory or resources.
[tab][color blue]ERROR_FILE_NOT_FOUND[/color] -- The specified file was not found.
[tab][color blue]ERROR_PATH_NOT_FOUND[/color] -- The specified path was not found.
[tab][color blue]ERROR_BAD_FORMAT[/color] -- The .EXE file is invalid (non-Win32 .EXE or error in .EXE image).
[tab][color blue]SE_ERR_ACCESSDENIED[/color] -- The operating system denied access to the specified file.
[tab][color blue]SE_ERR_ASSOCINCOMPLETE[/color] -- The filename association is incomplete or invalid.
[tab][color blue]SE_ERR_DDEBUSY[/color] -- The DDE transaction could not be completed because other DDE transactions were being processed.
[tab][color blue]SE_ERR_DDEFAIL[/color] -- The DDE transaction failed.
[tab][color blue]SE_ERR_DDETIMEOUT[/color] -- The DDE transaction could not be completed because the request timed out.
[tab][color blue]SE_ERR_DLLNOTFOUND[/color] -- The specified dynamic-link library was not found.
[tab][color blue]SE_ERR_FNF[/color] -- The specified file was not found.
[tab][color blue]SE_ERR_NOASSOC[/color] -- There is no application associated with the given filename extension.
[tab][color blue]SE_ERR_OOM[/color] -- There was not enough memory to complete the operation.
[tab][color blue]SE_ERR_PNF[/color] -- The specified path was not found.
[tab][color blue]SE_ERR_SHARE[/color] -- A sharing violation occurred.

Pros
[tab]This function allows you to use the default Windows settings to open, print, or explore files or directories. It will also allow you to override those defaults. Not every parameter needs to filled in, many can be NULL.

Cons
[tab]ShellExecute cannot execute 16-bit programs. It is more complex than WinExec. It cannot you tell when the spawned program has finished.

Examples
Code:
ShellExecute(handle, "open";, "MyFile.txt", NULL, NULL, SW_SHOWNORMAL); // opens MyFile with default editor for text files
ShellExecute(handle, NULL, "MyEditor.exe";, "MyFile.txt", NULL, SW_SHOWNORMAL); // opens MyFile with MyEditor program
ShellExecute(handle, "print", "MyFile.txt", NULL, NULL, SW_SHOWNORMAL); // Prints MyFile
ShellExecute(handle, "explore", "C:\\", NULL, NULL, SW_SHOWNORMAL); // Explores root of C: drive
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top