(Favorites) winexec, ShellExecute, CreateProcess

Source: Internet
Author: User

Winexec, ShellExecute, CreateProcess
<Http://www.cppblog.com/pingtoo/archive/2008/11/08/66288.html>
Use the winexec command

(1) function prototype:

Uint win exec (maid, uint ucmdshow );

(2) parameters:

Lpcmdline: A string pointing to an empty ending string containing the commands of the application to be executed.
Line (file name with optional parameters ).

Ucmdshow: defines how Windows display, and provides
The value of the wshowwindow member used by startupinfo.

(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
Is the best way to run new programs. To transmit the command line as the first parameter, you also need to determine
The second parameter of the display program (which may ignore it. Usually, set it to sw_show,
You can also try sw_minimized or sw_maximized. Winexec cannot be obtained using CreateProcess
All options, and it is indeed simple.

The second parameter has the following seven options:
Sw_maximize // maximize run
Sw_minimize // minimize run
Sw_restore // restore the maximized or minimized window to normal
Sw_showmaximized // activate the window and maximize the running
Sw_showminimized // activate the window and minimize the running
Sw_shownoactivate // not activated
Sw_shownormal // Normal Mode

Use the ShellExecute command

(1) function prototype:

Hinstance ShellExecute (hwnd, lpctstr lpoperation, lpfile, lptstr lpparameters, lpdirectory, int nshowcmd );

(2) parameters:

Hwnd: The Window handle pointing to the parent window. This window receives any information boxes generated by the application.

Lpoperation: An empty string address, which specifies the operation to be performed. Below
The operation string is valid:

"Open" This function opens the file specified by the lpfile parameter. This file can be an executable file or
File, but also a folder.
"Print" This function prints the file specified by the lpfile parameter. This file should be a document file. If
Open an executable file.
"E" This function searches for folders specified by the lpfile parameter. This file should be a document file,

This parameter can be null. In this case, the function is used to open the file specified by the lpfile parameter.

Lpfile: An empty ending string address, which specifies the file to be opened or printed or
The folder to open or search.

Lpparameters: If the lpfile parameter specifies an executable file, lpparameters is
Null end string address, which specifies the parameters to be passed to the application. If lpfile specifies
Document files, lpparameters should be empty.

Lpdirectory: An empty ending string address, which specifies the default directory.

Nshowcmd: If lpfile specifies an executable file, nshowcmd indicates that the application is opened as shown in
Display. If lpfile specifies a document file, nshowcmd should be blank.

(3) return value:

If the function is successfully called, the returned value is greater than 32. Otherwise, it is an error value smaller than or equal to 32.

Note: You can use this function to open or search for a shell folder. Open the folder and use any of the following
Format:

ShellExecute (handle, null, path_to_folder, null, null, sw_shownormal );
  
Or

ShellExecute (handle, "open", path_to_folder, null, null, sw_shownormal );

Search for folders in the following format:

ShellExecute (handle, "cmde", path_to_folder, null, null, sw_shownormal );

The ShellExecute command is outdated but easy to get. This command proposes to the Command Interpreter
Although you can use ShellExecute to run a program
The document delivery name, and the command interpreter decides to run the program. In addition, open the directory folder
The ShellExecute command is not commonly used.

(4) program example

The following example shows how to use winexec and ShellExecute. The following program has a console
Program example, which uses two different methods to open a text file. The following program uses winexec and
The Notepad program is specified. Then, use ShellExecute to open a text file.

Program list

# Include <windows. h>
# Include <iostream. h>

Void main (INT argc, char * argv [])
{
Cout <"opening with winexec \ n ";
If (winexec ("Notepad readme.txt", sh_show) <32)
Messagbox (null, "can't winexec", null, mb_ OK );
Cout <"press enter \ n ";
Messagbox (null, "press OK to continue", "progrm launched", mb_ OK );
Cout <"opening with ShellExecute \ n ";
If (ShellExecute (null,?open=,#readme.txt ", null, null, sw_show)
<(Handle) 32)
Messagbox (null, "can't ShellExecute \ n", null, mb_ OK );
}

Use the CreateProcess command

(1) function prototype:

Bool CreateProcess (
Lptstr lpapplicationname,
Lptstr lpcommandline,
Lpsecurity_attributes lpprocessattributes,
Lpsecurity_attributes lpthreadattributes,
Bool binherithandles,
DWORD dwcreationflags,
Lpvoid lpenvironment,
Maid directory,
Lpstartupinfo,
Lpprocess_information lpprocessinformation
);

(2) parameters:

Lpapplicationname: point to an empty string that specifies the module to be executed.

Lpcommandline: point to an empty string that defines the command line to be executed.

Lpprocessattributes: points to a security_attributes structure, which determines the return
Whether the return handle can be inherited by the quilt process.

Lpthreadattributes: points to a security_attributes structure, which determines the return
Whether the return 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 identifier for Process Creation.

Lpenvironment: point to the Environment block of a new process.

Lpcurrentdirectory: a string that points to an empty end. This string defines the current driver of the sub-process.
And the current directory.

Lpstartupinfo: point to a startupinfo structure, which defines
How to display.

Lpprocessinformation: point to the process_information structure, which accepts information about the new
Indicates the 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.

The ShellExecute and winexec commands are used for simple jobs. To completely control a new process,
CreateProcess must be called.

In the preceding parameters, the lpstartupinfo parameter is the startupinfo structure. Can be used to set the Console
Title, initial size and position of the new window, and redirection standard input and output. New programs can usually
Ignore most of these data items, if you choose to do that. You can specify a flag in the struct, which indicates
The data segment to be set. Sometimes, if you do not want to set any information, you must pass a valid pointer to the null knot.
(Determine to set the size to CB, and set the dwflags member to 0 ). Returns the lpprocessinformation parameter.
Process and thread handle, including the process and thread ID. These handles have
Access specified in lpprocessattributes and lpthreadattributes.

Note that some parameters for CreateProcess are specific to the console application, while others
Parameters are useful to various applications. In most cases, it is not necessary to enter the startupinfo structure,
But it must be provided in any way. The return value is Boolean, and the returned value that is actually interested occurs
Parameter transfer structure (process_information ). CreateProcess returns
The process ID and its handle, as well as the initial thread ID and its handle. You can send the ID to another process or use a sentence
To control the new process.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.