WinExec Procedure

Runs the specified application.

Syntax

[ hHandle = ] WinExec ( sCmdLine [, enCmdShow] [, hWnd] [, sVerb] [, sDefaultDir ] )

The WinExec procedure syntax has the following parts:

 

Name

Type

Description

hHandle

AHandle

Process handle to the specified application. This number is <=32 if the function failed to start the process. See table below for possible error numbers.

sCmdline

Val BString

Command line to hold the path or executable filename and optional parameters

enCmdshow

Val enumAFormShow

Optional (aformShowNormal default). Initial window state of the window

hWnd

Val AHandle

Optional. Handle to a parent window  used to display any message boxes that the system might produce while executing this function. Pass 0 for use the desktop window as parent.

sVerb

Val BString

Optional. A string, referred to as a verb, that specifies the action to be performed. See below for valid verbs.

sDefaultDir

Val BString

Optional default directory

Where

hHandle error numbers (<=32) can be be one of the following values:

 

Value

Description

0

The operating system is out of memory or resources

2

The specified file was not found.

3

 The specified path was not found.

5

The operating system denied access to the specified file.

8

There was not enough memory to complete the operation.

26

A sharing violation occurred.

27

The file name association is incomplete or invalid.

30

The DDE transaction could not be completed because other DDE transactions were being processed.

28

The DDE transaction could not be completed because the request timed out.

29

The DDE transaction failed.

31

There is no application associated with the given file name extension. This error will also be returned if you attempt to print a file that is not printable.

32

The specified dynamic-link library was not found.

enumAFormShow can be one of the following values:

 

Name

Value

Description

aformShowHide

0

Hide. Hides the window and passes activation to another window.

aformShowNormal

1

Normal. Activates and displays a window. If the window is minimized or maximized, Windows restores it to its original size and position.

aformShowMinimized

2

Minimized. Activates the window and displays it as an icon.

aformShowMaximize

3

Maximize. Activates the window and displays it as a maximized window (full window).

aformShowNoActivate

4

NoActivate. Displays the window in its most recent size and position. The current active window remains active.

aformShow

5

Show. Activates the window and displays it in its current size and position.

aformShowMinimize

6

Minimize. Minimizes the specified window and activates the top-level window in the window-manager's list.

aformShowMinNoActive

7

ShowMinNoActive. Displays the window as an icon. The current active window remains active.

aformShowState

8

ShowState. Displays the window in its current state. The current active window remains active.

aformShowRestore

9

Restore. Redisplays the current windows.

aformShowDefault

10

Default. Displays the default window.

The following verbs are valid:

 

Verb

Description

edit

Opens an editor. If sCmdLine is not a document file, the function will fail.

explore

The function explores the folder specified in sCmdFile.

open

The function opens the file specified in sCmdFile for an parameter.

print

The function prints the dicument file specified in sCmdLine.

properties

Displays the file or folder's properties.

Comments

If the sCmdLine contains a file name or path containing spaces, it must be surrounded by double quote characters (").  Escape characters such as double quote, path separator ("\") have to be prefixed with "\". See examples below.

If the sCmdLine contains an application filename that does not contain a path, Windows searches the directories in the following order:

  1. The current directory.

  2. The Windows directory.

  3. The Windows SYSTEM sub-directory.

  4. The directories listed in the PATH statement.

  5. The list of directories mapped in a network.

For a non-Windows application, the PIF file, if used, determines the state of the application.

The functions return immediately after starting the process. If the application requires to wait until the process is terminated use the WaitForSingleObject function.

Applications can use the Windows API GetExitCodeProcess to determine a process exit code:

Example

hNotePad=WinExec("notepad C:\\MyProject\\Examples\\afont.h",aformShowNoActivate, ,"open")

! file path contains a space - prefixed with \"
hNotePad1=WinExec("notepad \"C:\\Program Files\\ATEasy\\Examples\\afont.h\"", aformShowNoActivate, ,"open")

hNotePad=WinExec("\"C:\\Program Files\\DSS\\DssExe.exe\" C:\\SFL_ATP_20.DSS", aformShowNormal,,"open")

! file name contains spaces
hHandle=WinExec("\"A:\\Lesson 8 Meaning.doc\"", , ,"print")

! execute and wait until executable was closed 

hHandle=WinExec("D:\\MyProject\\TestDoc\\A.doc", , ,"print")

WaitForSingleObject(hHandle)     ! wait until print is done

 

! Batch file execution, ?C execute and close, /K execute and stay

WinExec("cmd.exe /C C:\\a.bat")

See Also

Exit Statement, ExitCode, Run, WaitForSingleObject