How to get the PID and VID of a USB Mass Storage Device



I am trying to get the Version ID and PID of a Mass storage Device in my
application programmatically.
I am using the following code to get the VID and PID of a flash disk.But I
am getting a same value every time for all different flash disks.
Anyone knows any other way of getting VID,PID of a Flash Disk
programmatically?or is thereanything wrong in the following code?


int done = FALSE;
int drive = 0;

// for (drive = 0; drive < MAX_IDE_DRIVES; drive++)
// {
HANDLE hPhysicalDriveIOCTL = 0;

// Try to get a handle to PhysicalDrive IOCTL, report failure
// and exit if can't.
char driveName [256];

sprintf (driveName, "\\\\.\\PhysicalDrive%d", drive);

// Windows NT, Windows 2000, Windows XP - admin rights not required
hPhysicalDriveIOCTL = CreateFile (driveName, 0,
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
OPEN_EXISTING, 0, NULL);
// if (hPhysicalDriveIOCTL == INVALID_HANDLE_VALUE)
// printf ("Unable to open physical drive %d, error code: 0x%lX\n",
// drive, GetLastError ());

if (hPhysicalDriveIOCTL != INVALID_HANDLE_VALUE)
{
STORAGE_PROPERTY_QUERY query;
DWORD cbBytesReturned = 0;


char buffer [1000];
memset ((void *) & query, 0, sizeof (query));
query.PropertyId = StorageDeviceProperty;
query.QueryType = PropertyStandardQuery;

memset (buffer, 0, sizeof (buffer));

if ( DeviceIoControl (hPhysicalDriveIOCTL, IOCTL_STORAGE_QUERY_PROPERTY,
& query,
sizeof (query),
buffer,
sizeof (buffer),
& cbBytesReturned, NULL) )
{
STORAGE_DEVICE_DESCRIPTOR * descrip = (STORAGE_DEVICE_DESCRIPTOR *) buffer;
char serialNumber [1000];

strcpy (serialNumber,
flipAndCodeBytes ( & buffer [descrip -> SerialNumberOffset]));
if (0 == HardDriveSerialNumber [0] &&
// serial number must be alphanumeric
// (but there can be leading spaces on IBM drives)
(isalnum (serialNumber [0]) || isalnum (serialNumber [19])))
strcpy (HardDriveSerialNumber, serialNumber);





if(descrip -> VendorIdOffset)
{
strcpy(vid, &buffer [descrip -> VendorIdOffset]);
}
if(buffer [descrip -> ProductIdOffset])
{
strcpy(pid, &buffer [descrip -> ProductIdOffset]);
}


// & buffer [descrip -> ProductRevisionOffset],
//serialNumber);

}
else
{
DWORD err = GetLastError ();
char errcod[4];
sprintf(errcod,"%d",err);
char errstr[100]="DeviceIOControl IOCTL_STORAGE_QUERY_PROPERTY error code =";
strcat(errstr,errcod);
AfxMessageBox (errstr);

}

memset (buffer, 0, sizeof (buffer));


CloseHandle (hPhysicalDriveIOCTL);
}
//}

Thanks in advance!

.



Relevant Pages

  • Re: Size of mallocd?
    ... char *buffer; ... buffer = calloc; ... I cannot do sizeof *buffer since it would only return the ... size of the pointer type. ...
    (comp.lang.c)
  • Re: Size of mallocd?
    ... char *buffer; ... buffer = calloc; ... I cannot do sizeof *buffer since it would only return the ... size of the pointer type. ...
    (comp.lang.c)
  • Re: Size of mallocd?
    ... char *buffer; ... buffer = calloc; ... I cannot do sizeof *buffer since it would only return the ... size of the pointer type. ...
    (comp.lang.c)
  • Re: I thought I understood sizeof().
    ... >> On the flip side, by using 'sizeof', if 'temp' is ever changed to a ... >> 'char *', ... > (which would have happened if 'temp' became smaller but snprintf() ... You are assuming that the buffer will always be larger than ...
    (comp.unix.programmer)
  • SSPI Kerberos for delegation
    ... const char *tokenSource, const char *name = NULL, ... DWORD bufsiz = sizeof buf; ... int n = ib.cbBuffer; ... // wserr() displays winsock errors and aborts. ...
    (microsoft.public.dotnet.framework.remoting)