1. System method: Prototype:
int __cdecl System (const char *);
For example:
System ("ipconfig");
2. WinExec method: The WinExec method has one more window parameter than the system method:
Prototype:
UINT winexec (
lpcstr lpcmdline,
UINT ucmdshow
);
For example:
WinExec ("ipconfig", sw_show);
Parameters can be: sw_show,,sw_showmaximized,sw_showminimized and so on, the overall usage is similar.
3. ShellExecute method: This method is mainly used for the operation of Open,edit,find and other methods:
Prototype:
HINSTANCE ShellExecute (
hwnd hwnd,
LPCTSTR lpoperation,
lpctstr lpfile,
lpctstr lpparameters,
LPCTSTR lpdirectory,
INT nshowcmd
);
For example:
ShellExecute (NULL, "open", "Abc.xls", null,null,sw_hide);//Open Default path Abc.xls file
4. Callable method of invocation: This method is more complicated by creating a new process to simulate the cmd command line, rendering the write command line and echoing through the pipe.
For example:
void Ctestmfcdlg::onok () {//Todo:add extra validation here security_attributes sa;
HANDLE Hread,hwrite;
sa.nlength = sizeof (security_attributes);
Sa.lpsecuritydescriptor = NULL;
Sa.binherithandle = TRUE; if (!
CreatePipe (&hread,&hwrite,&sa,0)) {MessageBox ("CreatePipe Failed");
Return
} startupinfo si;
Process_information Pi;
ZeroMemory (&si,sizeof (startupinfo));
SI.CB = sizeof (STARTUPINFO);
Getstartupinfo (&SI);
Si.hstderror = Hwrite;
Si.hstdoutput = Hwrite;
Si.wshowwindow = Sw_hide; Si.dwflags = Startf_usestdhandles |
Startf_useshowwindow;
Char cmdline[200];
CString Tmp,stredit2;
GetDlgItemText (IDC_EDIT_CMD,STREDIT2); Tmp.
Format ("cmd/c%s", stredit2);
sprintf (CmdLine, "%s", TMP); if (! CreateProcess (NULL,CMDLINE,NULL,NULL,TRUE,NULL,NULL,NULL,&SI,&PI)) {MessageBox ("CreateProcess failed!
");
Return } CloseHandle (HwriTE);
Char buffer[4096] = {0};
CString Stroutput;
DWORD Bytesread;
while (1) {if (NULL = ReadFile (hread,buffer,4095,&bytesread,null)) {break;
} Stroutput + = buffer;
Setdlgitemtext (Idc_edit_text,stroutput);
UpdateWindow ();
Sleep (1000);
} closehandle (Hread);
}
The above is in an MFC click on the OnOK button, the Idc_edit_cmd edit box in the command to execute, and the 1 seconds in the cmd command echo promised to the Idc_edit_text edit box implementation.