• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3518
  • Last Modified:

detect usb device and retrieve device descriptor and drive letter

as posted in http://www.experts-exchange.com/Programming/Q_20938290.html

hi,
i would like to seek help on detecting an usb device (removable media) and retrieve the device decriptor of the device and lastly, the drive letter that appears for the device. some elaboration, when there are more than 1 usb devices (removable media) attached, i would like to find out the respective device decriptors and drive letters.
i have read a couple of sites namely, www.usb.org and www.lvr.com but i was unable to find a working example to follow through. i will appreciate it if you could provide some c/c++ code to complete the tasks.

-ashrobo

note: thought i might get more help here.
0
ashrobo
Asked:
ashrobo
1 Solution
 
bkfirebirdCommented:
I would suggest that

1. first find out some info about this particular usb device, say the serial number.
There are simple MS SDK functions for doing that like GetVolumeInformation
see http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/base/volume_management_functions.asp

2. once you know the serial number, you can write a program to enumerate all the volumes on your system using FindFirstVolume, FindNextVolume, etc

3. Check each of these volumes and see which device has the serial number you are looking for.

4. Once you know which volume has the required serial number, then you can use GetVolumePathNamesForVolumeName and find out the path of this device.

I had done this kind of stuff some time ago and its a little hazy, go thru some of the available functions in the URL above ... they are all pretty simple to use.

hope this helps
kiran adigopula

0
 
ashroboAuthor Commented:
hi bkfirebird,
actually i would like to identify the usb devices not only by their serial numbers but also by the device descriptor values such as idVendor and idProduct. am i able to retreive those as well? thanks!

-ashrobo
0
 
bkfirebirdCommented:
LRESULT GetUsbDeviceStats()
{
      HRESULT  hres = CoInitializeEx(0, COINIT_MULTITHREADED);
      if (FAILED(hres))
      { cout << "Failed to initialize COM library. Error code = 0x"
       << hex << hres << endl; return 1;
      }

      hres =  CoInitializeSecurity(
    NULL,                        // Security descriptor
    -1,                          // COM negotiates authentication service
    NULL,                        // Authentication services
    NULL,                        // Reserved
    RPC_C_AUTHN_LEVEL_DEFAULT,   // Default authentication level for proxies
    RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation level for proxies
    NULL,                        // Authentication info
    EOAC_NONE,                   // Additional capabilities of the client or server
    NULL);                       // Reserved

if (FAILED(hres))
{
   cout << "Failed to initialize security. Error code = 0x"
        << hex << hres << endl;
   CoUninitialize();
   return 1;                  // Program has failed.
}


IWbemLocator * pIWbemLocator = NULL;
IWbemServices * pWbemServices = NULL;
IEnumWbemClassObject * pEnumObject = NULL;
BSTR bstrNamespace = (L"root\\CIMV2");
HRESULT hRes = CoCreateInstance (
  CLSID_WbemAdministrativeLocator,
  NULL ,
  CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER ,
  IID_IUnknown ,
  ( void ** ) & pIWbemLocator
  ) ;
if (SUCCEEDED(hRes))
{
  printf("\ncreated instance");
  hRes = pIWbemLocator->ConnectServer(
  bstrNamespace, // Namespace
  NULL, // Userid
  NULL, // PW
  NULL, // Locale
  0, // flags
  NULL, // Authority
  NULL, // Context
  &pWbemServices
  );

  if (SUCCEEDED(hRes))
        printf ("\nconnected to server");
  else printf ("\ncould not connect to server");
}
BSTR strQuery = (L"SELECT * FROM Win32_USBControllerDevice");
BSTR strQL = (L"WQL");
hRes = pWbemServices->ExecQuery(strQL, strQuery,
  WBEM_FLAG_RETURN_IMMEDIATELY,NULL,&pEnumObject);

if (SUCCEEDED(hRes))
        printf ("\nqueried");
  else printf ("\ncould not query");


ULONG uCount = 1, uReturned;
IWbemClassObject * pClassObject ;
hRes = pEnumObject->Reset();

if (SUCCEEDED(hRes))
        printf ("\nreset found");
  else printf ("\ncould not reset object");

hRes = pEnumObject->Next(WBEM_INFINITE,uCount, &pClassObject, &uReturned);
if (SUCCEEDED(hRes))
        printf ("\nobject found");
  else
  {
        IErrorInfo *p;
        GetErrorInfo(0, &p);
        BSTR sd;
        p->GetDescription(&sd);
        printf ("\ncould not find object %d %s", uReturned, sd );
  }

VARIANT v;
BSTR strClassProp = SysAllocString(L"DeviceID");
hRes = pClassObject->Get(strClassProp, 0, &v, 0, 0);
SysFreeString(strClassProp);

_bstr_t bstrPath = &v; //Just to convert BSTR to ANSI
char* strPath=(char*)bstrPath;
if (SUCCEEDED(hRes))
AfxMessageBox(strPath);
else
AfxMessageBox("Error in getting object");
VariantClear( &v );
pIWbemLocator->Release();
pWbemServices->Release();
pEnumObject->Release();
pClassObject->Release();
CoUninitialize();

return  0;
}
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
ppk1981Commented:
Hi,

Have you checked this link...

http://www.experts-exchange.com/Programming/Programming_Languages/Cplusplus/Q_20932764.html
looking forward....

cheers
praveen.
0
 
zhouadelCommented:
bkfirebird. Can you give more details about how to get your code fragment to work? Such as header's to include. Files to link to. etc.
Does it also work for Linux?
0
 
ashroboAuthor Commented:
Hi,
I have solved this problem by myself but I would like to reduce the points to 200 to reward bkfirebird for his/her help. Thanks!

-ashrobo
0
 
ashroboAuthor Commented:
this is the example that i followed to solve the problem..

http://www.intel.com/intelpress/usb/examples/DUSBVC.PDF

Hope this helps! And yes, I would still like to award bkfirebird, what should i do?

Thanks Tincho.

-ashrobo
0
 
DarthModCommented:
PAQed, with points refunded (0)

DarthMod
Community Support Moderator
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now