There are three API functions that can run executable files winexec, ShellExecute, and CreateProcess.
1.CreateProcess is less used because it is more complex to use.
2.WinExec main run EXE file. such as: WinExec (' Notepad.exe Readme.txt ', sw_show);
3.ShellExecute not only can run an EXE file, but it can also run files that are already associated.
The first must refer to the Shellapi.pas unit: uses SHELLAPI;
1). Standard usage
ShellExecute function prototype and parameter meanings are as follows:
function ShellExecute (Hwnd:hwnd; Operation, FileName, Parameters,directory:pchar; Showcmd:integer): HINST; stdcall;
HWnd: Used to specify the parent window handle. When an error occurs in the function call procedure, it acts as the parent window of the Windows message window. For example, you can set it as the main application window
Handle, which is application.handle, can also be set to the desktop window handle (obtained with the GetDesktopWindow function).
Operation: Used to specify the action to be made. Where the "open" operation means that the program specified by the filename parameter is executed, or the file or file specified by the filename parameter is opened
Clip; " Print "Operation means that the file specified by the filename parameter is printed;" Explore "operation means browsing the folder specified by the filename parameter. When the parameter is set to nil, the
The line default action is "open".
FileName: Used to specify the file name to open, the name of the program file to execute, or the folder name to browse.
Parameters: If the filename parameter is an executable program, this parameter specifies a command-line argument, otherwise this parameter should be nil or pchar (0).
Directory: Used to specify the default directory.
ShowCmd: If the filename parameter is an executable program, this parameter specifies how the program window is initially displayed, otherwise this parameter should be set to 0.
If the ShellExecute function call succeeds, the return value is the instance handle of the executing program. If the return value is less than 32, it indicates an error occurred.
The above is only the standard usage of the ShellExecute function, and the following describes its special usage.
2). Special usage
If the filename parameter is set to the "http:" protocol format, the function opens the default browser and links to the specified URL address. If multiple browsers are installed in the user's machine
, the function determines which browser to start based on the settings of the HTTP protocol handler (Protocols Handler) in the Windows 9x/nt registry.
Format one:/HTTP website domain name.
such as: ShellExecute (handle, ' open ', http://www.neu.edu.cn ', nil, nil, SW_SHOWNORMAL);
Format two: HTTP//Website name/page file name.
such as: ShellExecute (handle, ' open ', http://www.neu.edu.cn/default.htm ', nil,nil,sw_shownormal);
If you set the filename parameter to the "mailt" protocol format, the function launches the default mail client, such as Microsoft Outlook (also including Microsoft
Outlook Express) or Netscape Messanger. If more than one mail client is installed in the user's machine, the function will be processed according to the mailto protocol in the Windows 9X/NT Registry
The settings of the program determine which mail client to start.
Format one: mailt
such as: ShellExecute (handle, ' open ', ' mailt ', nil, nil, SW_SHOWNORMAL); Open the new mail window.
Format two: mailt user account @ Mail server address
such as: ShellExecute (handle, ' open ', ' [email protected] ', nil, nil, SW_SHOWNORMAL), open the New mail window, and automatically fill in the receiving
Address of the sender. If you specify multiple recipient addresses, the recipient addresses must be separated by semicolons or commas (hereinafter).
Format three: mailt user account @ Mail server address? subject= message subject &body= message body
such as: ShellExecute (handle, ' open ', ' [email protected]?subject=hello&body=this is a test ', nil, nil,
SW_SHOWNORMAL) opens a new mail window and automatically fills in the recipient's address, message subject, and message body. If the message body includes multiple lines of text, you must add a line break between each line of text
Escape character%0a.
Example (Delphi):
Call C:project1.exe in an application;
ShellExecute (handle, ' open ', ' c:project1.exe ', ' string contents ', nil, SW_SHOWNORMAL);
Can be called in Project1.exe:
Procedure Tform1.formcreate (Sender:tobject);
Var
I:integer;
Begin
For I:=1 to ParamCount do
If Paramstr (i) <> ' then
ShowMessage (Paramstr (i));
End
The last parameter, which specifies a command for the visibility of the window. Please use any of the following constants:
Sw_hide hidden window, active state to make a window
sw_minimize minimized window, active state to make a window
Sw_restore displays a window with its original size and position, while making it active
Sw_show displays a window with the current size and position, and makes it active
Sw_showmaximized to maximize the window and activate it
sw_showminimized Minimize the window and activate it
Sw_showminnoactive minimizes a window without changing the active window
Sw_showna displays a window with the current size and position without changing the active window
Sw_shownoactivate displays a window with the nearest size and position without changing the active window
Sw_shownormal is the same as Sw_restore
Delphi in ShellExecute use detailed