VC in the call cmd command four ways _cmd

Source: Internet
Author: User
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.

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.