getting a notification when the network cable is plugged or unplugged

Posted on 2004-09-07
Last Modified: 2007-12-19
how can i make a program that give me a message when the network cable is plugged or unplugged from the network card?
Question by:aft
  • 5
  • 3
  • 2
  • +1

Expert Comment

ID: 12004030
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


Expert Comment

ID: 12004385
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

Accepted Solution

aParser earned 500 total points
ID: 12007597
The code suggested in PAQ http:Q_20747385.html gives you an event-driven solution instead of polling periodically.
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.


Author Comment

ID: 12013471
thanx alot

Author Comment

ID: 12013519

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:


Expert Comment

ID: 12013765
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....

Author Comment

ID: 12013799
IsNetworkAlive  sometimes working and other times not working what is the problem?

Expert Comment

ID: 12013872
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.  


Expert Comment

ID: 12015915
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.

Author Comment

ID: 12033024
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?

Author Comment

ID: 12033048
DextroSoft ur code can not give me if the network cable is plugged or not. what fields contain this information?

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
change vba from autofit to 13.5 width? 4 29
VBA error replacing data 6 40
VBA Shell can't Find Word document 11 112
Collapse and expand table in Word 2010 2 34
Introduction While answering a recent question ( in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question