IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Windows Discussion :

Acceder en c++ à un device USB "composé"


Sujet :

Windows

  1. #1
    Candidat au Club
    Profil pro
    Inscrit en
    Mars 2006
    Messages
    5
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2006
    Messages : 5
    Points : 4
    Points
    4
    Par défaut Acceder en c++ à un device USB "composé"
    Bonjour,

    J'ai un périphérique USB composé d'un HUB qui vient se brancher sur le PC et sur lequel est branché un lecteur de cartes & une clé USB standard. J'utilise windows 2000 et windows XP.

    Mon but est, ayant un handle sur la clé USB, d'identifier quel est le lecteur de carte qui est branché sur le même Hub (et s'il est branché! ).

    Pour ça, j'essaye d'utiliser les fonctions CM_Get_Sibling mais
    1) on m'a dit que ces fonctions sont vouées à disparaitre ds les versions suivantes de windows
    2) cette fonction me retourne un DEVINST alors que pour que je puisse avoir un handle, il me faudrait un HDEVINFO (pour pouvoir faire un CreateFile puis un DeviceIOControl)

    Quelqu'un aurait une idée? Merci!

  2. #2
    Membre régulier
    Inscrit en
    Février 2006
    Messages
    83
    Détails du profil
    Informations forums :
    Inscription : Février 2006
    Messages : 83
    Points : 71
    Points
    71
    Par défaut
    ce code fonctionne il te faut juste modifer le GUID

    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
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    #include <stdio.h>
    #include <stdlib.h>
    
    #include <objbase.h>    //DWORRD, ...
    #include <setupapi.h>	// you may have to manually include this library.
    #include <initguid.h>   //USBIO_GUID
    //#include <conio.h>    // ? unnecessary for the moment
    
    
    //displayPeriph libs
    #include <windows.h>
    //#include <devguid.h>
    #include <regstr.h>
    #pragma comment &#40;lib,"setupapi.lib"&#41;
    #define INITGUID
    
    int main&#40;&#41;
    &#123;
     //GUID hidGUID = &#123;0xa5dcbf10, 0x6530, 0x11d2, &#123;0x90, 0x1f, 0x00, 0xc0, 0x4f, 0xb9, 0x51, 0xed&#125;&#125;; 
     //GUID hidGUID = &#123;0x53f56307, 0xb6bf, 0x11d0, &#123;0x94, 0xf2, 0x00, 0xa0, 0xc9, 0x1e, 0xfb, 0x8b&#125;&#125;;
    ///*ROOT_HSB*/GUID hidGUID = &#123;0xf18a0e88, 0xc30c, 0x11d0, &#123;0x88, 0x15, 0x00, 0xa0, 0xc9, 0x06, 0xbe, 0xd8&#125;&#125;; 
    //GUID hidGUID = &#123;0x53f56307, 0xb6bf, 0x11d0, &#123;0x94, 0xf2, 0x00, 0xa0, 0xc9, 0x1e, 0xfb, 0x8b&#125;&#125;;
    ///*DOUCHETTE*/GUID hidGUID = &#123;0x745a17a0L, 0x74d3, 0x11d0, &#123;0xb6, 0xfe, 0x00, 0xa0, 0xc9, 0x0f, 0x57, 0xda&#125;&#125;;
    /*DOUCHETTE*/GUID hidGUID = &#123;0xA5DCBF10L, 0x6530, 0x11D2, &#123;0x90, 0x1F, 0x00, 0xC0, 0x4F, 0xB9, 0x51, 0xED&#125;&#125;;
    	
    /*
    HDEVINFO hardwareDeviceInfoSet;
        SP_DEVICE_INTERFACE_DATA deviceInterfaceData;
        PSP_INTERFACE_DEVICE_DETAIL_DATA deviceDetail;
        ULONG requiredSize;
        HANDLE deviceHandle = INVALID_HANDLE_VALUE;
        DWORD result;
    
    
    
       
        hardwareDeviceInfoSet = SetupDiGetClassDevs &#40;&hidGUID,
                                                     NULL,
                                                     NULL, 
                                                     &#40;DIGCF_PRESENT |
                                                     DIGCF_INTERFACEDEVICE&#41;&#41;; 
    
        deviceInterfaceData.cbSize = sizeof&#40;SP_DEVICE_INTERFACE_DATA&#41;;
    
    
        result = SetupDiEnumDeviceInterfaces &#40;hardwareDeviceInfoSet,
                                              NULL,
                                              &hidGUID,
                                              0,
                                              &deviceInterfaceData&#41;;
    
        if &#40;result == FALSE&#41;
        &#123;
            SetupDiDestroyDeviceInfoList &#40;hardwareDeviceInfoSet&#41;;
    
        &#125;
    
     
        SetupDiGetDeviceInterfaceDetail &#40;hardwareDeviceInfoSet,
                                         &deviceInterfaceData,
                                         NULL, //interfaceDetail,
                                         0, //interfaceDetailSize,
                                         &requiredSize,
                                         0&#41;; //infoData&#41;&#41;
    
     
        deviceDetail = &#40;PSP_INTERFACE_DEVICE_DETAIL_DATA&#41;malloc&#40;requiredSize&#41;;
        deviceDetail->cbSize = sizeof&#40;SP_INTERFACE_DEVICE_DETAIL_DATA&#41;;
    
    
        if &#40;!SetupDiGetDeviceInterfaceDetail &#40;hardwareDeviceInfoSet,
                                              &deviceInterfaceData,
                                              deviceDetail,
                                              requiredSize,
                                              &requiredSize,
                                              NULL&#41;&#41; 
        &#123;
            SetupDiDestroyDeviceInfoList &#40;hardwareDeviceInfoSet&#41;;
            free &#40;deviceDetail&#41;;
    
        &#125;
    
    
        deviceHandle = CreateFile &#40;deviceDetail->DevicePath,
                                   GENERIC_READ,
                                   FILE_SHARE_READ | FILE_SHARE_WRITE,
                                   NULL,        // no SECURITY_ATTRIBUTES structure
                                   OPEN_EXISTING, // No special create flags
                                   0, 
                                   NULL&#41;;       // No template file
    DWORD res = GetLastError&#40;&#41;;
    	if&#40;deviceHandle==INVALID_HANDLE_VALUE&#41;
    			printf&#40;"INVALID HANDLE\n"&#41;;
    		else
    			printf&#40;"LE CREATEFILE FONCTIONNE\n"&#41;;
    
    printf&#40;"devicename &#58; %s\n",deviceDetail->DevicePath&#41;;*/
    /*
    char* buffer;
    DWORD nb_caractere;
    printf&#40;"lecture\n"&#41;;
    
    COMMTIMEOUTS Timeout;
    Timeout.ReadTotalTimeoutConstant=10000;
    Timeout.WriteTotalTimeoutConstant=0;
    Timeout.WriteTotalTimeoutMultiplier=0;
    SetCommTimeouts&#40;deviceHandle,&Timeout&#41;;
    
    int testread;
    testread=ReadFile&#40;deviceHandle,buffer,2048,&nb_caractere,NULL&#41;;
    
    if&#40;testread==0&#41;
    printf&#40;"erreur read %d\n",testread&#41;;
    else
    printf&#40;"contenu &#58; %s\n",buffer&#41;;
    
    if&#40;GetLastError&#40;&#41;==ERROR_HANDLE_EOF&#41;
    printf&#40;"ERROR_HANDLE_EOF"&#41;;
    if&#40;GetLastError&#40;&#41;==ERROR_IO_PENDING&#41;
    printf&#40;"ERROR_IO_PENDING"&#41;;
    */
    /*DWORD z;
    DWORD nb;
    if&#40;&#40;nb=WriteFile&#40;deviceHandle,"coucou",6,&z,NULL&#41;&#41;==0&#41;
    printf&#40;"ERREUR WRITEFILE"&#41;;
    
    
    SetupDiDestroyDeviceInfoList &#40;hardwareDeviceInfoSet&#41;;
    free &#40;deviceDetail&#41;;
    
     */
    	HDEVINFO hDevInfo;
        SP_INTERFACE_DEVICE_DATA InterfaceDeviceInfoData;
    
        hDevInfo = SetupDiGetClassDevs&#40;&hidGUID, 0, 0, DIGCF_PRESENT | DIGCF_INTERFACEDEVICE&#41;;
    
        if &#40;hDevInfo == INVALID_HANDLE_VALUE&#41;
        &#123;
    		printf&#40;"Erreur SetUpDiGetClassDevs\n"&#41;;
            return 0;
        &#125;
    
    	SP_DEVINFO_DATA DeviceInfoData;
    	DeviceInfoData.cbSize = sizeof&#40;SP_DEVINFO_DATA&#41;;
    	int nb = SetupDiEnumDeviceInfo&#40;hDevInfo,0,&DeviceInfoData&#41;;
    	for &#40;int i=0;SetupDiEnumDeviceInfo&#40;hDevInfo,i,&DeviceInfoData&#41;;i++&#41;
    	&#123;
    		InterfaceDeviceInfoData.cbSize = sizeof&#40;SP_INTERFACE_DEVICE_DATA&#41;;
    		DWORD needed;
    		if &#40;!SetupDiEnumDeviceInterfaces&#40;hDevInfo,NULL,&hidGUID,i,&InterfaceDeviceInfoData&#41;&#41;
    		&#123;
    			SetupDiDestroyDeviceInfoList&#40;hDevInfo&#41;;
    			return 0;
    		&#125;
    		//une première fois afin de connaître la taille de la zone mémoire à réserver &#40;needed&#41;
    		SetupDiGetDeviceInterfaceDetail&#40;hDevInfo,&InterfaceDeviceInfoData,NULL,0,&needed,NULL&#41;;
    		//on réserve la taille mémoire
    		PSP_DEVICE_INTERFACE_DETAIL_DATA detail = &#40;PSP_INTERFACE_DEVICE_DETAIL_DATA&#41; new char&#91;needed&#93;;
    		if &#40;!detail&#41;
    		&#123;
    			printf&#40;"Problème mémoire PSP_INTERFACE_DEVICE_DETAIL_DATA"&#41;;
    			SetupDiDestroyDeviceInfoList&#40;hDevInfo&#41;;
    			return 0;
    		&#125;
    		//on cherche le "nom" du device
    		detail->cbSize = sizeof&#40;SP_DEVICE_INTERFACE_DETAIL_DATA&#41;;
    		if &#40;!SetupDiGetDeviceInterfaceDetail&#40;hDevInfo,&InterfaceDeviceInfoData,detail,needed,NULL,NULL&#41;&#41;
    		&#123;
    			delete detail;
    			printf&#40;"Aucune information de détail"&#41;;
    			SetupDiDestroyDeviceInfoList&#40;hDevInfo&#41;;
    			return 0;
    		&#125;
    
    		printf&#40;"%s\n",detail->DevicePath&#41;;
    		HANDLE hCom;
    		hCom = CreateFile&#40;detail->DevicePath, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL&#41;;
    		DWORD test = GetLastError&#40;&#41;;
    
    		if &#40;hCom == INVALID_HANDLE_VALUE&#41;
    		&#123;
    			LPSTR Message;
    			FormatMessage&#40;FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,NULL,test,
    				MAKELANGID&#40;LANG_NEUTRAL, SUBLANG_DEFAULT&#41;,&#40;LPSTR&#41;&Message,0,NULL&#41;;
    			printf&#40;"%s\n",Message&#41;;
    		&#125;
    		else
    		&#123;
    			//printf&#40;"HANDLE Correct %d\n",hCom&#41;;
    		&#125;
    /*
    
    char* CB=&#40;char*&#41;malloc&#40;50&#41;;
    DWORD nb;
    if&#40;!ReadFile&#40;hCom,CB,100,&nb,NULL&#41;&#41;
    	printf&#40;"erreur readfile\n"&#41;;
    test = GetLastError&#40;&#41;;
    CB&#91;nb&#93;='\0';
    
    
    		if &#40;hCom != INVALID_HANDLE_VALUE&#41;
    		&#123;
    			CloseHandle&#40;hCom&#41;;
    			printf&#40;"la c sur ca bug\n"&#41;;
    		&#125;
    		if &#40;detail&#41;
    			delete detail;
    	&#125;
    
    	*/&#125;
        SetupDiDestroyDeviceInfoList&#40;hDevInfo&#41;;
    return 0;
    &#125;

  3. #3
    Candidat au Club
    Profil pro
    Inscrit en
    Mars 2006
    Messages
    5
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2006
    Messages : 5
    Points : 4
    Points
    4
    Par défaut
    Merci beaucoup pour les infos!

    J'arrive à lister les périphériques ayant un GUID (USB Device, USB Hub, etc)
    Par contre, du coup, je n'arrive pas à récupérer le bon handle de mon périphérique de stockage de masse USB...

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
        hDrive = CreateFile &#40; lettreLecteur, GENERIC_READ,
            FILE_SHARE_READ|FILE_SHARE_WRITE, 0,
            OPEN_EXISTING, 0, 0 &#41;;
        if &#40; INVALID_HANDLE_VALUE != hDrive &#41;
        &#123;
     /* Faire qqchose, mais quoi? */
        &#125;
    Dans lettreLecteur, il y a "\\.\D:", par exemple, ce qui correspond à la lettre de la flash USB et dont je voudrais connaitre le lecteur de carte associé.
    Maintenant, avec l'autre code, j'arrive à lister tous les périphériques d'une certaine classe GUID. Mais comment je pourrais faire le lien entre les 2?

  4. #4
    Expert éminent sénior

    Homme Profil pro
    pdg
    Inscrit en
    Juin 2003
    Messages
    5 751
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 42
    Localisation : France, Hérault (Languedoc Roussillon)

    Informations professionnelles :
    Activité : pdg

    Informations forums :
    Inscription : Juin 2003
    Messages : 5 751
    Points : 10 670
    Points
    10 670
    Billets dans le blog
    3
    Par défaut
    Peut être avec GetVolumeNameForVolumeMountPoint.

  5. #5
    Candidat au Club
    Profil pro
    Inscrit en
    Mars 2006
    Messages
    5
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2006
    Messages : 5
    Points : 4
    Points
    4
    Par défaut
    Ca me donne une nouvelle approche du probleme...

    je n'ai tjrs pas réussi à récupérer le bon handle. Avec ces éléments, je vais essayer de trouver une solution.
    Si qqun a d'autres idées, je suis preneur!

    Merci

Discussions similaires

  1. installation device usb
    Par mongolic dans le forum Matériel
    Réponses: 1
    Dernier message: 12/11/2009, 00h32
  2. acceder sur disque externe(sur USB ) en DOS
    Par 00go11 dans le forum Scripts/Batch
    Réponses: 0
    Dernier message: 19/12/2007, 20h33
  3. [C++] Simulation déco / reco d'un device USB
    Par benecie119 dans le forum MFC
    Réponses: 7
    Dernier message: 24/05/2006, 12h26

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo