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.