Detecting if specific network card installed

deesloop

Member
Joined
Jul 27, 2007
Messages
8
Programming Experience
Beginner
I'm writing a util to offer remote users to connect to the office.
Rather than present all connection option - unavailable or not I'd like to disable the options where the hardware is not plugged in.

So if the "Fusion UMTS" modem type is not available then the corresponding 3G connection is greyed out.

How can I programmatically discern if a specific modem (there is also a dialup modem) is currently connected?

Thanks a lot
 
I'm not sure if you need this patch, but its better to download it just in case HERE

With the code below make sure to add System.Management as a Reference
VB.NET:
    Private Function ModemExist(ByVal ModemName As String) As Boolean
        Dim ClassName, Conditions As String
        ClassName = "Win32_POTSModem"
        Conditions = "PNPDeviceID is not Null And Name ='" & ModemName & "'"
        Dim Properties() As String = {"Name", "PNPDeviceID"}
        Dim WmiResults As Management.ManagementObjectCollection = GetWMIResults(ClassName, Conditions, Properties)
        Dim Result As Boolean = WmiResults.Count > 0
        WmiResults.Dispose()
        Return Result
    End Function

    Private Function GetWMIResults(ByVal ClassName As String, ByVal Conditions As String, ByVal Properties() As String) As Management.ManagementObjectCollection
        'Initializes variables
        Dim WMIScope As New System.Management.ManagementScope(Management.ManagementPath.DefaultPath)
        Dim WMIEnumOptions As New System.Management.EnumerationOptions
        WMIEnumOptions.Rewindable = True

        'Creates the query and the searcher
        Dim WMIQuery As New Management.SelectQuery(ClassName, Conditions, Properties)
        Dim WMISearcher As New System.Management.ManagementObjectSearcher(WMIScope, WMIQuery, WMIEnumOptions)

        'Gets the results
        Dim WMIObjCol As System.Management.ManagementObjectCollection = WMISearcher.Get()

        WMISearcher.Dispose()

        Return WMIObjCol
    End Function

To use this code call ModemExists(Name) it returns true if your Name exists.

If you want to know the actual Name properties in order to verify just use this VBScript code

VB.NET:
Set objWMI = GetObject("winmgmts:\\.\root\cimv2")
Set colObjects = objWMI.ExecQuery("Select * From Win32_POTSModem")

For Each objItem in colObjects
	WScript.Echo objItem.Name
Next

Paste the above code into notepad and name it Modems.vbs (Make sure it is a vbs file and not Modem.vbs.txt) Run it by typing CScript Modems.vbs at the command prompt.

Let me know if you have any problems.

Rob
 
Had another thought about this.
Could this further interrogate WMI to see if a NIC is installed with a speed of 100Mb. and if so could it tell the status of that nic - eg connected or not?

Not sure where it should look up as NIC's I presume are different that Modems?
 
The way to do it would be to query the Win32_NetworkAdapterConfiguration for cards where the DefaultIPGateway is not empty and that it's IPAddress is not 0.0.0.0. If both these conditions are true then the card has been assigned an IP address via DHCP and the network cable is installed.

I've tried to modify your code but made a real hash of it.

Any ideas?
 
Changing your VB script to

Set objWMI = GetObject("winmgmts:\\.\root\cimv2")
Set colObjects = objWMI.ExecQuery("Select * From Win32_NetworkAdapterConfiguration")
For Each objItem in colObjects
wScript.Echo objItem.Description
Next

lists all interfaces fine.
However when I add
wscript.Echo objitem.IPAddress
I get a type mismatch error

How can I filter them to show only those with an IP address that is NOT 0.0.0.0 and has a default gateway that is not nul?
 
The class for most of the network adapters is Win32_NetworkAdapter. I didn't mention this originally, but you can view the classes using WbemTest. This Link will show you how to use WbemTest and explore the different classes.

If you want to find NICs that are connected then you should use the query

Select * From Win32_NetworkAdapter Where NetConnectionID is not Null

Bear in mind that you may get a few extra results. Like VPN adapters and such.

As for speed. You can try this Link

It is in VB6 so you'd have to convert it. A task I wouldn't want.

I'm sure you could do it with Windows APIs. I'm not sure how far you want to take it though.
 
Back
Top