获取已安装的网卡列表

时间:2011-10-31 07:39:46

标签: vb6 registry

我想使用visual basic 6.0在所有已安装的网络适配器名称中填充组合框。有没有办法这样做?我还想知道如何添加,编辑和删除注册表的值?

2 个答案:

答案 0 :(得分:1)

最简单的方法是shell-out ipconfig命令,将输出重定向到文件,然后解析文件。通常称为“ShellAndWait()”的函数有很多实现。我采取了一个我已经松开的 - 它可能不是最好的,但它确实有效。

Option Explicit

Private Declare Function CloseHandle Lib "Kernel32.dll" ( _
    ByVal hHandle As Long _
) As Long

Private Declare Function OpenProcess Lib "Kernel32.dll" ( _
    ByVal dwDesiredAccess As Long, _
    ByVal bInheritHandle As Long, _
    ByVal dwProcessId As Long _
) As Long

Private Declare Function WaitForSingleObject Lib "Kernel32.dll" ( _
    ByVal hHandle As Long, _
    ByVal dwMilliseconds As Long _
) As Long


Private Const INFINITE As Long = -1&
Private Const SYNCHRONIZE As Long = &H100000

Private Sub Form_Load()

    Dim oNetworkAdapters As VBA.Collection
    Dim vNetworkAdapter As Variant

    Set oNetworkAdapters = GetNetworkAdapters()

    cmbNICs.Clear
    For Each vNetworkAdapter In oNetworkAdapters
        cmbNICs.AddItem vNetworkAdapter
    Next vNetworkAdapter

End Sub

Public Function GetNetworkAdapters() As VBA.Collection

    Dim sTempFileName   As String
    Dim nFileNo         As Integer
    Dim sLine           As String
    Dim oNetworkAdapters As VBA.Collection

    Set oNetworkAdapters = New VBA.Collection

    sTempFileName = Environ$("TEMP") & "\VBTmp" & Format$(Now, "yyyymmddhhnnss")

    If ShellAndWait("cmd.exe /c ipconfig > """ & sTempFileName & """", vbHide) Then

        nFileNo = FreeFile

        Open sTempFileName For Input As #nFileNo

        Do Until EOF(nFileNo)
            Line Input #nFileNo, sLine
            If Len(sLine) > 0 Then
                If sLine Like "*:" Then
                    If Not sLine Like "  *:" Then
                        oNetworkAdapters.Add sLine
                    End If
                End If
            End If
        Loop

        Close #nFileNo

        Kill sTempFileName

    End If

    Set GetNetworkAdapters = oNetworkAdapters

End Function

' Start the indicated program and wait for it to finish, hiding while we wait.
Public Function ShellAndWait(ByRef in_sProgramName As String, _
                             ByVal in_enmWindowStyle As VbAppWinStyle) As Boolean

    Dim nProcessId      As Long
    Dim hProcess        As Long

    ' Start the program.
    On Error GoTo ShellError
    nProcessId = Shell(in_sProgramName, in_enmWindowStyle)
    On Error GoTo 0

    DoEvents

    ' Wait for the program to finish.
    ' Get the process handle.
    hProcess = OpenProcess(SYNCHRONIZE, 0, nProcessId)
    If hProcess <> 0 Then
        WaitForSingleObject hProcess, INFINITE
        CloseHandle hProcess
    End If

    ShellAndWait = True

    Exit Function

ShellError:
    MsgBox "Error starting task '" & in_sProgramName & "'" & vbCrLf & Err.Description, vbOKOnly Or vbExclamation, "Error"
End Function

答案 1 :(得分:0)

以下是检测所有以太网和无线适配器的简单代码

 NetworkInterface slectedNic;
IEnumerable<NetworkInterface> nics = NetworkInterface.GetAllNetworkInterfaces().Where(network => network.OperationalStatus == OperationalStatus.Up && (network.NetworkInterfaceType == NetworkInterfaceType.Ethernet || network.NetworkInterfaceType == NetworkInterfaceType.Wireless80211));
foreach (NetworkInterface item in nics)
        {
           cmbAdptors.Items.Add(item);
        }

但是如果你想只检测有源无线适配器

更改

 .Where(network => network.OperationalStatus == OperationalStatus.Up && (network.NetworkInterfaceType == NetworkInterfaceType.Ethernet || network.NetworkInterfaceType == NetworkInterfaceType.Wireless80211));

 .Where(network => network.OperationalStatus == OperationalStatus.Up && network.NetworkInterfaceType == NetworkInterfaceType.Wireless80211)