• Viernes 19 de Abril de 2024, 06:46

Autor Tema:  Manejo Usb????  (Leído 1072 veces)

zed2000

  • Miembro activo
  • **
  • Mensajes: 31
    • Ver Perfil
Manejo Usb????
« en: Lunes 13 de Agosto de 2007, 01:44 »
0
Como puedo saber el path de un pendrive....
con este programa lo que hace es imprimir esto:

\\?\usb#vid_10d6&pid_1100#5&8b16baf&0&3#{a5dcbf10-6530-11d2-901f-00c04fb951ed}

osea la unidad asignada????
y alguna informacion sobre estaj tipos de datos
SP_DEVICE_INTERFACE_DATA
GUID

#include <stdlib.h>
#include <shlobj.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 (lib,"setupapi.lib")

#define INITGUID

int main()

{

GUID hidGUID = {0xa5dcbf10, 0x6530, 0x11d2, {0x90, 0x1f, 0x00, 0xc0, 0x4f, 0xb9, 0x51, 0xed}};

HDEVINFO hardwareDeviceInfoSet;

SP_DEVICE_INTERFACE_DATA deviceInterfaceData;

PSP_INTERFACE_DEVICE_DETAIL_DATA deviceDetail;

ULONG requiredSize;

HANDLE deviceHandle = INVALID_HANDLE_VALUE;

DWORD result;

//Get the HID GUID value - used as mask to get list of devices

// HidD_GetHidGuid (&hidGUID);

//Get a list of devices matching the criteria (hid interface, present)

hardwareDeviceInfoSet = SetupDiGetClassDevs (&hidGUID,

NULL, // Define no enumerator (global)

NULL, // Define no

(DIGCF_PRESENT | // Only Devices present

DIGCF_DEVICEINTERFACE)); // Function class devices.

deviceInterfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);

//Go through the list and get the interface data

result = SetupDiEnumDeviceInterfaces (hardwareDeviceInfoSet,

NULL, //infoData,

&hidGUID, //interfaceClassGuid,

0, //changement

&deviceInterfaceData);

/* Failed to get a device - possibly the index is larger than the number of devices */

if (result == FALSE)

{

SetupDiDestroyDeviceInfoList (hardwareDeviceInfoSet);

// return INVALID_HANDLE_VALUE;

}

//Get the details with null values to get the required size of the buffer

SetupDiGetDeviceInterfaceDetail (hardwareDeviceInfoSet,

&deviceInterfaceData,

NULL, //interfaceDetail,

0, //interfaceDetailSize,

&requiredSize,

0); //infoData))

//Allocate the buffer

deviceDetail = (PSP_INTERFACE_DEVICE_DETAIL_DATA)malloc(requiredSize);

deviceDetail->cbSize = sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA);

//Fill the buffer with the device details

if (!SetupDiGetDeviceInterfaceDetail (hardwareDeviceInfoSet,

&deviceInterfaceData,

deviceDetail,

requiredSize,

&requiredSize,

NULL))

{

SetupDiDestroyDeviceInfoList (hardwareDeviceInfoSet);

free (deviceDetail);

// return INVALID_HANDLE_VALUE;

}

//Open file on the device

deviceHandle = CreateFile (deviceDetail->DevicePath,

GENERIC_WRITE,

FILE_SHARE_WRITE,

NULL, // no SECURITY_ATTRIBUTES structure

OPEN_EXISTING, // No special create flags

0,

NULL); // No template file

if(deviceHandle==INVALID_HANDLE_VALUE)

printf("erreur");

else

printf("ca marche");

printf("\tDrive path: %s\n", deviceDetail->DevicePath);

SetupDiDestroyDeviceInfoList (hardwareDeviceInfoSet);
free (deviceDetail);
}

Karman

  • Miembro activo
  • **
  • Mensajes: 84
    • Ver Perfil
    • http://www.inexinferis.com.ar
Re: Manejo Usb????
« Respuesta #1 en: Sábado 25 de Agosto de 2007, 21:00 »
0
fijate esta función, la creé en base a la documentación en el MSDN(solo funciona con XP, esa es su gran limitación):

Código: Text
  1.  
  2. WORD GetDevicesNumber(const GUID* DeviceGUID,PDEVICES device){  
  3.   HDEVINFO hdev;
  4.   SP_DEVICE_INTERFACE_DATA did;
  5.   PSP_DEVICE_INTERFACE_DETAIL_DATA pdidd;
  6.   DWORD cbRequired,idev;    hdev=SetupDiGetClassDevs(DeviceGUID,0,0,DIGCF_PRESENT|DIGCF_DEVICEINTERFACE);
  7.   if (hdev != INVALID_HANDLE_VALUE) {  
  8.     did.cbSize = sizeof(did);
  9.     for(idev=0;idev &#60; 100; idev++) {
  10.       SetLastError(ERROR_NO_MORE_ITEMS);
  11.       if(SetupDiEnumDeviceInterfaces(hdev, 0, DeviceGUID, idev, &did)) {      
  12.         if(device!=NULL){
  13.           cbRequired = 0;
  14.           if(SetupDiGetDeviceInterfaceDetail(hdev, &did, 0, 0, &cbRequired, 0)||GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
  15.             pdidd = (PSP_DEVICE_INTERFACE_DETAIL_DATA)LocalAlloc(LPTR, cbRequired);
  16.             if (pdidd) {
  17.               pdidd-&#62;cbSize = sizeof(*pdidd);
  18.               if(SetupDiGetDeviceInterfaceDetail(hdev, &did, pdidd, cbRequired, &cbRequired, 0)) {
  19.                 strcpy(device[idev].DevicePath,pdidd-&#62;DevicePath);
  20.               }
  21.               LocalFree(pdidd);
  22.             }          
  23.           }
  24.         }
  25.       }else {
  26.         if(GetLastError() == ERROR_NO_MORE_ITEMS)
  27.           break;
  28.       }
  29.     }
  30.     SetupDiDestroyDeviceInfoList(hdev);
  31.   }  
  32.   return idev;
  33. }
  34.  

donde:
const GUID* DeviceGUID es el guid del tipo de dispositivo a detallar (el mismo que utilizabas anteriormente)
y PDEVICES es un puntero a la siguiente estructura:

Código: Text
  1.  
  2. typedef struct _DEVS_{
  3.   DISK_GEOMETRY Geometry;
  4.   LARGE_INTEGER DriveSize;
  5.   LARGE_INTEGER BufferSize;
  6.   char Model[64];
  7.   char Revision[64];
  8.   char Serial[64];
  9.   char DevicePath[MAX_PATH];
  10.   char DeviceName[32];
  11. }DEVICES,*PDEVICES;
  12.  
  13.  

Saludos