2011-10-31 8 views

答えて

1

最も単純な方法は、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 
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); 
     } 

を検出する簡単なコードですが、とuは唯一のアクティブなワイヤレスアダプタを検出したい場合は

変更

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

.Where(network => network.OperationalStatus == OperationalStatus.Up && network.NetworkInterfaceType == NetworkInterfaceType.Wireless80211) 
関連する問題