# re: 在vc中执行dos命令--『原著:獾兄』 回复 更多评论
2007-04-16 23:01 by
嗯,无意中看到你这个帖子,
刚好借鉴了2行程序用到我的程序里。
真的很感激。
多谢了。
# re: 在vc中执行dos命令--『原著:獾兄』 回复 更多评论
2007-07-02 15:49 by
頂一個,我把他包成function
delay可以用來完整接收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);
}