面对现实,超越自己
逆水行舟,不进则退
posts - 269,comments - 32,trackbacks - 0
代码示例:

#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 = CCommonFun::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;
    }
}

posted on 2013-03-01 15:51 王海光 阅读(7339) 评论(5)  编辑 收藏 引用 所属分类: MFC

FeedBack:
# re: MFC检测网络连接和ping IP地址
2014-10-21 10:26 | 罗弘阳
你好,按上面的代码我编译的时候提示这个:
'CCommonfun' : is not a class or namespace name,

请问这个CCommonfun怎么来的?  回复  更多评论
  
# re: MFC检测网络连接和ping IP地址[未登录]
2014-10-23 18:04 | 王海光
@罗弘阳
自己写的一个公共函数类,上面代码用到的函数:
/******************************************************************
* 函数介绍:运行程序
* 输入参数:
* 输出参数:
* 返回值 :
*******************************************************************/
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;
}
  回复  更多评论
  
# re: MFC检测网络连接和ping IP地址
2014-10-29 10:27 | 罗弘阳
@王海光
谢谢!可以了。
  回复  更多评论
  
# re: MFC检测网络连接和ping IP地址
2015-12-23 16:24 | mfc新手
刚好需要这个,谢谢楼主了。  回复  更多评论
  
# re: MFC检测网络连接和ping IP地址
2016-11-10 15:41 | SherlockLee
通过这种方式无法实现对ping结果的侦测,例如ping一个地址X.X.X.X无法知道是可以正常回复,还是请求超时。  回复  更多评论
  

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理