Solved

detect usb device and retrieve device descriptor and drive letter

Posted on 2004-04-10
14
3,403 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
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Would like to move button in a function 3 77
is twain_32.dll cmpatible with windows 10 ? 10 163
c++ reading data from file into two dimensional array 3 110
Issues with C++ Class 19 93
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

832 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