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 strongm 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 WinExec

Execute (spawn) a program

How do I use WinExec

by  2ffat  Posted    (Edited  )
Declaration
Code:
UINT WinExec(LPCSTR lpCmdLine, UINT uCmdShow);

lpCmdLine is the null-terminated character string to be executed. This can be an executable file or a file name and its parameters.
uCmdShow tells how to show a Windows-based program to be displayed. For non-Windows apps, the PIF file determines how to display the program. uCmdShow can be one of the following:
[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 the call to WinExec succeeds, the return value is greater than 31. Otherwise, the value can be:
[color blue]0[/color] -- The system is out of memory or resources.
[color blue]ERROR_BAD_FORMAT[/color] -- The .EXE file is invalid (non-Win32 .EXE or error in .EXE image).
[color blue]ERROR_FILE_NOT_FOUND[/color] -- The specified file was not found.
[color blue]ERROR_PATH_NOT_FOUND[/color] -- The specified path was not found.

Pros
[tab]This is the easiest way of spawning a program.

Cons
[tab]You have no control over the spawned program. WinExec cannot you tell when the spawned program has finished nor can it execute 16-bit programs.

Example
Code:
WinExec("MyOtherProgram.exec", SW_SHOWNORMAL); // Runs MyOtherProgram in normal window
WinExec("notepad.exe Program.cpp", SH_SHOW); // Runs notepad which opens Program.cpp

Gotchas
[tab]This function has been depreciated and may not be supported in future versions of Windows. Currently (in Builder 5, at least), this is implemented as a call to CreateProcess.
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