prashant_sharma
prashant_sharma

Reputation: 63

How can i start my application when network connection is available?

I am installing a service using installshield. This service is configured to get started when the user restarts the system or logs in. Now i need to start the app when network connection is available in the PC. Is there any way in which this can be done??

Thanks!

Upvotes: 3

Views: 4930

Answers (3)

user240141
user240141

Reputation:

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

Upvotes: 0

Jochen Kalmbach
Jochen Kalmbach

Reputation: 3684

To check for network connection, you can call IsNetworkAlive. Here is an example:

#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; 
} 

Starting with Vista, you can also take a look at the Network list manager. This will give you a more detailed answer:

You cann call the method INetworkListManager::GetConnectivity to check for network connectivity:

#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; 
} 

Upvotes: 6

Hidden
Hidden

Reputation: 3628

You can try to ping a website for example google which is 99,99% Online and react to a boolean value.

Upvotes: 0

Related Questions