码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2015-03-31 09:16:02      阅读:611      评论:0      收藏:0      [点我收藏+]

标签:

/******************************************************************
* 函数介绍:运行程序
* 输入参数:
* 输出参数:
* 返回值 :
*******************************************************************/
DWORD CCommonFun::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;
}


#include <Sensapi.h>
#pragma comment(lib, "Sensapi.lib")
BOOL CheckNetIsOK(const CString sUpdateIP)
{
    //Judge Network is Connected
    int nCount = 1;
    do 
    {
        DWORD dw;
        if( IsNetworkAlive(&dw))
        {
            break;
        }
        else
        {
            Sleep(10000);
            CString sNetWorkConnect;
            sNetWorkConnect.Format("第%d次网络未成功连接, 10秒后重试", nCount);
            m_recvCtrl.SetWindowText(sNetWorkConnect);
            nCount++ ;
        }
    } while (nCount <4);

    if (nCount == 4)
    {
        m_recvCtrl.SetWindowText("网络连接失败, 共检测40秒");
        return FALSE;
    }

    DWORD n = WinExecAndWait32(_T("ping.exe"), sUpdateIP + " -n 2"/*sCmdPara*/, NULL, 10000);
    if (n == 0)
    {
        return TRUE;
    }
    else
    {
        CString sNetWorkConnect;
        sNetWorkConnect.Format("网络连接正常, Ping:%s 失败, 请检测此IP对应的服务器是否正常工作", sUpdateIP);
        m_recvCtrl.SetWindowText(sNetWorkConnect);
        return FALSE;
    }
}


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

标签:

原文地址:http://blog.csdn.net/jiangqin115/article/details/44756905

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!