VC WinExec the method to open the specified program or file _c language

Source: Internet
Author: User
Tags function prototype
⑴ function Prototype:
UINT Win Exec (lpcstr lpcmdline, UINT ucmdshow);
⑵ Parameters:
lpCmdLine: Point to a null-terminated string containing the command line (filename plus optional parameter) of the application to be executed.
Ucmdshow: Defines how Windows applications ' windows are displayed, and provides the values of Wshowwindow members of startupinfo parameters for the CreateProcess function.
⑶ return Value:
If the function call succeeds, the return value is greater than 31. If a function call fails, the return value is one of the following:
①0: System memory or resource is depleted.
Invalid ②error_bad_format:exe file (not 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 also requires deciding how to display the second parameter of the program that may be ignored by the program. Typically, it is set to Sw_show, or you can try sw_minimized or sw_maximized. WinExec does not allow all the options obtained with CreateProcess, and it is simple indeed.
Using the ShellExecute command
⑴ function Prototype:
Quote:
Copy Code code as follows:

HINSTANCE ShellExecute (HWND hwnd, LPCTSTR lpoperation, LPCTSTR lpfile, LPCTSTR lpparameters, LPCTSTR lpDirectory, INT nSho WCMD);

⑵ Parameters:
HWND: The window handle that points to the parent window. This window receives any information boxes that the application produces.
Lpoperation: A null-terminated string address that specifies the action to be performed. The following action string is valid:
Open this function opens the file specified by the parameter lpfile, which can be either an executable file or a document file, or a folder.
Print this function prints the file specified by the parameter lpfile, which should be a document file and open if the file is an executable file.
"Explore" This function searches the folder specified by the parameter lpfile, which should be a document file,
This parameter can be empty. In this case, the function is used to open the file specified by the parameter lpfile.
Lpfile: A null-terminated string address that specifies the file to open or print or the folder to open or search.
Lpparameters: If the parameter lpfile specifies an executable file, Lpparameters is a null-terminated string address that specifies the parameters to pass to the application. If lpfile specifies a document file, Lpparameters should be empty.
Lpdirectory: A null-terminated string address, this string specifies the default directory.
nShowCmd: If lpfile specifies an executable file, nShowCmd indicates how the application will appear when it is opened. If lpfile specifies a document file, nShowCmd should be empty.
⑶ return Value:
If the function call succeeds, the return value is greater than 32 or an error value less than or equal to 32.
Description: You can use this function to open or search a shell folder. You can open a folder in any of the following forms:
Code:
Copy Code code as follows:

ShellExecute (handle, NULL, path_to_folder, NULL, NULL, SW_SHOWNORMAL);

Or
Quote:
Copy Code code as follows:

ShellExecute (Handle, "open", path_to_folder, NULL, NULL, SW_SHOWNORMAL);

Search Folders, available in the following form
Copy Code code as follows:

ShellExecute (handle, "explore", path_to_folder, NULL, NULL, SW_SHOWNORMAL);

The ShellExecute command is outdated but easy to obtain. This command explains to the command interpreter the request to open, browse, or print a document or folder, although you can run the program with ShellExecute, but usually only the document name is sent, and the command interpreter decides to run that program. In addition, the ShellExecute command is useful when opening directory folders.
⑷ Program Example
The following is an example of the use of the name WinExec and ShellExecute. The following program has an example of a console program that uses two different methods to open a text file. The following program uses WINEXEC and explicitly specifies that the Notepad program be used. Then, using ShellExecute, open the text file.
List of programs
Code:
Copy Code code as follows:

#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);
}

Using the CreateProcess command
⑴ function Prototype:
Code:
Copy Code code as follows:

BOOL CreateProcess (
LPCTSTR Lpapplicationname,
LPTSTR lpCommandLine,
Lpsecurity_attributes Lpprocessattributes,
Lpsecurity_attributes Lpthreadattributes,
BOOL bInheritHandles,
DWORD dwCreationFlags,
LPVOID Lpenvironment,
LPCTSTR Lpcurrentdirectory,
Lpstartupinfo Lpstartupinfo,
Lpprocess_information lpprocessinformation
);

⑵ Parameters:
Lpapplicationname: Point to a string with an empty end, he specified the module to be executed
lpCommandLine: Points to an empty end 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 the handle from the calling process.
dwCreationFlags: Defines additional flags for controlling priority classes and process creation.
Lpenvironment: An environment block that points to a new process.
Lpcurrentdirectory: Points to an empty end 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 for the 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.
The ShellExecute and WinExec commands are used for simple jobs. If you want to fully control a new process, you must call CreateProcess.
In the above parameter, the parameter lpstartupinfo is the STARTUPINFO structure. Can be used to set the header of the console, the initial size and position of the new window, and redirect standard input and output. New programs can usually ignore most of these data items if you choose to do so. You can specify a flag in the structure body that indicates the data segment that you want 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). Parameter lpprocessinformation returns process and thread handles, and also includes process and thread IDs. These handles have access specified in the Parameters lpprocessattributes and lpthreadattributes.
Note that some of the 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. The return value is Boolean, and the return value that is truly interesting occurs in the structure that is passed 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.