Click here to Skip to main content
15,889,822 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
VB
  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
'Fails here, and even needbytes is set to 0

      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.
Posted
Updated 4-Dec-11 23:06pm
v2

There's a comment against the Size assignment on P/Invoke.net

SP_DEVICE_INTERFACE_DETAIL_DATA didd = new SP_DEVICE_INTERFACE_DETAIL_DATA();
didd.cbSize = 4 + Marshal.SystemDefaultCharSize; // trust me :)


So it's probably worth checking the assigned value in your code.

http://www.pinvoke.net/default.aspx/Structures/SP_DEVICE_INTERFACE_DETAIL_DATA.html[^]

Alan.
 
Share this answer
 
Comments
creizlein 5-Dec-11 6:25am    
You are correct, i had tested with that code as well, and still the same issue, that results in 6, and i had tested it without success, all trough with 8 i can at least set the needbytes as i have explained. (i believe this has to deal with the 64/32bits difference)
Try this:

If Not SetupDiGetDeviceInterfaceDetail(h, dia, IntPtr.Zero, 0, needbytes, Nothing) Then
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900