Solved

getting a notification when the network cable is plugged or unplugged

Posted on 2004-09-07
11
597 Views
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?
0
Comment
Question by:aft
  • 5
  • 3
  • 2
  • +1
11 Comments
 

Expert Comment

by:DextroSoft
Comment Utility
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
      Next
End Sub

0
 
LVL 8

Expert Comment

by:mladenovicz
Comment Utility
Here is the code that checks if you are connected on network

Const NETWORK_ALIVE_AOL = &H4
Const NETWORK_ALIVE_LAN = &H1
Const NETWORK_ALIVE_WAN = &H2
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!"
    Else
        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
0
 
LVL 3

Accepted Solution

by:
aParser earned 500 total points
Comment Utility
The code suggested in PAQ http:Q_20747385.html gives you an event-driven solution instead of polling periodically.

http://www.mvps.org/btmtz/shnotify/
0
 

Author Comment

by:aft
Comment Utility
thanx alot
0
 

Author Comment

by:aft
Comment Utility
aParser

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?

SHCNE_UPDATEITEM
first item displayname: Local Area Connection
first item path:

SHCNE_UPDATEIMAGE
Index of image in system imagelist: 259

SHCNE_UPDATEITEM
first item displayname: Local Area Connection
first item path:

0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Expert Comment

by:DextroSoft
Comment Utility
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....
0
 

Author Comment

by:aft
Comment Utility
IsNetworkAlive  sometimes working and other times not working what is the problem?
0
 

Expert Comment

by:DextroSoft
Comment Utility
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.  

0
 
LVL 3

Expert Comment

by:aParser
Comment Utility
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.
0
 

Author Comment

by:aft
Comment Utility
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?
0
 

Author Comment

by:aft
Comment Utility
DextroSoft ur code can not give me if the network cable is plugged or not. what fields contain this information?
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

763 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now