在编程WM平台的网络应用程序时,需要判断当前是否连接了网络,方法有两种:
1. InternetGetConnectedState()
参数具体看MSDN
这种方法不能实时检测网络。
2. 可以实时检测网络
#include <connmgr.h>
#include <connmgr_status.h>
bool m_bhHttp; //表示当前网络是否连接
HRESULT ret = ConnMgrRegisterForStatusChangeNotification(TRUE, g_hWnd);
UINT m_ConnectMsg = RegisterWindowMessage(CONNMGR_STATUS_CHANGE_NOTIFICATION_MSG);
然后在窗口消息回调函数中处理:
//网络状态改变的发出的消息
if (m_ConnectMsg == uMsg)
{
switch (wParam)
{
case CONNMGR_STATUS_CONNECTED: //连接
m_bHttp = true;
break;
case CONNMGR_STATUS_DISCONNECTED: //未链接
m_bHttp = false;
break;
}
}