Solved

detect usb device and retrieve device descriptor and drive letter

Posted on 2004-04-10
14
3,387 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
14 Comments
 
LVL 4

Expert Comment

by:bkfirebird
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 1

Expert Comment

by:ppk1981
Comment Utility
Hi,

Have you checked this link...

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

cheers
praveen.
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Expert Comment

by:zhouadel
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
PAQed, with points refunded (0)

DarthMod
Community Support Moderator
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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 video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

771 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now