Bonjour,

Je souhaite lister tous les peripheriques USB de mon PC en utilisant les dll de base Hid et SetupApi dans lesquelles se trouves les fonctions HIdD_GetHidGuid et les fonctions SetupDi.....

Apres une longue semaine de recherche Google et apres avoir surf sur pas mal de forum je suis arrive a taper le code ci-dessous.
Ce code devrait marcher mais il ne detecte rien.
Merci d'avance pour vos remarques.
NB. Le code est en phase d'optimisation donc ne tenez pas compte de la mise en forme ou des astuces de simplification
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
 
Imports System
Imports System.Runtime.InteropServices
Imports System.IO
 
Public Class TestUSB
 
#Region "Dfinition des structures"
    ' GUID structure 
    <StructLayout(LayoutKind.Sequential)> _
    Public Structure GUID
        Public Data1 As Integer
        Public Data2 As System.UInt16
        Public Data3 As System.UInt16
        <MarshalAs(UnmanagedType.ByValArray, SizeConst:=8)> Public data4 As Byte()
    End Structure
 
    'SP_DEVICE_INTERFACE_DATA definition
    <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> _
    Public Structure SP_DEVICE_INTERFACE_DATA
        Public cbSize As Int32
        Public InterfaceClassGuid As System.Guid
        Public Flags As Int32
        Public Reserved As Int32
    End Structure
 
    'SP_DEVICE_INTERFACE_DETAIL_DATA definition
    <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> _
    Public Structure SP_DEVICE_INTERFACE_DETAIL_DATA
        Public cbSize As Int32
        Public DevicePath() As String
    End Structure
 
    'SP_DEVINFO_DATA definition
    <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> _
    Public Structure SP_DEVINFO_DATA
        Public cbSize As Int32
        Public ClassGuid As GUID
        Public DevInst As Int32
        Public Reserved As Int32
    End Structure
#End Region
 
#Region "Dll Import"
    <DllImport("HID.dll")> _
    Private Shared Sub HidD_GetHidGuid(ByRef GuidPtr As System.Guid)
    End Sub
 
    <DllImport("setupapi.dll", SetLastError:=True)> _
    Private Shared Function SetupDiGetClassDevs _
        (ByRef lpHidGuid As System.Guid, _
         ByVal Enumerator As String, _
         ByVal hwndParent As Integer, _
         ByVal Flags As Integer) _
         As IntPtr
    End Function
 
    <DllImport("setupapi.dll", SetLastError:=True)> _
    Private Shared Function SetupDiEnumDeviceInterfaces _
        (ByVal DeviceInfoSet As IntPtr, _
        ByVal DeviceInfoData As Integer, _
        ByRef lpHidGuid As System.Guid, _
        ByVal MemberIndex As Integer, _
        ByRef DeviceInterfaceData As SP_DEVICE_INTERFACE_DATA) _
        As Boolean
    End Function
 
    <DllImport("setupapi.dll", SetLastError:=True)> _
    Private Shared Function SetupDiGetDeviceInterfaceDetail _
        (ByVal DeviceInfoSet As Integer, _
        ByRef lpDeviceInterfaceData As SP_DEVICE_INTERFACE_DATA, _
        ByVal DeviceInterfaceDetailData As IntPtr, _
        ByVal DeviceInterfaceDetailDataSize As Integer, _
        ByRef requiredSize As Integer, _
        ByVal DeviceInfoData As IntPtr) _
        As Boolean
    End Function
 
    <DllImport("setupapi.dll", SetLastError:=True)> _
    Private Shared Function SetupDiDestroyDeviceInfoList(ByVal DeviceInfoSet As IntPtr) As Integer
    End Function
 
#End Region
 
    Public Function Finding_Device() As String
 
        Dim HidGuid As System.Guid
        'Dim HidGuid As GUID
        Const DIGCF_PRESENT As Short = &H2S
        Const DIGCF_DEVICEINTERFACE As Short = &H10S
        Dim DeviceInfoSet As IntPtr
        Dim MemberIndex As Integer
        Dim MyDeviceInterfaceData As SP_DEVICE_INTERFACE_DATA
        Dim result As Boolean
        Dim Buffersize As Integer
        Dim Success As Boolean
        Dim DetailDataBuffer As IntPtr
        Dim DevicePathName As String
        'Dim DevicePathName(127) As String
 
        HidD_GetHidGuid(HidGuid)
        DeviceInfoSet = SetupDiGetClassDevs(HidGuid, vbNullString, 0, DIGCF_PRESENT Or DIGCF_DEVICEINTERFACE)
        MemberIndex = 2
        MyDeviceInterfaceData.cbSize = Marshal.SizeOf(MyDeviceInterfaceData)
        result = SetupDiEnumDeviceInterfaces(DeviceInfoSet, 0, HidGuid, MemberIndex, MyDeviceInterfaceData)
        Success = SetupDiGetDeviceInterfaceDetail(DeviceInfoSet, MyDeviceInterfaceData, IntPtr.Zero, 0, Buffersize, IntPtr.Zero)
        DetailDataBuffer = Marshal.AllocHGlobal(Buffersize)
        Marshal.WriteInt32(DetailDataBuffer, 4 + Marshal.SystemMaxDBCSCharSize)
        Success = SetupDiGetDeviceInterfaceDetail(DeviceInfoSet, MyDeviceInterfaceData, DetailDataBuffer, Buffersize, Buffersize, IntPtr.Zero)
        Dim pDevicePathName As IntPtr = New IntPtr(DetailDataBuffer.ToInt32 + 4)
        DevicePathName = Marshal.PtrToStringAuto(pDevicePathName)
        Marshal.FreeHGlobal(DetailDataBuffer)
        SetupDiDestroyDeviceInfoList(DetailDataBuffer)
        Return HidGuid.ToString
    End Function
 
End Class