Network Devices & SetupAPI.DLL
I am trying to enumerate all the network adapters on a machine and then disable all of them except for one. I am trying to do this with setupapi.dll. I have some C# examples but I guess I am just converting the source code incorrectly. Here is what I currently have:
Code:
Private Declare Function SetupDiGetClassDevsW Lib "setupapi.dll" (ByRef ClassGUID As Guid, ByVal samDesired As Integer, ByVal Flags As Integer, ByVal hWndParent As String) As Integer
Private Declare Function SetupDiEnumDeviceInterfaces Lib "setupapi.dll" (ByVal hDevInfo As IntPtr, ByRef devInfo As SP_DEVINFO_DATA, ByRef Guid As Guid, ByVal memberIndex As Integer, ByRef deviceInterfaceData As SP_DEVICE_INTERFACE_DATA) As Boolean
Private Declare Function SetupDiGetDeviceInterfaceDetail Lib "setupapi.dll" (ByVal hDevInfo As IntPtr, ByRef DeviceInterfaceData As SP_DEVICE_INTERFACE_DATA, ByRef DeviceInterfaceDetailData As SP_DEVICE_INTERFACE_DETAIL_DATA, ByVal DataSize As Integer, ByVal RequiredSize As Integer, ByRef DeviceInfoData As SP_DEVINFO_DATA) As Boolean
Const GUID_DEVINTERFACE_NETADAPT As String = "4D36E972-E325-11CE-BFC1-08002BE10318"
Const DIGCF_PRESENT As Integer = &H2
Const DIGCF_DEVICEINTERFACE As Integer = &H10
Private NetAdapterGUID As Guid
Private Structure SP_DEVINFO_DATA
Dim cbSize As Integer
Dim ClassGuid As Guid
Dim DevInst As Integer
Dim Reserved As IntPtr
End Structure
Private Structure SP_DEVICE_INTERFACE_DATA
Dim cbSize As Integer
Dim Guid As Guid
Dim Flags As Integer
Dim Reserved As IntPtr
End Structure
Private Structure SP_DEVICE_INTERFACE_DETAIL_DATA
Dim cbSize As Integer
Dim DevicePath As Char
End Structure
Private Sub frmMain_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim Handle As IntPtr
Dim Dia As SP_DEVICE_INTERFACE_DATA
Dim Da As SP_DEVINFO_DATA
Dim Didd As SP_DEVICE_INTERFACE_DETAIL_DATA
Dim Success As Boolean = True
Dim i As Integer = 0
Dim nRequiredSize = 0
Dim nBytes As Integer = 1000
NetAdapterGUID = New Guid(GUID_DEVINTERFACE_NETADAPT)
Handle = SetupDiGetClassDevsW(NetAdapterGUID, 0, IntPtr.Zero, DIGCF_PRESENT Or DIGCF_DEVICEINTERFACE)
While Success
Dia = New SP_DEVICE_INTERFACE_DATA
Dia.cbSize = Marshal.SizeOf(Dia)
Success = SetupDiEnumDeviceInterfaces(Handle, Nothing, NetAdapterGUID, i, Dia)
If Success Then
Da = New SP_DEVINFO_DATA
Da.cbSize = Marshal.SizeOf(Da)
Didd = New SP_DEVICE_INTERFACE_DETAIL_DATA
Dia.cbSize = 4 + Marshal.SystemDefaultCharSize
MsgBox(SetupDiGetDeviceInterfaceDetail(Handle, Dia, Didd, nBytes, nRequiredSize, Da))
End If
i += 1
End While
End Sub
The code is failing on Success = SetupDiEnumDeviceInterfaces(Handle, Nothing, NetAdapterGUID, i, Dia)
Does anyone have any ideas?