Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 612
  • Last Modified:

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?
0
aft
Asked:
aft
  • 5
  • 3
  • 2
  • +1
1 Solution
 
DextroSoftCommented:
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
 
mladenoviczCommented:
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
 
aParserCommented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
aftAuthor Commented:
thanx alot
0
 
aftAuthor Commented:
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
 
DextroSoftCommented:
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
 
aftAuthor Commented:
IsNetworkAlive  sometimes working and other times not working what is the problem?
0
 
DextroSoftCommented:
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
 
aParserCommented:
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
 
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?
0
 
aftAuthor Commented:
DextroSoft ur code can not give me if the network cable is plugged or not. what fields contain this information?
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

  • 5
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now