VC winexec Method for opening a specified program or file

Source: Internet
Author: User

(1) function prototype:
Uint win exec (maid, uint ucmdshow );
(2) parameters:
Lpcmdline: point to an empty ending string that contains the application to be executed. Program Command 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.
Use the ShellExecute command
(1) function prototype:
Quote: CopyCode The Code is as follows: hinstance ShellExecute (hwnd, lpctstr lpoperation, lpfile, lptstr lpparameters, lptstr lpdirectory, int nshowcmd );

(2) parameter:
hwnd: Specifies the window handle pointing to the parent window. This window receives any information boxes generated by the application.
lpoperation: An empty ending string address, which specifies the operation to be executed. The following Operation string is valid:
"open" This function opens the file specified by the lpfile parameter. This file can be an executable file or a document file, it is also a folder.
"print" This function prints the file specified by the lpfile parameter. This file should be a document file. If this file is an executable file, open the file.
"e" This function searches for folders specified by the lpfile parameter. This file is a document file.
this parameter can be blank. 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 be opened or searched.
lpparameters: If the lpfile parameter specifies an executable file, lpparameters is an empty string address, which specifies the parameter to be passed to the application. If lpfile specifies a document file, lpparameters should be empty.
lpdirectory: An empty ending string address, which specifies the default directory.
nshowcmd: If lpfile specifies an executable file, nshowcmd indicates how the application is displayed when it is opened. If lpfile specifies a document file, nshowcmd should be blank.
(3) Return Value:
if the function is successfully called, the return 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 a folder in either of the following formats:
code: copy Code the code is as follows: ShellExecute (handle, null, path_to_folder, null, null, sw_shownormal);

Or
Quote:Copy codeThe Code is as follows: ShellExecute (handle, "open", path_to_folder, null, null, sw_shownormal );

Search for folders in the following format:Copy codeThe Code is as follows: ShellExecute (handle, "cmde", path_to_folder, null, null, sw_shownormal );

the ShellExecute command is outdated but easy to get. This command sends a request to the command interpreter to open, browse, or print a document or folder. Although you can run the program using ShellExecute, only the document name is usually sent, the command interpreter decides the program to run. In addition, the ShellExecute command is not commonly used when you open a directory folder.
(4) program example
the following example shows how to use winexec and ShellExecute. The following program provides a console program example, which uses two different methods to open a text file. The following program uses winexec and explicitly specifies the Notepad program. Then, use ShellExecute to open a text file.
program list
code: copy Code the code is as follows: # include
# include
void main (INT argc, char * argv [])
{< br> 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:
Code:Copy codeThe Code is as follows: 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 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 identifier for Process Creation.
Lpenvironment: point to the Environment block of a new process.
Lpcurrentdirectory: point to an empty string that defines the current drive and directory of the child process.
Lpstartupinfo: points to a startupinfo structure, which defines how the new process's main window will be 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.
The ShellExecute and winexec commands are used for simple jobs. To completely control a new process, you must call CreateProcess.
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.

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.