VC + + method to run another program in the program

Source: Internet
Author: User

There are three ways to run another program in the VC + + program: WinExec (), Shellexcute (), and CreateProcess ()

Three SDK functions:   winexec, ShellExecute   , CreateProcess You can implement calls to other programs, where WinExec is the simplest, ShellExecute is more flexible than winexec, and CreateProcess is the most complex.
    WinExec  two parameters, the previous specified path, followed by a specified display mode.
    ShellExecute  can specify a working directory, and can also find the association of files directly open without loading the application associated with the file, ShellExecute can also open the Web page, Start the appropriate message association to send the message, and so on.
    CreateProcess  has 10 parameters, but most of them can be replaced with NULL, which can specify the security properties of the process, the inheritance information, the class priority, and so on. If we want to get enough information about the new process and control the details of the new process, we need to use the CreateProcess function to achieve these goals.
 
syntax for three SDK functions ( winexec, Shellexec, crateprocess ):
 

winexec

This function is the simplest, with only two parameters, the prototype is as follows:

 
      uint winexec (

      LPCSTR lpCmdLine,   //  Command path

       UINT uCmdShow      //  display mode

     )

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.


Here's how to use it:

WinExec ("Notepad.exe", sw_show); Open Notepad
WinExec ("D://program files//test//test.exe", sw_showmaximized); To maximize the way to hit Test.exe where the sw_show,sw_showmaximized is the way the window is displayed when executing the program, defined in Winuser.h.
Note that if you use sw_showmaxmized to load a program that does not have a maximized button, such as Neterm,calc, there will be no normal form, but it has been added to the task list.


ShellExecute

The prototype is as follows:

HInstance ShellExecute (

HWND hwnd,//parent window Handle

LPCTSTR lpoperation,//operation, open "edit", "Explore", "open", "find", "print", "NULL"

LPCTSTR lpfile,//file name, front add path

LPCTSTR lpparameters,//Parameter

LPCTSTR lpdirectory,//default folder

INT nshowcmd//display mode

);


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 perform. The following action string is valid:

"Open" This function opens the file specified by the parameter lpfile, which can be an executable file or a document file, but also a folder.
"Print" This function prints the file specified by the parameter lpfile, which should be a document file, and if the file is an executable file, open the file.
"Explore" This function searches for 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, the lpparameters should be empty.

Lpdirectory: A null-terminated string address that 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, the nShowCmd should be empty.

return value:

If the function call succeeds, the return value is greater than 32, otherwise an error value of less than or equal to 32.

Description: You can use this function to open or search for a shell folder. Open folders can be in any of the following forms:

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

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

Search Folders, which can be used in the following form

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

The ShellExecute command is outdated but easy to get. This command makes a request to the command interpreter to open, browse, or print a document or folder, although the program can be run with ShellExecute, but usually only the document name is sent, and the command interpreter decides to run the program. The ShellExecute command is also useful when opening a directory folder.


Here's how to use it:
ShellExecute (NULL, "open", "C://test.txt", null,null,sw_shownormal); Open C:/test.txt File
ShellExecute (NULL, "open", "http://www.google.com", NULL, NULL, SW_SHOWNORMAL); Open a webpage www.google.com
ShellExecute (NULL, "Explore", "d://c++", null,null,sw_shownormal); Open Directory D:/c++
ShellExecute (NULL, "print", "C://test.txt", Null,null, Sw_hide); Print File C:/test.txt
ShellExecute does not support directed output.

Sample Program

The following is an example of the use of the name winexec and ShellExecute. The following program has a console program example that uses two different methods to open a text file. The following program uses winexecand explicitly specifies that the Notepad program is used. Then, using ShellExecute, open the text file.

List of programs

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


CreateProcess

The prototype is as follows:

BOOL CreateProcess (

LPCTSTR Lpapplicationname,//Execute program name

LPTSTR lpcommandline,//Parameter line

The following two parameters describe the security properties of the processes and threads that are created, and if NULL, the default security attributes are used

Lpsecurity_attributes lpprocessattributes,//Process security ATTRIBUTES

Lpsecurity_attributes lpthreadattributes,//thread security ATTRIBUTES

BOOL binherithandles,//Inheritance flag

DWORD dwcreationflags,//Create flag

LPVOID lpenvironment,//environment variable

LPCTSTR lpcurrentdirectory,//Run the initial directory of the process

Lpstartupinfo Lpstartupinfo,//For setting various properties when creating a child process

Lpprocess_information lpprocessinformation//For receiving information after the process has been created

;

Here's how to use it:
Process_information Pi;
Startupinfo si;
memset (&si,0,sizeof (SI));
Si.cb=sizeof (SI);
Si.wshowwindow=sw_show;
Si.dwflags=startf_useshowwindow;
BOOL Fret=createprocess ("D://putty.exe", NULL,NULL,FALSE,NULL,NULL,NULL,NULL,&AMP;SI,&AMP;PI);


Parameters:

Lpapplicationname: Point 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 priority 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 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, and if the function call fails, the return value is 0.

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.

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.
As can be seen, through the above several different methods, can be implemented in the application to open other applications, some of the methods may be troublesome, so we need to choose the most suitable for their own purpose of the way to achieve their goal!





About the three SDK functions: WinExec, Shellexecute,createprocessOther Precautions:

1. Define the header file

The following two header files must be defined in the header file stdafx.h:

#include <shlobj.h>//Can be replaced by windows.h
#include <shellapi.h>
If you define a header file #include <windows.h> you do not have to define #include <shlobj.h>, "windows.h" not only contains "shellapi.h", it also defines a number of data types, Without these data types, the shellapi.h itself will go wrong.

2. Define the path

The path represented in C + + is "//" instead of the usual "/", so the above three functions represent a path:

Disk://directory//...//file Name

WinExec ("D://program files//test//test.exe", sw_showmaximized);

ShellExecute (NULL, "open", "C://test.txt", null,null,sw_shownormal);
BOOL Fret=createprocess ("D://putty.exe", NULL,NULL,FALSE,NULL,NULL,NULL,NULL,&AMP;SI,&AMP;PI);

This prawn is also good oh: http://www.blogcn.com/u2/81/45/jinrightherewaiting/index.html

VC + + method to run another program in the program

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.