WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Networks - Servers
plus
plus
plus
plus
plus
plus
plus

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Connected to network? Is Network Alive?

Keywords:    is network alive IsNetworkAlive

To interrogate if you are connected to a network:

I took a romp through the MSDN Platform SDK documentation this afternoon. A little searching was done and here's what I learned:

The following URL points to some MSDN documentation that discusses the IsNetworkAlive() function. It is built into Win2K & WinME. On WinNT and Win9x, you need to have IE v5.x installed for this API function to exist.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/synchro/1sensref_0j8l.asp
Anyway, this API function should be simple enough to call through DllCall() inside of a WinBatch script.

Here's some test code that I banged out on the keyboard:

; IsNetworkAlive.wbt
; 32-bit

Title01 = 'Test IsNetworkAlive()'
FlagsBuf = BinaryAlloc(4)
BinaryPoke4(FlagsBuf,0,0)

Result = DllCall('SENSAPI.DLL',long:'IsNetworkAlive',lpbinary:FlagsBuf)

DllRC = DllLastError()
Flags = BinaryPeek(FlagsBuf,0)
BinaryFree(FlagsBuf)
FlagsBuf = 0
MsgText = StrCat('Result = ',Result,@CRLF,'RC = ',DllRC,@CRLF,'Flags = ',Flags)

Message(Title01,MsgText)
exit
From the SENSAPI.H header file in the Platform SDK, here are some constant definitions that are used to decode the flags bit mask value:
#define NETWORK_ALIVE_LAN 0x00000001
#define NETWORK_ALIVE_WAN 0x00000002
#define NETWORK_ALIVE_AOL 0x00000004
I ran the code above on my development laptop system and it appeared to give valid results. I unplugged the CAT5 cable from my LAN adapter and the Result value was zero. I plugged the CAT5 cable back into the LAN adapter and the Result value was one; also, the flags value was one.

One thing to watch out for is that there may be certain aspects of your system configuration that could cause this function to return misleading results. For example, I have VMware installed on my laptop so that I can run other operating systems as guests inside of virtual machines. One of the features of VMware is support for a virtual LAN environment so that the guest operating sytems can communicate with each other and with the host system [e.g. my laptop] through normal network protocols like TCP/IP and IPX. This host-only networking feature is supported via a virtual LAN adapter that exists in the device configuration of the host system. Thus, unplugging the CAT5 cable from the physical network adapter but not disabling the host-only virtual LAN adapter resulted in IsNetworkAlive() returning a value of one. I had to deliberately disable the host-only virtual LAN adapter before I could get IsNetworkAlive() to accurately report that there were no network adapters active on my system.

The sample script could easily be converted into a UDF for convenient use.

The usage of the IsNetworkAlive() API function could also be integrated into one of the existing network related extenders. The simple use LoadLibrary() and GetProcAddress() would allow for this API function to be used if the required SENSAPI.DLL file is installed on the system. Absence of the SENSAPI.DLL file could result in an extender function returning a controlled error message indicating that the desired API function is not available on the system.

The Synchronization Manager also has a feature called the System Event Notification Service. This service allows for an application to register with the service so that certain events related to changes in network connectivity can be sent to an application. It is feasible to have an extender function create an additional thread that registers to receive these events and then sits in the background there after. When one of these events are received, the thread could store the event information in a global variable in the extender. The script that called the extender function could then poll the global variable contents periodically via another extender function to check to see if any events have been received. Also, a blocking-mode version of the extender function could be provided to cause the extender function to not return until one of the desired events has been received.


Article ID:   W15184
File Created: 2002:09:05:13:50:24
Last Updated: 2002:09:05:13:50:24