PS:用子线程,并对输出的字符串进行拆解,效果更逼真.
直接代码附解释:
void CShellPingDlg::OnBnClickedBtnPing() // 窗体上按钮的BN_CLICKED事件
{
CString host,addr;
GetDlgItemTextA(IDC_TXT_IP, host);//获取窗体CEdit空间 IDC_TXT_IP 的文本
addr = "ping " + host; // 外部程序 ping.exe 和 参数,如 ping csdn.netSECURITY_ATTRIBUTES sa;
HANDLE hRead, hWrite;sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.lpSecurityDescriptor = NULL;
sa.bInheritHandle = TRUE;//创建匿名管道
if(!CreatePipe(&hRead, &hWrite, &sa, 0))
{
MessageBox("Error: CreatePipe");
return;
}STARTUPINFO si;
PROCESS_INFORMATION pi;
si.cb = sizeof(STARTUPINFO);
GetStartupInfo(&si);
si.hStdError = hWrite;
si.hStdOutput = hWrite;
si.wShowWindow = SW_HIDE;