转载自:http://blog.csdn.net/jiangqin115/article/details/44756905,实际测试后记录
DWORD CChildView::WinExecAndWait32(LPCTSTR lpszAppPath,
LPCTSTR lpParameters,
LPCTSTR lpszDirectory,
DWORD dwMilliseconds,
BOOL bIsWait,
int nShow)
{
SHELLEXECUTEINFO ShExecInfo = { 0 };
ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
ShExecInfo.hwnd = NULL;
ShExecInfo.lpVerb = NULL;
ShExecInfo.lpFile = lpszAppPath;
ShExecInfo.lpParameters = lpParameters;
ShExecInfo.lpDirectory = lpszDirectory;
ShExecInfo.nShow = nShow; //SW_SHOW
ShExecInfo.hInstApp = NULL;
ShellExecuteEx(&ShExecInfo);
if (ShExecInfo.hProcess == NULL)
return 1;
if (!bIsWait)
return 0;
if (WaitForSingleObject(ShExecInfo.hProcess, dwMilliseconds) == WAIT_TIMEOUT)
{
TerminateProcess(ShExecInfo.hProcess, 0);
return 1;
}
DWORD dwExitCode;
BOOL bOK = GetExitCodeProcess(ShExecInfo.hProcess, &dwExitCode);
ASSERT(bOK);
return dwExitCode;
}
void CChildView::OnButtonLantest()
{
DWORD n = WinExecAndWait32(_T("ping.exe"), SysProp.ServeIP + _T(" -n 2"), NULL, 10000, TRUE,SW_SHOW);
if (n == 0)
{
MessageBox(_T("通信正常"));
return;
}
else
{
CString sNetWorkConnect;
sNetWorkConnect=_T("Ping:")+ SysProp.ServeIP+_T("失败");
MessageBox(sNetWorkConnect,_T("错误"),MB_ICONERROR);
return;
}
}
注意:
SysProp.ServeIP为自定义要ping的IP。
WinExecAndWait32函数中的nShow参数决定是否显示ping的cmd界面。