Calling EXE executable in C + + program

Source: Internet
Author: User
Tags clear screen function prototype
calling EXE executable in C + + programIn the development of the project, sometimes separate to develop, sometimes separate EXE file, some times will also call the ready-made toolkit inside some of the EXE file, so that in the project will be through the call EXE file to use.

Then in C + + directly call the EXE file in the way there. The main options to consider now are:

A. Using the system function

B. Using the exec or the EXECV function

C. Using the WinExec function

D. Using the CreateProcess function

E. Using the Shellexcecuteex function

1 in the above 5 method, the system function, function prototype system (execute shell command) definition function is, int system (const char * string), you can invoke some DOS commands, such as:

System ("CLS");/clear screen, equal to use CLS command on DOS

The system function invokes the fork () function to produce the child horizontally, and the subprocess calls/bin/sh-c string to execute the command represented by the parameter string string, which then returns the invoked process, which cannot control whether the window is displayed. Generally, this method is not considered in the project.

2 using EXECL or EXECV functions

This is the two functions in the EXEC function family, in the case of UNIX, the EXEC function family finds the executable file based on the specified filename and replaces the contents of the calling process, that is, executing an executable file inside the calling process, where the executable file can be a binary file , can also be any Linux executable script file, unlike the general situation, the EXEC function family function execution succeeds will not return, only if the call fails to return a-1, from the original program's call point and then proceed down. It is also unable to control whether the window is displayed.

3) using the WinExec function

The WinExec function makes it easy to execute a program in an application and perform a standard procedure for general use:

WinExec (' C:\WINDOWS\NOTEPAD. EXE ', SW_SHOWNORMAL);

The second parameter is the Control Program main window display mode, with WINEXEC can only execute EXE file, but the WinExec function is not good to control the main program to wait for the end of the EXE program. WinExec is the old function of Windows, and now the CreateProcess can almost replace all the features of winexe.

4) using the CreateProcess function

The CreateProcess function is to create a new process, and here is an example of using the CreateProcess function:

void Createchildprocessandwaituntildone (const ansistring& strcmdline)
{

Process_information Piprocinfo;
Startupinfo Sistartinfo;

Set up members of STARTUPINFO structure.
SISTARTINFO.CB = sizeof (STARTUPINFO);
sistartinfo.lpreserved = null;
Sistartinfo.lpreserved2 = null;
Sistartinfo.cbreserved2 = 0;
Sistartinfo.lpdesktop = null;
sistartinfo.dwflags = 0;


Create the child process.
CreateProcess (

Null
Strcmdline.c_str (),
NULL,//process security attributes
NULL,//primary thread security attributes
0,//Handles are inherited
0,//creation Flags
NULL,//Use parent\ ' s environment
NULL,//Use parent\ ' s current directory
&sistartinfo,//Startupinfo pointer
&piprocinfo); Receives process_information

The processs to finish
DWORD rc = WaitForSingleObject (
Piprocinfo.hprocess,//process handle
Infinite);
}

With the CreateProcess function, you have to artificially control the life and death of the process, which is implemented using Process control. The meaning of process control is that you can create a process, and you can end the process through a process handle.

5) using the Shellexcecuteex function

This function essentially executes a function on a file and returns a value other than 0 if the execution succeeds, or returns a value of 0. The prototype of the function is:

Winshellapi BOOL WINAPI ShellExecuteEx (
Lpshellexecuteinfo lpexecinfo);

Parameter lpexecinfo: Is the address of the data structure Lpshellexecuteinfo, which contains and receives some information about the executed application. The following is an example used:

Shellexecuteinfo Shexecinfo = {0};
shexecinfo.cbsize = sizeof (SHELLEXECUTEINFO);
Shexecinfo.fmask = see_mask_nocloseprocess;
Shexecinfo.hwnd = NULL;
Shexecinfo.lpverb = _t ("open");
Shexecinfo.lpfile = _t ("A.exe");
Shexecinfo.lpparameters = _t ("a A");
Shexecinfo.lpdirectory = NULL;
Shexecinfo.nshow = Sw_hide;
Shexecinfo.hinstapp = NULL;
ShellExecuteEx (&shexecinfo);
AfxGetApp ()->beginwaitcursor ();
WaitForSingleObject (Shexecinfo.hprocess,infinite);
AfxGetApp ()->endwaitcursor ();

This function can pass the EXE file's parameter through the Shexecinfo.lpparameters, through the property shexecinfo.nshow to control whether the window displays.

The back three functions are SDK functions, now in the implementation of the EXE file in the project, the use of more is the back of the two function, I used in my project is the ShellExecuteEx function, more convenient than the CreateProcess function.


--Reprint--label:  C + +

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.