MFC 调用系统ping工具检测网络

转载自: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界面。

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值