USB Device info

Ken Stewart-Smith 1 Reputation point
2023-01-10T21:59:07.84+00:00

I am using VB 2005 and need help getting the following code to compile. The code retrieves USB device info:

                    '<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode, Pack:=1)> _
                    '    Structure SP_DEVICE_INTERFACE_DETAIL_DATA
                    '                    Public cbSize As Integer
                    '                    <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=260)> _
                    '                    Public DevicePath As String
                    '    End Structure

                    Dim didd As SP_DEVICE_INTERFACE_DETAIL_DATA = New SP_DEVICE_INTERFACE_DETAIL_DATA()

                    If IntPtr.Size = 8 Then 'for 64-bit operating systems
                        didd.cbSize = 8
                    Else
                        didd.cbSize = 4 + Marshal.SystemDefaultCharSize 'for 32-bit systems
                    End If

                    '<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode)> _
                    '    Public Structure SP_DEVINFO_DATA
                    '                    Public cbSize As Integer
                    '                    Public ClassGuid As Guid
                    '                    Public DevInst As Integer
                    '                    Public Reserved As IntPtr
                    '    End Structure

                    Dim da As SP_DEVINFO_DATA = New SP_DEVINFO_DATA()

                    da.cbSize = Marshal.SizeOf(da)

                    '<DllImport("Setupapi.dll", CharSet:=CharSet.Unicode, SetLastError:=True)> _
                    '        Public Shared Function SetupDiGetDeviceInterfaceDetail(ByVal hDevInfo As IntPtr, ByRef deviceInterfaceData As SP_DEVICE_INTERFACE_DATA, ByVal deviceInterfaceDetailData As IntPtr, ByVal deviceInterfaceDetailDataSize As UInt32, <System.Runtime.InteropServices.Out()> ByRef requiredSize As UInt32, ByVal deviceInfoData As IntPtr) As Boolean
                    'End Function

                    bRet = SetupDiGetDeviceInterfaceDetail(hdevDisplayInfoSet, data, didd, dwBytes, dwBytes, da)

                    ' COMPILE ERRORS:
                    ' ddid - SP_DEVICE_INTERFACE_DETAIL_DATA cannot be converted to 'System.IntPtr'
                    ' da - SP_DEVINFO_DATA cannot be converted to 'System.IntPtr'

The code give the above two errors. How can I modify the code to compile and run properly.

Thanks,

Ken

Developer technologies VB
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Michael Taylor 60,161 Reputation points
    2023-01-10T22:37:54.35+00:00

    If this code compiled before then I cannot answer as to why but doing a quick google on pinvoke.net reveals the import should look like this.

    Public Declare Auto Function SetupDiGetDeviceInterfaceDetail Lib "setupapi.dll" ( _
        ByVal DeviceInfoSet As IntPtr, _
        ByRef DeviceInterfaceData As SP_DEVICE_INTERFACE_DATA, _
        ByRef DeviceInterfaceDetailData As SP_DEVICE_INTERFACE_DETAIL_DATA, _
        ByVal DeviceInterfaceDetailDataSize As UInteger, _
        ByRef RequiredSize As UInteger, _
        ByRef DeviceInfoData As SP_DEVINFO_DATA) As Boolean '
    

    Then you are passing (by ref) the structure, not a pointer to the structure. Additionally you didn't provide us all the input that you were passing so you may need to make additional changes as well. I'm also not sure that the SP_DEVICE_INTERFACE_DETAIL_DATA structure is properly set for the charset or its size is correct. But I haven't tested this. You can refer to the earlier PInvoke.net link to see what should be working VB code for this API.

    0 comments No comments

  2. Castorix31 90,521 Reputation points
    2023-01-12T00:07:25.83+00:00

    See one of the samples I had posted, like in this thread : How do I retreive a USB "Bus reported device description"

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.