I have been dealing with this all night long, and i know that there are many references topics that list on how to properly call this function but no matter what i still can make it work so i hope someone can help me out.
The error I'm getting is ErrorCode 1784 ("The supplied user buffer is not valid for the requested operation.") and here is a my code:
Const DIGCF_PRESENT As Integer = 2
Const DIGCF_DEVICEINTERFACE As Integer = 16
Public GUID_DISPLAY_DEVICE_ARRIVAL As Guid = Guid.Parse("{1CA05180-A699-450A-9A0C-DE4FBE3DDD89}")
<StructLayout(LayoutKind.Sequential)> Structure SP_DEVICE_INTERFACE_DETAIL_DATA
Public cbSize As Integer
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=256)> Public DevicePath As String
End Structure
<StructLayout(LayoutKind.Sequential)> Structure SP_DEVICE_INTERFACE_DATA
Public cbSize As Integer
Public interfaceClassGuid As Guid
Public Flags As Integer
Private Reserved As IntPtr
End Structure
<dllimport("setupapi.dll",> _
Shared Function SetupDiGetDeviceInterfaceDetail(ByVal DeviceInfoSet As IntPtr, ByRef DeviceInterfaceData As SP_DEVICE_INTERFACE_DATA, ByRef DeviceInterfaceDetailData As SP_DEVICE_INTERFACE_DETAIL_DATA, ByVal DeviceInterfaceDetailDataSize As Integer, ByRef RequiredSize As Integer, ByRef DeviceInfoData As SP_DEVICE_INTERFACE_DATA) As Boolean
End Function
<dllimport("setupapi.dll",> _
Shared Function SetupDiEnumDeviceInterfaces(ByVal DeviceInfoSet As IntPtr, ByVal DeviceInfoData As Integer, ByVal InterfaceClassGuid As Guid, ByVal MemberIndex As UInteger, ByRef DeviceInterfaceData As SP_DEVICE_INTERFACE_DATA) As Boolean
End Function
<dllimport("setupapi.dll",> _
Shared Function SetupDiGetClassDevs(ByVal ClassGuid As Guid, ByVal Enumerator As Integer, ByVal hwndParent As IntPtr, ByVal Flags As UInteger) As IntPtr
End Function
Sub Tester
Dim h As IntPtr = SetupDiGetClassDevs(GUID_DISPLAY_DEVICE_ARRIVAL, 0, IntPtr.Zero, DIGCF_PRESENT Or DIGCF_DEVICEINTERFACE)
Dim memberIndex As UInteger = 0
Dim dia As New SP_DEVICE_INTERFACE_DATA()
dia.cbSize = Marshal.SizeOf(dia)
If SetupDiEnumDeviceInterfaces(h, 0, GUID_DISPLAY_DEVICE_ARRIVAL, memberIndex, dia) Then
Dim didd As New SP_DEVICE_INTERFACE_DETAIL_DATA()
didd.cbSize = Marshal.SizeOf(didd)
Dim needbytes As Integer = 0
If Not SetupDiGetDeviceInterfaceDetail(h, dia, didd, didd.cbSize, needbytes, Nothing) Then
End If
End If
End Sub
The strange part is that i cannot even get needBytes to be correctly populated, it always returns to 0, the only way i have found to get a proper value in needbytes is to make a initial call setting the size to 8 (I'm running 64 bits), this way, needbytes get returned with a value of 216, which seems valid, but if i try to set the size to 216 i still get the same error.
I have been playing with the LayoutKind and Pack properties of the structure without success as well, so I'm really out of ideas, any help would be really appreciated.