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);
}