Go Premium for a chance to win a PS4. Enter to Win


getting a notification when the network cable is plugged or unplugged

Posted on 2004-09-07
Medium Priority
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
    'URL: http://www.allapi.net/
    'E-Mail: KPDTeam@Allapi.net
    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 2000 total points
ID: 12007597
The code suggested in PAQ http:Q_20747385.html gives you an event-driven solution instead of polling periodically.

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.


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 really...my 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 matter..eg. 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

916 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