VC Execute DOS command _dos

Source: Internet
Author: User
Tags readfile

Moor a Huanxiangwa dedicated to the group of you small white, old white don't peek, thank you.
Sometimes in the VC to execute the familiar DOS commands can write a lot less code, such as sharing/cancellation of a folder, shut down a service, in the local area network to hide this machine and so on. The following neat direct look at the code:
(We look at the time is best to maximize the window and then see, or blame My code to write the chaos, in fact, I row is very neat.)

///////////////////////////////////////////////////////////////////////////////////////////////////////////////
CString dosstring = "net config Server/hidden:yes";
This command is to hide the machine in your online neighborhood. You can replace it with a DOS command you're interested in. Don't be wrong about that.



The following is a functional part of the use of several times can be encapsulated into a function, of course, the parameters are above the dosstring
#define DOSCMD (LPTSTR) (LPCTSTR) dosstring
Security_attributes sa;
HANDLE Hread,hwrite;
sa.nlength = sizeof (security_attributes);
Sa.lpsecuritydescriptor = NULL;
Sa.binherithandle = TRUE;
if (! CreatePipe (&hread,&hwrite,&sa,0))
{
return FALSE;
MessageBox ("Fail creat Pipe");
}
Startupinfo si;
Process_information Pi;
SI.CB = sizeof (STARTUPINFO);
Getstartupinfo (&SI);
Si.hstderror = Hwrite;
Si.hstdoutput = Hwrite;
Si.wshowwindow = Sw_hide;
Si.dwflags = Startf_useshowwindow | Startf_usestdhandles;
if (! CreateProcess (NULL,DOSCMD,NULL,NULL,TRUE,NULL,NULL,NULL,&SI,&PI))
{
return FALSE;
MessageBox ("fail");
}
CloseHandle (Hwrite);
Char buffer[4096] = {0};//decides cache size not too wasteful
DWORD Bytesread;
while (true)
{
if (ReadFile (hread,buffer,4095,&bytesread,null) = NULL)//buffer is the return result after execution, you can choose how you want to handle it.
{break;}
Sleep (200);
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////// /
The code is not long, directly handcuffed to your program well.

At the top, I wrapped him up in a function.
Delay can be used to fully receive ping timeout

void Crundlg::runcmd (CString cmd, char *buf, unsigned int bufsize, unsigned int delay)
{
Security_attributes sa;
HANDLE Hread, Hwrite;
sa.nlength = sizeof (security_attributes);
Sa.lpsecuritydescriptor = NULL;
Sa.binherithandle = TRUE;

if (! CreatePipe (&hread, &hwrite, &sa, 0)) {
MessageBox ("Create pipe Fail");
Return
}

Startupinfo si;
Process_information Pi;
SI.CB = sizeof (STARTUPINFO);
Getstartupinfo (&SI);
Si.hstderror = Hwrite;
Si.hstdoutput = Hwrite;
Si.wshowwindow = Sw_hide;
Si.dwflags = Startf_useshowwindow | Startf_usestdhandles;
if (! CreateProcess (null, (LPTSTR) (LPCTSTR) cmd, NULL, NULL, TRUE, NULL, NULL, NULL, &si,π)) {
MessageBox ("Create process fail");
Return
}
CloseHandle (Hwrite);
Sleep (delay);
DWORD Bytesread;
while (TRUE) {
memset (buf, 0, bufsize);
/* If The ReadFile function succeeds, the return value is nonzero. */
if (ReadFile (Hread, buf, bufsize, &bytesread, NULL)!= null) {
Break
}
Sleep (200);
}
CloseHandle (Hread);
Return
}

void Crundlg::onexec ()
{
Todo:add your control notification handler code here
CString cmd = "ipconfig";
Char *buf;
unsigned int bufsize = 1024;
unsigned int delay = 0;
if ((buf = (char *) malloc (bufsize)) = = NULL) {
Return
}

memset (buf, 0, bufsize);
Runcmd (cmd, buf, bufsize, delay);
M_opt. Format ("%s", buf);
UpdateData (FALSE);
Free (BUF);
}

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.