Jump to Real's How-to Main page

Detect network and/or internet connectivity state

[Local function declaration]
FUNCTION boolean IsNetworkAlive(ref int flags) LIBRARY "sensapi.dll"


[Powerscript]
// The computer has one or more LAN cards that are active.
CONSTANT integer NETWORK_ALIVE_LAN = 1;
// The computer has one or more active RAS connections (internet).
CONSTANT integer NETWORK_ALIVE_WAN = 2;
// Win9x. The computer is connected to the America Online network.
CONSTANT integer NETWORK_ALIVE_AOL = 4;

OleObject wsh
int networkState, k

// to do the "bitwise AND" later...
wsh = CREATE OleObject
wsh.ConnectToNewObject( "MSScriptControl.ScriptControl" )
wsh.language = "vbscript"


IF IsNetworkAlive(networkState) THEN
   // check if a network card is active
   k = integer(wsh.Eval( string(networkState) + &
                          " AND " + &
                         string(NETWORK_ALIVE_LAN)))
   IF k = NETWORK_ALIVE_LAN THEN
        MessageBox("","network is ON")
   ELSE
        MessageBox("","network is OFF")
   END IF  

   // check if internet connection is active
   k = integer(wsh.Eval( string(networkState) + &
                          " AND " + &
                         string(NETWORK_ALIVE_WAN)))
   IF k = NETWORK_ALIVE_WAN THEN
        MessageBox("","internet is ON")
   ELSE
        MessageBox("","internet is OFF")
   END IF  
ELSE
   MessageBox("","no network ?!?")
END IF
See this related HowTo
If you find this article useful, consider making a small donation
to show your support for this Web site and its content.

Written and compiled by Réal Gagnon ©1998-2005
[ home ]