MFC calls an external EXE file

Source: Internet
Author: User

Article address: http://hi.baidu.com/%C0%D6%B5%C4%E3%B0%E2%EA/blog/item/287895f1a6f80b3dbc310910.html

When developing an MFC application, if you want to implement a specific function, of course, the general practice is to add a dialog box to process this function and add a class to the dialog box, you can directly drop the dialog box when you need to call the function. However, if that function is implemented in an external EXE and there is no implementation code for that EXE file, what should I do? Directly call the external EXE file ..

Method 1 (process mode)
Startupinfo Si;
Process_information PI;
Zeromemory (& Si, sizeof (SI ));
Si. cb = sizeof (SI );
Zeromemory (& Pi, sizeof (PI ));
If (! CreateProcess (null, "E; \ test.exe", null, null, false, create_no_window, null, null, & Si, & PI ))
{
MessageBox ("error", "prompt", mb_ OK | mb_iconinformation );
}
Waitforsingleobject (PI. hprocess, infinite );
Closehandle (PI. hprocess );
Closehandle (PI. hthread );
Method 2 (thread mode)
Winexec ("test. EXE", sw_hide );

Q: How to open an application?
ShellExecute (this-> m_hwnd, "open", "calc.exe", "", "", sw_show );
Or
ShellExecute (this-> m_hwnd, "open", "notepad.exe ",
"C: \ mylog. log", "", sw_show );
As you can see, I haven't passed the full path of the programs.
 
Q: How do I open a file related to the same system program?
ShellExecute (this-> m_hwnd, "open ",
"C: \ abc.txt", "", "", sw_show );
 
Q: How to open a webpage?
ShellExecute (this-> m_hwnd, "open ",
"Http://hi.baidu.com/%C0%D6%B5%C4%E3%B0%E2%EA/home", "", "", sw_show );
 
Q: How to activate related programs and send emails?
ShellExecute (this-> m_hwnd, "open ",
"Mailto: gongziya@gmail.com", "", "", sw_show );
 
Q: How do I print documents with a system printer?
ShellExecute (this-> m_hwnd, "print ",
"C: \ abc.txt", "", "", sw_hide );
 
Q: How can I use the system search function to find a specified file?
ShellExecute (m_hwnd, "find", "d: \ Nish ",
Null, null, sw_show );

**************************************** **************************************

The two methods above are the Netease blog "MFC call EXE" I posted.

**************************************** **************************************

Write a test.exe file for testing. The source code is as follows:

# Include <iostream>

Using namespace STD;

Int main ()

{

Cout <"\ nhello world" <Endl;

Return 0;

}

Test procedure:

# Include <iostream>

# Include <windows. h>

Using namespace STD;

Int main ()

{

Winexec ("test. EXE", sw_hide );

Return 0;

}

The call is indeed effective. Now there is an exegame box.exe (push box game), which is placed in the box folder of the edisk. Next, create an MFC dialog box-based application. Double-click the OK button to go to the message processing function onok. Edit the Code as follows:

Void ccreate_process_demodlg: onok ()

{

Startupinfo Si;

Process_information PI;

Zeromemory (& Si, sizeof (SI ));

Si. cb = sizeof (SI );

Zeromemory (& Pi, sizeof (PI ));

If (! CreateProcess (text ("E: \ Box \ box.exe"), null, false, 0, null, null, & Si, & PI ))

{

MessageBox ("not found", "title", mb_ OK );

}

Waitforsingleobject (PI. hprocess, infinite );

Closehandle (PI. hprocess );

Closehandle (PI. hthread );

: MessageBox (null, "back to the main program", "normal return", mb_ OK );

Cdialog: onok ();

}

Open the external exe program, and exit and return to the main program for further execution.

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.