我正在使用 installshield 安装服务。此服务配置为在用户重新启动系统或登录时启动。现在我需要在 PC 中的网络连接可用时启动应用程序。有什么办法可以做到这一点??
谢谢!
我正在使用 installshield 安装服务。此服务配置为在用户重新启动系统或登录时启动。现在我需要在 PC 中的网络连接可用时启动应用程序。有什么办法可以做到这一点??
谢谢!
要检查网络连接,您可以调用IsNetworkAlive。这是一个例子:
#include <stdio.h>
#include <tchar.h>
#include <Windows.h>
#include <Sensapi.h>
#pragma comment(lib, "Sensapi.lib")
int _tmain(int argv, char *argc[])
{
DWORD dwSens;
if (IsNetworkAlive(&dwSens) == FALSE)
{
printf("No network connection");
}
else
{
switch(dwSens)
{
case NETWORK_ALIVE_LAN:
printf("LAN connection available");
break;
case NETWORK_ALIVE_WAN:
printf("WAN connection available");
break;
default:
printf("Unknown connection available");
break;
}
}
return 0;
}
从 Vista 开始,您还可以查看网络列表管理器。这将为您提供更详细的答案:
您不能调用方法 INetworkListManager ::GetConnectivity来检查网络连接:
#include <stdio.h>
#include <tchar.h>
#include <Windows.h>
#include <Netlistmgr.h>
#include <atlbase.h>
int _tmain(int argv, char *argc[])
{
printf("\n");
CoInitialize(NULL);
{
CComPtr<INetworkListManager> pNLM;
HRESULT hr = CoCreateInstance(CLSID_NetworkListManager, NULL,
CLSCTX_ALL, __uuidof(INetworkListManager), (LPVOID*)&pNLM);
if (SUCCEEDED(hr))
{
NLM_CONNECTIVITY con = NLM_CONNECTIVITY_DISCONNECTED;
hr = pNLM->GetConnectivity(&con);
if SUCCEEDED(hr)
{
if (con & NLM_CONNECTIVITY_IPV4_INTERNET)
printf("IP4: Internet\n");
if (con & NLM_CONNECTIVITY_IPV4_LOCALNETWORK)
printf("IP4: Local\n");
if (con & NLM_CONNECTIVITY_IPV4_SUBNET)
printf("IP4: Subnet\n");
if (con & NLM_CONNECTIVITY_IPV6_INTERNET)
printf("IP6: Internet\n");
if (con & NLM_CONNECTIVITY_IPV6_LOCALNETWORK)
printf("IP6: Local\n");
if (con & NLM_CONNECTIVITY_IPV6_SUBNET)
printf("IP6: Subnet\n");
}
}
}
CoUninitialize();
return 0;
}
您可以尝试 ping 一个网站,例如 99.99% 在线的 google,并对布尔值做出反应。
Modify and tell your service for the network avilablity every second, as it arrives into your system, do whatever you want in your code.
Also, you can create a exe with a timer contorl and on regular interval poll for internet/network connection