Wednesday, December 22, 2010

Get Network status

Tip -
Get Network status Using System Event Notification Service (SENS) and Windows Management Instrumentation(WMI)

Details- 

SENS or WMI can be used in applications to get the status of a local system, whether it is connected or disconnected to/from a network( LAN/WAN).
- Get Network status
- Notify you when local system is connected/disconnected  to/from a network

SENS APIs will not work in crossover cable network, where WMI will work both in LAN/WAN and cross-cable network.
   (crossover cable is a type of Ethernet cable used to connect computing devices together directly where they would normally be connected via a network switchhub or router,
     such as directly connecting two personal computers via their network interface controllers.)

1.SENSE
The System Event Notification Service (SENS) now provides  a unique set of connectivity functions and notifications  in the operating system, creating a uniform connectivity and notification interface for applications.
          - Get Network status
   The IsNetworkAlive function determines whether or not a local system is connected to a network, and identifies the type of network connection.

- Notify you when local system is connected/disconnected  to/from a network
   If applications want to be notified when local system is connected or disconnected to/from a network, we can use  ISensNetwork interface of SENS.
   The following methods of ISensNetwork interface will be notified when local system is connected or disconnected  to/from a network.
   ConnectionMadeNoQOCInfo ()
   ConnectionLost().

2. WMI
WMI(Windows Management Instrumentation) is a new management technology allowing scripts to monitor and control managed resources throughout the network.
          - Get Network status
The IWbemServices interface is used by clients and providers to access WMI services.
The interface is implemented by WMI and WMI providers, and is the primary WMI interface.
  We can use  IWbemServices::ExecQuery() to get the current network status.
  The following query string can be used to get the current network status
             SELECT NetConnectionStatus FROM Win32_NetworkAdapter WHERE NetConnectionID  = 'Local Area Connection’

- Notify you when local system is connected/disconnected  to/from a network
IWbemObjectSink::Indicate() method  of WMI will automatically invoked whenever network connection is established or dropped.
IWbemServices::ExecNotificationQueryAsync() method on WMI will be called for subscribing the network connection and drop notifications.
The following query string can be used to get the notifications
            SELECT * FROM MSNdis_StatusMediaConnect
SELECT * FROM MSNdis_StatusMediaDisconnect

[Note:]
WMI is slow to query data and if you are trying to use it during start up it can delay you starting as the WMI service takes a long time to come up.
However, WMI information is richer, as in you need to sometimes make several API calls to get the same amount, some information is a lot easier to get at and the filtering syntax can dramatically reduce the amount code you have to write.
If speed isn't a massive issue, it is better to lean towards WMI.

You can check the WMI query using wbemtest.exe tool. (http://technet.microsoft.com/en-us/library/cc785775(WS.10).aspx)
Click Start, click Run, and then type wbemtest.exe.

 
Reference   -


Examples are available in

Posted By :Ajay Kumar S.

No comments:

Post a Comment