【C++】检测网络是否可用

static bool checkIsNetwork()
{
	Sleep(100);
	CoInitialize(NULL);
	//  通过NLA接口获取网络状态    
	IUnknown *pUnknown = NULL;
	BOOL   bOnline = false;
	//是否在线      
	HRESULT Result = CoCreateInstance(CLSID_NetworkListManager, NULL, CLSCTX_ALL, IID_IUnknown, (void **)&pUnknown);
	if (SUCCEEDED(Result))    {
		INetworkListManager *pNetworkListManager = NULL;
		if (pUnknown)            Result = pUnknown->QueryInterface(IID_INetworkListManager, (void **)&pNetworkListManager);
		if (SUCCEEDED(Result))        {
			VARIANT_BOOL IsConnect = VARIANT_FALSE;
			if (pNetworkListManager)            Result = pNetworkListManager->get_IsConnectedToInternet(&IsConnect);
			if (SUCCEEDED(Result))            {
				bOnline = (IsConnect == VARIANT_TRUE) ? true : false;
			}
		}        if (pNetworkListManager)            pNetworkListManager->Release();
	}    if (pUnknown)        pUnknown->Release();
	CoUninitialize();

	return bOnline;
}

int main(){
	while (!checkIsNetwork());
	return 0;
}
发布了40 篇原创文章 · 获赞 5 · 访问量 3683
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 技术黑板 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览