Run the. exe file and get the return value (using WinExec and system differences)

Source: Internet
Author: User

Recently, with the students to write an interface, you need to call to the release of the. exe file, which requires the transmission of command-line arguments and get the return value , although not a very complicated thing, but still to find some information recorded, May bring a little convenience to friends who have the same questions.

At first, I used the winexec function, Baidu to call the. exe file function, there will be a lot of winexec, ShellExecute, CreateProcess These three functions introduced, their complexity from low to high in order to increase the level, Visible WinExec is the simplest one, because I only need a simple call, so temporarily use WinExec.

For the use of the above three functions, you can refer to the following links: http://blog.csdn.net/manji_lee/article/details/8079110

I thought it was all over, but my program needs to get the return value (the value of the main function return in the. exe source) after running the. exe, and then use an IF condition statement to do different things based on the value returned. I found that although the. exe was successfully run, the value returned to me was always 33.

	String path = "F:\\my_work\\code\\c++\\test2\\debug\\test2.exe";
	int a = WinExec (Path.data (), sw_showmaximized);
In other words, I ran the above code successfully, but a was always 33, only to discover later that WinExec could only return whether it was running successfully and the return value was greater than 31, which means the operation was successful. So, in fact, I don't really get the return value of the returned value--main function I want.

For a description of the return value of the WinExec function, refer to the link below: http://www.cnblogs.com/ziwuge/archive/2012/03/12/2392472.html

So I found the system () function, which modifies the code to the following form:

	String path = "F:\\my_work\\code\\c++\\test2\\debug\\test2.exe";
	int a = WinExec (Path.data (), sw_showmaximized);
	int B = System (Path.data ());
At this point, you will be able to correctly get the value of the return of the main function in the. exe source.

For the difference between system and winexec, you can refer to the links below: http://shashanzhao.com/archives/642.html

In the link, the 4th statement is as follows:

System is the synchronous calling process, and the calling process does not end and does not return. WinExec is a process call that is not synchronized, and a call immediately returns a value (which represents whether it is running successfully) and does not end with the calling program. As an example:

If you do not understand the command line arguments for the main function argc and argv usage, you can refer to the following link:

Http://zhidao.baidu.com/link?url=sPhn7dpegsBMhMOtkGJSO0_d9OtjW4MfDq9BX_1XEfkqGICqU2YPRNTdUlQaiC-1Fztdh1n5DzazeCU9B2Br3_

Test2 compilation Generation Test2.exe:


Test5 call Test2.exe, divided into the following two cases:

(1) Use the system () function, as shown in the following figure:


Then 0 is displayed on the last line:


(2) using the WinExec () function, the following figure:


Then 33 (the normal return value of the. exe call) appears at the front:





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.