MFC检测网络连接和ping IP地址

[cpp]  view plain  copy
  1. /****************************************************************** 
  2. * 函数介绍:运行程序 
  3. * 输入参数: 
  4. * 输出参数: 
  5. * 返回值 : 
  6. *******************************************************************/  
  7. DWORD CCommonFun::WinExecAndWait32(LPCTSTR lpszAppPath,  
  8. LPCTSTR lpParameters,  
  9. LPCTSTR lpszDirectory,  
  10. DWORD dwMilliseconds,  
  11. BOOL bIsWait,  
  12. int nShow)  
  13. {  
  14. SHELLEXECUTEINFO ShExecInfo = {0};  
  15. ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);  
  16. ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;  
  17. ShExecInfo.hwnd = NULL;  
  18. ShExecInfo.lpVerb = NULL;  
  19. ShExecInfo.lpFile = lpszAppPath;  
  20. ShExecInfo.lpParameters = lpParameters;  
  21. ShExecInfo.lpDirectory = lpszDirectory;  
  22. ShExecInfo.nShow = nShow; //SW_SHOW  
  23. ShExecInfo.hInstApp = NULL;  
  24. ShellExecuteEx(&ShExecInfo);  
  25.   
  26. if ( ShExecInfo.hProcess == NULL)  
  27. return 1;  
  28.   
  29. if ( !bIsWait )  
  30. return 0;  
  31.   
  32. if (WaitForSingleObject(ShExecInfo.hProcess, dwMilliseconds) == WAIT_TIMEOUT)  
  33. {  
  34. TerminateProcess(ShExecInfo.hProcess, 0);  
  35. return 1;  
  36. }  
  37.   
  38. DWORD dwExitCode;  
  39. BOOL bOK = GetExitCodeProcess(ShExecInfo.hProcess, &dwExitCode);  
  40. ASSERT(bOK);  
  41.   
  42. return dwExitCode;  
  43. }  


[cpp]  view plain  copy
  1. #include <Sensapi.h>  
  2. #pragma comment(lib, "Sensapi.lib")  
  3. BOOL CheckNetIsOK(const CString sUpdateIP)  
  4. {  
  5.     //Judge Network is Connected  
  6.     int nCount = 1;  
  7.     do   
  8.     {  
  9.         DWORD dw;  
  10.         if( IsNetworkAlive(&dw))  
  11.         {  
  12.             break;  
  13.         }  
  14.         else  
  15.         {  
  16.             Sleep(10000);  
  17.             CString sNetWorkConnect;  
  18.             sNetWorkConnect.Format("第%d次网络未成功连接, 10秒后重试", nCount);  
  19.             m_recvCtrl.SetWindowText(sNetWorkConnect);  
  20.             nCount++ ;  
  21.         }  
  22.     } while (nCount <4);  
  23.   
  24.     if (nCount == 4)  
  25.     {  
  26.         m_recvCtrl.SetWindowText("网络连接失败, 共检测40秒");  
  27.         return FALSE;  
  28.     }  
  29.   
  30.     DWORD n = WinExecAndWait32(_T("ping.exe"), sUpdateIP + " -n 2"/*sCmdPara*/, NULL, 10000);  
  31.     if (n == 0)  
  32.     {  
  33.         return TRUE;  
  34.     }  
  35.     else  
  36.     {  
  37.         CString sNetWorkConnect;  
  38.         sNetWorkConnect.Format("网络连接正常, Ping:%s 失败, 请检测此IP对应的服务器是否正常工作", sUpdateIP);  
  39.         m_recvCtrl.SetWindowText(sNetWorkConnect);  
  40.         return FALSE;  
  41.     }  
  42. }  
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值