There are three API functions that can run executable files winexec, ShellExecute, and CreateProcess. CreateProcess is less used because it is more complex to use.
WinExec mainly run EXE files.
⑴ function prototype: UINT Win Exec (LPCSTR lpcmdline, uint ucmdshow);
⑵ Parameters:
lpCmdLine: Point to a null-terminated string that contains the command line (filename plus optional parameter) of the application that will be executed.
Ucmdshow: Defines how Windows applications ' windows are displayed, and provides the CreateProcess function with the value of the Wshowwindow member of the Startupinfo parameter.
⑶ return Value:
If the function call succeeds, the return value is greater than 31. If the function call fails, the return value is one of the following:
①0: System memory or resource is exhausted.
The ②error_bad_format:exe file is not valid (not a Win32.EXE or. exe image error).
③error_file_not_found: The specified file was not found.
④error_path_not_found: The specified path was not found.
Although Microsoft believes that WinExec is obsolete, in many cases, a simple winexec function is still the best way to run a new program. Simply passing the command line as the first parameter, you also need to decide how to display the second parameter of the program (which the program might ignore). Typically, you set it to Sw_show, or you can try sw_minimized or sw_maximized. WinExec does not allow all options obtained with CreateProcess, and it is indeed simple.
ShellExecute can run not only exe files, but also files that are already associated.
1. Standard usage
ShellExecute function prototype and parameter meanings are as follows:
HINSTANCE ShellExecute (HWND hwnd, LPCTSTR lpoperation, LPCTSTR lpfile, LPCTSTR lpparameters, lpctstr lpDirectory, INT nshowcmd);
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 to the application main window handle, which is application.handle, or you can set it as the desktop window handle (obtained with the GetDesktopWindow function).
Lpoperation: 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 folder specified by the filename parameter is opened, and the "print" operation means that the file specified by the filename parameter is printed; "Explore" The action represents browsing the folder specified by the filename parameter. When the parameter is set to nil, the default action "open" is executed.
lpFileName: Used to specify the file name to open, the name of the program file to execute, or the name of the folder to browse.
Lpparameters: If the filename parameter is an executable program, this parameter specifies a command-line argument, 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 how the program window is initially displayed, 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;
}
When the "D:\\test.log" file does not exist, the execution results are as follows:
If the function executes the error, GetLastError () may not catch the error code, for example, when the "D:\\tese.log" file exists, and the Notepad "notepad.exe" is named as a different name:
The return values for the other two functions are not listed.
2. Special usage
1) If you set the filename parameter to the "http:" protocol format, the function will open the default browser and link 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);
2) If the filename parameter is set to the "mailto:" 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 determines which mail client to start based on the settings of the Mailto protocol handler in the Windows 9x/nt registry.
Format one: mailto: ShellExecute (handle, "open", "mailto:", nil, Nil, SW_SHOWNORMAL); Open the new mail window.
Format two: mailto: User account @ Mail server address such as: ShellExecute (handle, "open", "mailto:[email protected]", nil, Nil, SW_SHOWNORMAL);
Opens a new mail window and automatically fills in the recipient's address. If multiple recipient addresses are specified, the recipient addresses must be separated by semicolons or commas (hereinafter)
such as: ShellExecute (This->m_hwnd, "open", "mailto:[email protected]", "", "", sw_show);
Format three: mailto: User account @ Mail server address? subject= message subject &body= message body
such as: ShellExecute (handle, "open", "mailto:[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 newline escape character%0a between each line of text.
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));
The last parameter, which specifies a command for the visibility of the window. Please use any one 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
3, in layman ShellExecute Translator: Xu Jingzhou (Original: Nishant S)
Q: How do I open an app? As you can see, I did not pass the full 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 document associated with a system program?
ShellExecute (This->m_hwnd, "open", "C:\\abc.txt", "", "", sw_show);
Q: How do I open a webpage?
ShellExecute (This->m_hwnd, "open", "http://www.google.com", "", "", sw_show);
Q: How to activate the relevant program, send email?
ShellExecute (This->m_hwnd, "open", "mailto:[email protected]", "", "", sw_show);
Q: How do I print a document using a system printer?
ShellExecute (This->m_hwnd, "print", "C:\\abc.txt", "", "", sw_hide);
Q: How can I use the system lookup function to find the specified file?
ShellExecute (M_hwnd, "find", "d:\\nish", NULL, NULL, SW_SHOW);
Q: How do I start a program until it runs to the end?
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);
Or:
Process_information ProcessInfo;
Startupinfo Startupinfo; This is a [in ] parameter
ZeroMemory (&startupinfo, sizeof (STARTUPINFO));
STARTUPINFO.CB = sizeof (STARTUPINFO); Only compulsory field
if (CreateProcess ("C:\\winnt\\notepad.exe", NULL, null,null,false,0,null, null,&startupinfo,& ProcessInfo))
{
WaitForSingleObject (Processinfo.hprocess,infinite);
CloseHandle (Processinfo.hthread);
CloseHandle (processinfo.hprocess);
}
Else
{
MessageBox ("The process could not being started ... ");
}
Q: How do I display the properties 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 is a file as well
Shexecinfo.lpparameters = "";
Shexecinfo.lpdirectory = NULL;
Shexecinfo.nshow = sw_show;
Shexecinfo.hinstapp = NULL;
ShellExecuteEx (&shexecinfo);
Using the CreateProcess command
⑴ function Prototypes:
Lpsecurity_attributes Lpprocessattributes,
Lpsecurity_attributes Lpthreadattributes,
BOOL bInheritHandles,
LPVOID Lpenvironment,
LPCTSTR Lpcurrentdirectory,
Lpstartupinfo Lpstartupinfo,
Lpprocess_information lpprocessinformation
);
⑵ parameter:
Lpapplicationname: Points to a string with a null end, he specifies the module to execute
lpCommandLine: points to a null-terminated string that defines the command line to execute.
Lpprocessattributes: points to a security_attributes structure that determines whether the returned handle can be inherited by the quilt process.
Lpthreadattributes: points to a security_attributes structure that determines whether the returned handle can be inherited by the quilt process.
bInheritHandles: Indicates whether the new process inherits a handle from the calling process.
dwCreationFlags: Defines additional flags that control the precedence class and process creation.
Lpenvironment: the environment block that points to a new process.
Lpcurrentdirectory: points to a null-terminated string that defines the current drive and current directory of the child process.
Lpstartupinfo: points to a STARTUPINFO structure that defines how the main window of a new process will be displayed.
Lpprocessinformation: points to the process_information structure that accepts presentation information about the new process.
⑶ return value:
If the function call succeeds, the return value is not 0; If the function call fails, the return value is 0.
In the above parameters, the parameter lpstartupinfo is the STARTUPINFO structure. It can be used to set the header of the console, the initial size and position of the new window, and redirect the standard input and output. The new program can often ignore most of these data items, if you choose to do so. You can specify a flag in the struct that indicates the data segment to set. Sometimes, you do not want to set any information, you must also pass a valid pointer to the empty structure (determine the set size to CB, and set the dwflags member to 0). The parameter lpprocessinformation returns the process and thread handles, and also includes the process and thread IDs. These handles have access specified in the Parameters lpprocessattributes and lpthreadattributes.
Note that some parameters for CreateProcess are specific to the console application, while other parameters are useful for a variety of applications. In most cases, it is not always necessary to fill in the STARTUPINFO structure, but it must be provided anyway. Its return value is Boolean, and the return value of real interest occurs in the structure that is passed as a parameter (process_information). CreateProcess returns the process ID and its handle in the structure, along with the initial thread ID and its handle. You can send the ID to another process, or use a handle to control the new process.
The ShellExecute and WinExec commands are used for simple jobs. If you want to take full control of a new process, you must call CreateProcess.
"References thank the Author"
1, WinExec, ShellExecute and CreateProcess
2, WinExec, ShellExecute, CREATEPROCESS
Transferred from: http://www.cnblogs.com/ziwuge/archive/2012/03/12/2392472.html
WinExec, ShellExecute and CreateProcess and the way of judging return values [go]