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!
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;
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments