当网络连接可用时如何启动我的应用程序?

pra*_*rma 2 networking installshield visual-c++

我正在使用 installshield 安装服务。此服务配置为在用户重新启动系统或登录时启动。现在我需要在 PC 中的网络连接可用时启动该应用程序。有什么办法可以做到这一点吗??

谢谢!

Joc*_*ach 5

要检查网络连接,您可以调用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; 
} 
Run Code Online (Sandbox Code Playgroud)

从 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; 
} 
Run Code Online (Sandbox Code Playgroud)