There are three API functions that can run the executable files winexec, ShellExecute, and CreateProcess. Because of its complexity, CreateProcess is rarely used.
Winexec mainly runs the EXE file.
(1) function prototype: uint win exec (lpcstr lpcmdline, uint ucmdshow );
(2) parameters:
Lpcmdline: point to an empty ending string that contains the application to be executed.ProgramCommand Line (file name with optional parameters ).
Ucmdshow: defines how Windows application windows are displayed, and provides the value of the wshowwindow member of startupinfo for the CreateProcess function.
(3) return value:
If the function is successfully called, the return value is greater than 31. If the function call fails, the return value is one of the following:
① 0: The system memory or resources are used up.
② Error_bad_format: the EXE file is invalid (non-win32.exe or. EXE image error ).
③ Error_file_not_found: the specified file is not found.
④ Error_path_not_found: the specified path is not found.
Microsoft considers winexec obsolete, but in many cases, simple winexec functions are still the best way to run new programs. Simply passing the command line that acts as the first parameter also requires deciding how to display the second parameter of the Program (which may ignore it. Generally, set it to sw_show. You can also try sw_minimized or sw_maximized. Winexec does not allow all options obtained using CreateProcess, but it is indeed simple.
ShellExecute can run not only EXE files, but also associated files.
1. Standard usage
The meanings of the parameters and prototype of the ShellExecute Function are as follows:
Hinstance ShellExecute (hwnd, lpctstr lpoperation, lpfile, lptstr lpparameters, lpdirectory, int nshowcmd );
● Hwnd: Specifies the parent window handle. When an error occurs during a function call, it is used as the parent window of the Windows message window. For example, you can set it to the application Main Window handle, that is, application. Handle, or desktop window handle (obtained using the getdesktopwindow function ).
● Lpoperation: used to specify the operation to be performed. The "open" operation indicates that the program specified by the filename parameter is executed, or the file or folder specified by the filename parameter is opened. The "print" operation indicates that the file specified by the filename parameter is printed; the "Browse e" operation indicates browsing the folder specified by the filename parameter. If the parameter is set to nil, the default operation "open" is executed ".
● Lpfilename: used to specify the name of the file to be opened, the name of the program file to be executed, or the name of the folder to be browsed.
● Lpparameters: If the filename parameter is an executable program, this parameter specifies the command line parameter. Otherwise, this parameter should be nil or pchar (0 ).
● Lpdirectory: used to specify the default directory.
● Lpshowcmd: If the filename parameter is an executable program, this parameter specifies the initial display mode of the program window; otherwise, this parameter should be set to 0.
Return Value:
# Include <stdio. h>
# Include <stdlib. h>
# Include <windows. h>
# Include <shellapi. h>
Int Main ( Void )
{
Hinstance hnewexe = shellexecutea (null, " Open " , " D: \ TESE. Log " , Null, null, sw_show );
If (DWORD) hnewexe <= 32 )
{
Printf ( " Return Value: % d \ n " , (DWORD) hnewexe );
}
Else
{
Printf ( " Successed! \ N " );
}
Printf ("getlasterror: % d \ n", getlasterror ());
System ( " Pause " );
Return 1 ;
}
If the "D: \ test. log" file does not exist, the execution result is as follows:
If a function execution error occurs, getlasterror () may not capture the error code, for example, when "D: \ TESE. log file exists. When Notepad "notepad.exe" is named as another name:
The return values of the other two functions are not listed.
2. Special usage
1) if you set the filename parameter to the "http:" protocol format, this function will open the default browser and link to the specified URL address. If multiple browsers are installed on your machine, the function determines which browser to start based on the settings of the HTTP handler (Protocols handler) in Windows 9x/NT Registry.
Format 1: http: // website domain name. Example: ShellExecute (handle, "open", "http://www.neu.edu.cn", nil, nil, sw_shownormal );
Format 2: http: // website domain name/Webpage file name. Example: ShellExecute (handle, "open", "http://www.neu.edu.cn/default.htm", nil, nil, sw_shownormal );
2) If you set the filename parameter to the "mailto:" protocol format, this function starts the default mail client program, such as Microsoft Outlook (including Microsoft Outlook Express) or Netscape Messanger. If multiple email client programs are installed on your machine, this function determines which email client program to start based on the settings of the mailto protocol handler in the Windows 9x/NT Registry.
Format 1: mailto: for example: ShellExecute (handle, "open", "mailto:", nil, nil, sw_shownormal); open the new mail window.
Format 2: mailto: User Account @ mail server address such as: ShellExecute (handle, "open", "mailto: who@mail.neu.edu.cn", nil, nil, sw_shownormal );
Open the new email window and enter the recipient address automatically. If multiple recipient addresses are specified, the recipient addresses must be separated by semicolons or commas (, the same below)
Example: ShellExecute (this-> m_hwnd, "open", "mailto: nishinapp@yahoo.com", "", "", sw_show );
Format 3: mailto: User Account @ email server address? Subject = Email Subject & Body = Email body
Example: ShellExecute (handle, "open", "mailto: who@mail.neu.edu.cn? Subject = Hello & Body = This is a test ", nil, nil, sw_shownormal );
Open the new email window and enter the recipient address, subject, and body. If the mail body contains multiple lines of text, you must add the line feed escape character % 0a between each line of text.
Example (Delphi ):
Call C: project1.exe in an application;
ShellExecute (handle, 'open', 'c: project1.exe ', 'string content', nil, sw_shownormal );
In project1.exe, you can call:
ProcedureTform1.formcreate (Sender: tobject );
VaRI: integer;
Begin
ForI: =1 ToParamcountDo
IfParamstr (I) <> )〈〉''ThenShowmessage (paramstr (I ));
End;
The last parameter specifies the visibility command for the window. Use any of the following Constants
Sw_hide hides the window, and the activity status gives a window
Sw_minimize minimizes the window, and the active status gives a window
Sw_restore displays a window with the original size and position, and enables it to enter the active status.
Sw_show displays a window with the current size and position, and enables it to enter the active status.
Sw_showmaximized: Maximize the window and activate it.
Sw_showminimized minimizes the window and activates it.
Sw_showminnoactive minimizes a window without changing the activity window.
Sw_showna displays a window with the current size and position without changing the activity window.
Sw_shownoactivate displays a window with the latest size and position without changing the activity window.
Sw_shownormal is the same as sw_restore.
3. Go to ShellExecute Translator: Xu Jingzhou (Original: nishant S)
Q: How to open an application? As you can see, I have not passed the complete path of the program.
ShellExecute (this-> m_hwnd, "open", "calc.exe", "", "", sw_show );
Or ShellExecute (this-> m_hwnd, "open", "notepad.exe", "C: \ mylog. log", "", sw_show );
Q: How do I open a file related to the same system program?
ShellExecute (this-> m_hwnd, "open", "C: \ abc.txt", "", "", sw_show );
Q: How to open a webpage?
ShellExecute (this-> m_hwnd, "open", "http://www.google.com", "", "", sw_show );
Q: How to activate related programs and send emails?
ShellExecute (this-> m_hwnd, "open", "mailto: nishinapp@yahoo.com", "", "sw_show );
Q: How do I print documents with a system printer?
ShellExecute (this-> m_hwnd, "print", "C: \ abc.txt", "", "", sw_hide );
Q: How can I use the system search function to find a specified file?
ShellExecute (m_hwnd, "find", "d: \ Nish", null, null, sw_show );
Q: How do I start a program until it finishes running?
Shellexecuteinfo shexecinfo ={0};
Shexecinfo. cbsize = sizeof (shellexecuteinfo );
Shexecinfo. fmask = see_mask_nocloseprocess;
Shexecinfo. hwnd = NULL;
Shexecinfo. lpverb = NULL;
Shexecinfo. lpfile = "C: \ myprogram.exe ";
Shexecinfo. lpparameters = "";
Shexecinfo. lpdirectory = NULL;
Shexecinfo. nshow = sw_show;
Shexecinfo. hinstapp = NULL;
Shellexecuteex (& shexecinfo );
Waitforsingleobject (shexecinfo. hprocess, infinite );
Or:
Process_information processinfo;
Startupinfo; // This is an [in] Parameter
Zeromemory (& startupinfo, Sizeof (Startupinfo ));
Startupinfo. cb = Sizeof (Startupinfo ); // Only compulsory field
If (CreateProcess ( " C: \ winnt \ notepad.exe " , Null, false, 0 , Null, null, & startupinfo, & processinfo ))
{
Waitforsingleobject (processinfo. hprocess, infinite );
Closehandle (processinfo. hthread );
Closehandle (processinfo. hprocess );
}
Else
{
MessageBox ( " The process cocould not be started... " );
}
Q: How do I display attributes of a file or folder?
Shellexecuteinfo shexecinfo = {0 };
Shexecinfo. cbsize = Sizeof (Shellexecuteinfo );
Shexecinfo. fmask = see_mask_invokeidlist;
Shexecinfo. hwnd = NULL;
Shexecinfo. lpverb = " Properties " ;
Shexecinfo. lpfile = " C :\\ " ; // Can be a file as well
Shexecinfo. lpparameters = "" ;
Shexecinfo. lpdirectory = NULL;
Shexecinfo. nshow = sw_show;
Shexecinfo. hinstapp = NULL;
Shellexecuteex (& shexecinfo );
Use the CreateProcess command
(1) function prototype:
bool CreateProcess (
lptstr lpapplicationname,
lptstr lpcommandline,
lpsecurity_attributes lpprocessattributes,
lpsecurity_attributes attributes,
bool binherithandles,
DWORD dwcreationflags,
lpvoid lpenvironment,
maid directory,
lpstartupinfo,
lpprocess_information lpprocessinformation
);
(2) parameter:
lpapplicationname: points to a string ending with an empty string, which specifies the module to be executed
lpcommandline: points to a string ending with an empty string, this string defines the command line to be executed.
lpprocessattributes: points to a security_attributes structure, which determines whether the returned handle can be inherited by the quilt process.
lpthreadattributes: points to a security_attributes structure, which determines whether the returned handle can be inherited by the quilt process.
binherithandles: indicates whether the new process inherits the handle from the called process.
dwcreationflags: defines the control priority class and additional flag for Process Creation.
lpenvironment: point to the Environment block of a new process.
lpcurrentdirectory: indicates an empty ending string that defines the current drive and directory of the sub-process.
lpstartupinfo: points to a startupinfo structure, which defines how the new process's main window is displayed.
lpprocessinformation: point to the process_information structure, which accepts the representation information about the new process.
(3) Return Value:
If the function call is successful, the return value is not 0. If the function call fails, the return value is 0.
In the preceding parameters, the lpstartupinfo parameter is the startupinfo structure. You can set the title of the console, the initial size and position of the new window, and redirect standard input and output. Most of these data items can be ignored by new programs. You can specify a flag in the struct that indicates the data segment to be set. Sometimes, if you do not want to set any information, you must also pass a valid pointer to the null structure (make sure to set the size to CB, and set the dwflags member to 0 ). The lpprocessinformation parameter returns the process and thread handle, including the process and thread ID. These handles have the access specified in the lpprocessattributes and lpthreadattributes parameters.
Note that some parameters for CreateProcess are specific to the console application, while other parameters are useful to various applications. In most cases, you do not have to enter the startupinfo structure, but you must provide it anyway. The return value is Boolean, and the returned value that is really interest occurs in the structure transmitted as a parameter (process_information ). CreateProcess returns the process ID and its handle in the structure, as well as the initial thread ID and its handle. You can send an ID to another process or use a handle to control the new process.
The ShellExecute and winexec commands are used for simple jobs. To completely control a new process, you must call CreateProcess.
[Thank you for your reference]
1,Winexec, ShellExecute, and CreateProcess
2,Winexec, ShellExecute, CreateProcess