Solved

detect usb device and retrieve device descriptor and drive letter

Posted on 2004-04-10
14
3,440 Views
Last Modified: 2007-12-19
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
Comment
Question by:ashrobo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
14 Comments
 
LVL 4

Expert Comment

by:bkfirebird
ID: 10800130
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
 

Author Comment

by:ashrobo
ID: 10800337
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
 
LVL 4

Expert Comment

by:bkfirebird
ID: 10804028
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Expert Comment

by:ppk1981
ID: 10804181
Hi,

Have you checked this link...

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

cheers
praveen.
0
 

Expert Comment

by:zhouadel
ID: 10845855
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
 

Author Comment

by:ashrobo
ID: 11060636
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
 

Author Comment

by:ashrobo
ID: 11060816
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
 
LVL 1

Accepted Solution

by:
DarthMod earned 0 total points
ID: 11400841
PAQed, with points refunded (0)

DarthMod
Community Support Moderator
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

687 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question