getting a notification when the network cable is plugged or unplugged

how can i make a program that give me a message when the network cable is plugged or unplugged from the network card?
Who is Participating?
aParserConnect With a Mentor Commented:
The code suggested in PAQ http:Q_20747385.html gives you an event-driven solution instead of polling periodically.
I'm certain some API are available for taht BUT another way to do it is with WMI. This will allow you to query information about the computer by using an SQL-like language. This can be used for querying harddrives, network cards, etc....

Try this :

Sub ListNetworkAdapter( strComputer )
    Dim objWMIService, colItems

      Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
      Set colItems = objWMIService.ExecQuery("Select * from Win32_NetworkAdapter",,48)
      For Each objItem in colItems
          Debug.Print "AdapterType: " & objItem.AdapterType
          Debug.Print "AutoSense: " & objItem.AutoSense
          Debug.Print "Availability: " & objItem.Availability
          Debug.Print "Caption: " & objItem.Caption
          Debug.Print "ConfigManagerErrorCode: " & objItem.ConfigManagerErrorCode
          Debug.Print "ConfigManagerUserConfig: " & objItem.ConfigManagerUserConfig
          Debug.Print "CreationClassName: " & objItem.CreationClassName
          Debug.Print "Description: " & objItem.Description
          Debug.Print "DeviceID: " & objItem.DeviceID
          Debug.Print "ErrorCleared: " & objItem.ErrorCleared
          Debug.Print "ErrorDescription: " & objItem.ErrorDescription
          Debug.Print "Index: " & objItem.Index
          Debug.Print "InstallDate: " & objItem.InstallDate
          Debug.Print "Installed: " & objItem.Installed
          Debug.Print "LastErrorCode: " & objItem.LastErrorCode
          Debug.Print "MACAddress: " & objItem.MACAddress
          Debug.Print "Manufacturer: " & objItem.Manufacturer
          Debug.Print "MaxNumberControlled: " & objItem.MaxNumberControlled
          Debug.Print "MaxSpeed: " & objItem.MaxSpeed
          Debug.Print "Name: " & objItem.Name
          Debug.Print "NetworkAddresses: " & objItem.NetworkAddresses
          Debug.Print "PermanentAddress: " & objItem.PermanentAddress
          Debug.Print "PNPDeviceID: " & objItem.PNPDeviceID
          Debug.Print "PowerManagementCapabilities: " & objItem.PowerManagementCapabilities
          Debug.Print "PowerManagementSupported: " & objItem.PowerManagementSupported
          Debug.Print "ProductName: " & objItem.ProductName
          Debug.Print "ServiceName: " & objItem.ServiceName
          Debug.Print "Speed: " & objItem.Speed
          Debug.Print "Status: " & objItem.Status
          Debug.Print "StatusInfo: " & objItem.StatusInfo
          Debug.Print "SystemCreationClassName: " & objItem.SystemCreationClassName
          Debug.Print "SystemName: " & objItem.SystemName
          Debug.Print "TimeOfLastReset: " & objItem.TimeOfLastReset
End Sub

Here is the code that checks if you are connected on network

Private Declare Function IsNetworkAlive Lib "SENSAPI.DLL" (ByRef lpdwFlags As Long) As Long
Private Sub Form_Load()
    'KPD-Team 2001
    Dim Ret As Long
    If IsNetworkAlive(Ret) = 0 Then
        MsgBox "The local system is not connected to a network!"
        MsgBox "The local system is connected to a " + IIf(Ret = NETWORK_ALIVE_AOL, "AOL", IIf(Ret = NETWORK_ALIVE_LAN, "LAN", "WAN")) + " network!"
    End If
End Sub

Make function from this, addd timer, and call this function, each 5 seconds, for example
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

aftAuthor Commented:
thanx alot
aftAuthor Commented:

i tried the link u sent to me
when i remove the network cable or put it. i get the same message like shown below. how can i differentiate between removing or plugging the network cable?

first item displayname: Local Area Connection
first item path:

Index of image in system imagelist: 259

first item displayname: Local Area Connection
first item path:

This is sure great stuff....but how do you get the "actual" status of your network when you startup the program ? I think you'll have a combination of the techniques described above....
aftAuthor Commented:
IsNetworkAlive  sometimes working and other times not working what is the problem?
Not point is :  Why include SHCNE and its subclassing in your program ( and all the stability problems that might ocuur with that in vb-programs)  , if you still need the IsNetworkAlive function to check for the initial status ?

I think SHCNE is just overkill for what you're asking.... but still, SHCNE is a great way to perform some other monitoring tasks such as file and folder monitoring ....

To resume, SHCNE is great if you want to monitor for things where the previous status doesn't if you want to know when a printer is added is doesn't really matter how many printer were already added, etc...

But hey...that is the greatness of programming, mostly of the time there are several techniques available for a problem.  

Aft, I think Dextrosoft is right: a combination of techniques may be the best approach.  I hate polling, so I went looking for an event I could hook, rather than check the status of the network every 5 seconds. And that's my answer as to why to include SHCNE: I hate polling.  Having gotten the initial state, I'd rather wait for the event than to poll.

I don't know of a way to distinguish between plugging and unplugging.  I would check the initial status and then hook the SHCNE_UPDATEITEM event.  Once it fires, I'd recheck the network connection and let that tell me whether I had just been plugged in or unplugged.
aftAuthor Commented:
aParser but how can i check if the cable is plugged or unplugged if i have a problem in IsNetworkAlive.  sometimes it works and other times not working what is the problem?
aftAuthor Commented:
DextroSoft ur code can not give me if the network cable is plugged or not. what fields contain this information?
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.