SetupDiEnumDeviceInterface and SetupDiEnumDeviceInterfaceDetail?

  • Thread starter Thread starter Schorschi
  • Start date Start date
S

Schorschi

These seem right?

Private Declare Auto Function SetupDiEnumDeviceInterfaces Lib
"SETUPAPI" _
(ByVal theList As IntPtr, ByVal theInformation As
_SP_DEVINFO_DATA, ByVal theClass As Byte(), ByVal theIndex As Integer,
ByRef theData As _SP_DEVICE_INTERFACE_DATA) As Byte

Private Declare Auto Function SetupDiGetDeviceInterfaceDetail Lib
"SETUPAPI" _
(ByVal theList As IntPtr, ByVal theInformation As
_SP_DEVICE_INTERFACE_DATA, ByRef theData As
_SP_DEVICE_INTERFACE_DETAIL_DATA, ByVal theLength As Integer, ByRef
theSize As Integer, ByVal theDevice As _SP_DEVINFO_DATA) As Byte

Public Structure _SP_DEVICE_INTERFACE_DATA

'

Dim theSize As Integer, _
theClass As Guid, _
theFlag As Integer, _
theReserved As IntPtr

End Structure

Public Structure _SP_DEVICE_INTERFACE_DETAIL_DATA

'

Dim theSize As Integer, _
thePath As Byte()

End Structure

Below is a subset of my class for getting interface information...

---------------------------------

Option Explicit On
Option Strict On

'

Imports System.Runtime.InteropServices.Marshal
Imports System.Runtime.InteropServices

'

Public Class DeviceInterfaceClass

'

Public Const GUID_DEVINTERFACE_DISK As String =
"{53f56307-b6bf-11d0-94f2-00a0c91efb8b}", _
GUID_DEVINTERFACE_TAPE As String =
"{53f5630b-b6bf-11d0-94f2-00a0c91efb8b}"

'

Private Declare Auto Function SetupDiEnumDeviceInterfaces Lib
"SETUPAPI" _
(ByVal theList As IntPtr, ByVal theInformation As
_SP_DEVINFO_DATA, ByVal theClass As Byte(), ByVal theIndex As Integer,
ByRef theData As _SP_DEVICE_INTERFACE_DATA) As Byte

Private Declare Auto Function SetupDiGetDeviceInterfaceDetail Lib
"SETUPAPI" _
(ByVal theList As IntPtr, ByVal theInformation As
_SP_DEVICE_INTERFACE_DATA, ByRef theData As
_SP_DEVICE_INTERFACE_DETAIL_DATA, ByVal theLength As Integer, ByRef
theSize As Integer, ByVal theDevice As _SP_DEVINFO_DATA) As Byte

'

Public Structure _SP_DEVICE_INTERFACE_DATA

'

Dim theSize As Integer, _
theClass As Guid, _
theFlag As Integer, _
theReserved As IntPtr

End Structure

Public Structure _SP_DEVICE_INTERFACE_DETAIL_DATA

'

Dim theSize As Integer, _
thePath As Byte()

End Structure

Private Structure InterfaceStructure

'

Dim theResult As Integer, _
theList As IntPtr, _
theId As Guid, _
theMachine As String

End Structure

Private theInterface As InterfaceStructure

'

Public Sub New(ByVal theClass As String, Optional ByVal theFlag As
Integer = DIGCF_DEVICEINTERFACE Or DIGCF_PRESENT, Optional ByVal
theMachine As String = Nothing)

'

With theInterface

'

.theId = New Guid(theClass)

.theList =
SetupDiCreateDeviceInfoListEx(.theId.ToByteArray, _
Nothing, Nothing, Nothing)

Select Case (IsHandle(.theList))

Case True

'

.theList =
SetupDiGetClassDevsEx(.theId.ToByteArray, Nothing, Nothing, _
theFlag, .theList, theMachine, Nothing)

Select Case (IsHandle(.theList))

Case True

'

Case False

'

.theResult = GetLastWin32Error()

Throw New Exception

End Select

Case False

'

.theResult = GetLastWin32Error()

Throw New Exception

End Select

End With

End Sub

Public Overloads Sub Dispose()

'

With theInterface

'

.theResult = CInt(IIf(CBool _
(SetupDiDestroyDeviceInfoList(.theList)),
DeviceError.Success, GetLastWin32Error))

.theList = Nothing

End With

End Sub

Public Function GetInterface(ByVal theIndex As Integer, ByRef
theData As _SP_DEVICE_INTERFACE_DATA) As Integer

Dim theInterfaceOrNot As Boolean

'

With theInterface

'

theData.theSize = SizeOf _
(theData)

theInterfaceOrNot = CBool(SetupDiEnumDeviceInterfaces _
(.theList, Nothing, .theId.ToByteArray, theIndex,
theData))

Select Case (theInterfaceOrNot)

Case True

'

Case False

'

.theResult = GetLastWin32Error()

End Select

'

Return (.theResult)

End With

End Function

Public Function GetInterfaceDetail(ByVal theInformation As
_SP_DEVICE_INTERFACE_DATA, ByRef thePath As String) As Integer

Dim theDetailOrNot As Boolean, _
theSize As Integer, _
theDetail As _SP_DEVICE_INTERFACE_DETAIL_DATA, _
theData As Byte()

'

With theInterface

'

theDetailOrNot = CBool(SetupDiGetDeviceInterfaceDetail _
(.theList, theInformation, Nothing, theSize, theSize,
Nothing))

Select Case (theDetailOrNot)

Case True

'

ReDim theData(theSize)

theDetail = DirectCast(theData,
_SP_DEVICE_INTERFACE_DETAIL_DATA)

theDetail.theSize = theSize

theDetailOrNot =
CBool(SetupDiGetDeviceInterfaceDetail _
(.theList, theInformation, theDetail, theSize,
theSize, Nothing))

Select Case (theDetailOrNot)

Case True

'

Case False

'

.theResult = GetLastWin32Error()

End Select

Case False

'

.theResult = GetLastWin32Error()

End Select

'

Return (.theResult)

End With

End Function

End Class
---------------------------------

I call the class with something like this...

theInterface = New DeviceInterfaceClass _
(GUID_DEVINTERFACE_DISK, DIGCF_PRESENT Or
DIGCF_DEVICEINTERFACE)

While (theResult <> 259)

'

theResult =
theInterface.GetInterface(theInterfaceIndex, theInterfaceData)

theResult =
theInterface.GetInterfaceDetail(theInterfaceData, theDevicePath)

theInterfaceIndex += 1

End While

theInterface.Dispose()

Can't seem to see why my GetInterface function does not work? it
always returns error 259, even when I know a given device has at least
one interface.

What I am trying to do is get the actual device path, that would be
acceptable for say CreateFile() API etc.
 
Back
Top