f



Scanning USB devices and reading USB descriptors

Hi,

I'm wondering how I can scan the system for USB devices and read off
the USB descriptors such as VID/PID/serial number in C? I have an
external USB HDD that I wish to access, and I'm currently able to do
so with DeviceIoControl by addressing it as PhysicalDriveX, which will
return me information about the device, except it only does it for the
attached fixed disk, rather than the USB device.

Thanks!

0
goister (139)
5/10/2007 9:43:51 AM
comp.lang.c 30657 articles. 3 followers. spinoza1111 (3246) is leader. Post Follow

13 Replies
640 Views

Similar Articles

[PageSpeed] 48

galapogos wrote:
> Hi,
Hi,

> 
> I'm wondering how I can scan the system for USB devices and read off
> the USB descriptors such as VID/PID/serial number in C? I have an
> external USB HDD that I wish to access, and I'm currently able to do
> so with DeviceIoControl by addressing it as PhysicalDriveX, which will
> return me information about the device, except it only does it for the
> attached fixed disk, rather than the USB device.

While this is not possible in plain standard C, there are certainly a
few libraries that you can use to access USB devices.
One of those is libusb [1], which is available on quite a few Unix-like
operating system.

[1] http://libusb.sourceforge.net/

> 
> Thanks!
> 


-- 
Pietro Cerutti

PGP Public Key ID:
http://gahr.ch/pgp
0
gahr (153)
5/10/2007 9:55:59 AM
On May 10, 5:55 pm, Pietro Cerutti <g...@gahr.ch> wrote:
> galapogos wrote:
> > Hi,
>
> Hi,
>
>
>
> > I'm wondering how I can scan the system for USB devices and read off
> > the USB descriptors such as VID/PID/serial number in C? I have an
> > external USB HDD that I wish to access, and I'm currently able to do
> > so with DeviceIoControl by addressing it as PhysicalDriveX, which will
> > return me information about the device, except it only does it for the
> > attached fixed disk, rather than the USB device.
>
> While this is not possible in plain standard C, there are certainly a
> few libraries that you can use to access USB devices.
> One of those is libusb [1], which is available on quite a few Unix-like
> operating system.
>
> [1]http://libusb.sourceforge.net/
>
>
>
> > Thanks!
>
> --
> Pietro Cerutti
>
> PGP Public Key ID:http://gahr.ch/pgp

Thanks. I actually have libusb-win32. I wasn't aware the source code
was included. I'll check it out.

I also checked out MSDN, specifically the "USB Structures and
Enumerations", and several of the functions all depend on usbdlib.h,
but despite copying the file over to my project from libusb I can't
seem to use the functions. How exactly do I use the libusb source
files?

0
goister (139)
5/10/2007 10:10:38 AM
galapogos wrote:
> On May 10, 5:55 pm, Pietro Cerutti <g...@gahr.ch> wrote:
>> galapogos wrote:
>>> Hi,
>> Hi,
>>
>>
>>
>>> I'm wondering how I can scan the system for USB devices and read off
>>> the USB descriptors such as VID/PID/serial number in C? I have an
>>> external USB HDD that I wish to access, and I'm currently able to do
>>> so with DeviceIoControl by addressing it as PhysicalDriveX, which will
>>> return me information about the device, except it only does it for the
>>> attached fixed disk, rather than the USB device.
>> While this is not possible in plain standard C, there are certainly a
>> few libraries that you can use to access USB devices.
>> One of those is libusb [1], which is available on quite a few Unix-like
>> operating system.
>>
>> [1]http://libusb.sourceforge.net/
>>
>>
>>
>>> Thanks!
>> --
>> Pietro Cerutti
>>
>> PGP Public Key ID:http://gahr.ch/pgp
> 
> Thanks. I actually have libusb-win32. I wasn't aware the source code
> was included. I'll check it out.
> 
> I also checked out MSDN, specifically the "USB Structures and
> Enumerations", and several of the functions all depend on usbdlib.h,
> but despite copying the file over to my project from libusb I can't
> seem to use the functions. How exactly do I use the libusb source
> files?

No clue. You better ask to a more specific newsgroup, since your issue
isn't related to the C programming language, but rather to the way you
access some third-party library function.

Please check the libusb website, they surely have documentation on their
product.


-- 
Pietro Cerutti

PGP Public Key ID:
http://gahr.ch/pgp
0
gahr (153)
5/10/2007 10:18:49 AM
galapogos wrote:
> Hi,
> 
> I'm wondering how I can scan the system for USB devices and read off
> the USB descriptors such as VID/PID/serial number in C? I have an
> external USB HDD that I wish to access, and I'm currently able to do
> so with DeviceIoControl by addressing it as PhysicalDriveX, which will
> return me information about the device, except it only does it for the
> attached fixed disk, rather than the USB device.

Use an SetupDi enumeration for DISKs, you find your drive by
its DeviceNumber. The USB device is the disk's parent device.

I've shown this in a sample:
http://www.codeproject.com/system/RemoveDriveByLetter.asp

In the demo project there are some comment lines about
the USB serial.


Greetings from Germany

Uwe
0
Uwe
5/10/2007 10:27:33 AM
On May 10, 6:27 pm, Uwe Sieber <m...@uwe-sieber.de> wrote:
> galapogos wrote:
> > Hi,
>
> > I'm wondering how I can scan the system for USB devices and read off
> > the USB descriptors such as VID/PID/serial number in C? I have an
> > external USB HDD that I wish to access, and I'm currently able to do
> > so with DeviceIoControl by addressing it as PhysicalDriveX, which will
> > return me information about the device, except it only does it for the
> > attached fixed disk, rather than the USB device.
>
> Use an SetupDi enumeration for DISKs, you find your drive by
> its DeviceNumber. The USB device is the disk's parent device.
>
> I've shown this in a sample:http://www.codeproject.com/system/RemoveDriveByLetter.asp
>
> In the demo project there are some comment lines about
> the USB serial.
>
> Greetings from Germany
>
> Uwe

Hi Uwe,
Thanks for the link. I'll try it out tomorrow, but I've taken a brief
look at it. It seems to support volumes and disks. For my application,
the drive will be a fixed disk in a USB enclosure, and it will be
uninitialized/unpartitioned/unformatted. I'm accessing it RAW using
ReadFile() and WriteFile(), so it will be accessed with "\\.
\PhysicalDriveX" rather than "\\.\X:". Will that change anything? Or
will it only eliminate the volume level(child of the disk)?

0
galapogos
5/10/2007 2:09:52 PM
galapogos wrote:
> On May 10, 6:27 pm, Uwe Sieber <m...@uwe-sieber.de> wrote:
>> galapogos wrote:
>>> Hi,
>>> I'm wondering how I can scan the system for USB devices and read off
>>> the USB descriptors such as VID/PID/serial number in C? I have an
>>> external USB HDD that I wish to access, and I'm currently able to do
>>> so with DeviceIoControl by addressing it as PhysicalDriveX, which will
>>> return me information about the device, except it only does it for the
>>> attached fixed disk, rather than the USB device.
>> Use an SetupDi enumeration for DISKs, you find your drive by
>> its DeviceNumber. The USB device is the disk's parent device.
>>
>> I've shown this in a sample:http://www.codeproject.com/system/RemoveDriveByLetter.asp
>>
>> In the demo project there are some comment lines about
>> the USB serial.
>>
>> Greetings from Germany
>>
>> Uwe
> 
> Hi Uwe,
> Thanks for the link. I'll try it out tomorrow, but I've taken a brief
> look at it. It seems to support volumes and disks. For my application,
> the drive will be a fixed disk in a USB enclosure, and it will be
> uninitialized/unpartitioned/unformatted. I'm accessing it RAW using
> ReadFile() and WriteFile(), so it will be accessed with "\\.
> \PhysicalDriveX" rather than "\\.\X:". Will that change anything? Or
> will it only eliminate the volume level(child of the disk)?

This saves the step to determine the device number
of the storage volume. Obviously you know the device
number, it's the X in "\\.\PhysicalDriveX".

On fixed disks the volume is not the child device of
the disk.


Uwe

0
Uwe
5/10/2007 4:27:19 PM
galapogos wrote:
> 
> I'm wondering how I can scan the system for USB devices and read
> off the USB descriptors such as VID/PID/serial number in C? I
> have an external USB HDD that I wish to access, and I'm currently
> able to do so with DeviceIoControl by addressing it as
> PhysicalDriveX, which will return me information about the device,
> except it only does it for the attached fixed disk, rather than
> the USB device.

I see no reference to USB in the C standard.  So this is off-topic
in c.l.c.  Follow-ups set.

-- 
 <http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
 <http://www.securityfocus.com/columnists/423>
 <http://www.aaxnet.com/editor/edit043.html>
 <http://kadaitcha.cx/vista/dogsbreakfast/index.html>
                        cbfalconer at maineline dot net



-- 
Posted via a free Usenet account from http://www.teranews.com

0
cbfalconer (19194)
5/10/2007 10:54:47 PM
In article <4643A2B7.3F88A55@yahoo.com>,
CBFalconer  <cbfalconer@maineline.net> wrote:
>galapogos wrote:
>> 
>> I'm wondering how I can scan the system for USB devices and read
>> off the USB descriptors such as VID/PID/serial number in C? I
>> have an external USB HDD that I wish to access, and I'm currently
>> able to do so with DeviceIoControl by addressing it as
>> PhysicalDriveX, which will return me information about the device,
>> except it only does it for the attached fixed disk, rather than
>> the USB device.
>
>I see no reference to USB in the C standard.  So this is off-topic
>in c.l.c.  Follow-ups set.

IOW, ...

(You know the drill)

0
gazelle2 (1307)
5/11/2007 12:54:52 AM
On May 11, 12:27 am, Uwe Sieber <m...@uwe-sieber.de> wrote:
> galapogos wrote:
> > On May 10, 6:27 pm, Uwe Sieber <m...@uwe-sieber.de> wrote:
> >> galapogos wrote:
> >>> Hi,
> >>> I'm wondering how I can scan the system for USB devices and read off
> >>> the USB descriptors such as VID/PID/serial number in C? I have an
> >>> external USB HDD that I wish to access, and I'm currently able to do
> >>> so with DeviceIoControl by addressing it as PhysicalDriveX, which will
> >>> return me information about the device, except it only does it for the
> >>> attached fixed disk, rather than the USB device.
> >> Use an SetupDi enumeration for DISKs, you find your drive by
> >> its DeviceNumber. The USB device is the disk's parent device.
>
> >> I've shown this in a sample:http://www.codeproject.com/system/RemoveDriveByLetter.asp
>
> >> In the demo project there are some comment lines about
> >> the USB serial.
>
> >> Greetings from Germany
>
> >> Uwe
>
> > Hi Uwe,
> > Thanks for the link. I'll try it out tomorrow, but I've taken a brief
> > look at it. It seems to support volumes and disks. For my application,
> > the drive will be a fixed disk in a USB enclosure, and it will be
> > uninitialized/unpartitioned/unformatted. I'm accessing it RAW using
> > ReadFile() and WriteFile(), so it will be accessed with "\\.
> > \PhysicalDriveX" rather than "\\.\X:". Will that change anything? Or
> > will it only eliminate the volume level(child of the disk)?
>
> This saves the step to determine the device number
> of the storage volume. Obviously you know the device
> number, it's the X in "\\.\PhysicalDriveX".
>
> On fixed disks the volume is not the child device of
> the disk.
>
> Uwe

OK. I already have code that scans all storage devices and identifies
the device number, so the X is taken care of. Now I guess I just need
to identify the parent of the device X, which is the USB-IDE bridge,
and then obtain its USB descriptors(VID/PID/SN).

I just tried loading your project in VS7, and I'm having problem
building it. PPNP_VETO_TYPE in cfgmgr32.h is not recognized. Is there
some library I need to include? This is weird since cfgmgr32.h is from
MS.

0
galapogos
5/11/2007 1:05:51 AM
On May 11, 9:05 am, galapogos <gois...@gmail.com> wrote:
> On May 11, 12:27 am, Uwe Sieber <m...@uwe-sieber.de> wrote:
>
>
>
> > galapogos wrote:
> > > On May 10, 6:27 pm, Uwe Sieber <m...@uwe-sieber.de> wrote:
> > >> galapogos wrote:
> > >>> Hi,
> > >>> I'm wondering how I can scan the system for USB devices and read off
> > >>> the USB descriptors such as VID/PID/serial number in C? I have an
> > >>> external USB HDD that I wish to access, and I'm currently able to do
> > >>> so with DeviceIoControl by addressing it as PhysicalDriveX, which will
> > >>> return me information about the device, except it only does it for the
> > >>> attached fixed disk, rather than the USB device.
> > >> Use an SetupDi enumeration for DISKs, you find your drive by
> > >> its DeviceNumber. The USB device is the disk's parent device.
>
> > >> I've shown this in a sample:http://www.codeproject.com/system/RemoveDriveByLetter.asp
>
> > >> In the demo project there are some comment lines about
> > >> the USB serial.
>
> > >> Greetings from Germany
>
> > >> Uwe
>
> > > Hi Uwe,
> > > Thanks for the link. I'll try it out tomorrow, but I've taken a brief
> > > look at it. It seems to support volumes and disks. For my application,
> > > the drive will be a fixed disk in a USB enclosure, and it will be
> > > uninitialized/unpartitioned/unformatted. I'm accessing it RAW using
> > > ReadFile() and WriteFile(), so it will be accessed with "\\.
> > > \PhysicalDriveX" rather than "\\.\X:". Will that change anything? Or
> > > will it only eliminate the volume level(child of the disk)?
>
> > This saves the step to determine the device number
> > of the storage volume. Obviously you know the device
> > number, it's the X in "\\.\PhysicalDriveX".
>
> > On fixed disks the volume is not the child device of
> > the disk.
>
> > Uwe
>
> OK. I already have code that scans all storage devices and identifies
> the device number, so the X is taken care of. Now I guess I just need
> to identify the parent of the device X, which is the USB-IDE bridge,
> and then obtain its USB descriptors(VID/PID/SN).
>
> I just tried loading your project in VS7, and I'm having problem
> building it. PPNP_VETO_TYPE in cfgmgr32.h is not recognized. Is there
> some library I need to include? This is weird since cfgmgr32.h is from
> MS.

OK I think I might have commented out the #include <cfg.h> line in
cfgmgr32.h earlier, which is causing the syntax error. However, when I
tried uncommenting it, I get the error that cfg.h is not found. Does
anyone know where I can find it?

0
galapogos
5/11/2007 1:09:47 AM
On May 11, 12:27 am, Uwe Sieber <m...@uwe-sieber.de> wrote:
> galapogos wrote:
> > On May 10, 6:27 pm, Uwe Sieber <m...@uwe-sieber.de> wrote:
> >> galapogos wrote:
> >>> Hi,
> >>> I'm wondering how I can scan the system for USB devices and read off
> >>> the USB descriptors such as VID/PID/serial number in C? I have an
> >>> external USB HDD that I wish to access, and I'm currently able to do
> >>> so with DeviceIoControl by addressing it as PhysicalDriveX, which will
> >>> return me information about the device, except it only does it for the
> >>> attached fixed disk, rather than the USB device.
> >> Use an SetupDi enumeration for DISKs, you find your drive by
> >> its DeviceNumber. The USB device is the disk's parent device.
>
> >> I've shown this in a sample:http://www.codeproject.com/system/RemoveDriveByLetter.asp
>
> >> In the demo project there are some comment lines about
> >> the USB serial.
>
> >> Greetings from Germany
>
> >> Uwe
>
> > Hi Uwe,
> > Thanks for the link. I'll try it out tomorrow, but I've taken a brief
> > look at it. It seems to support volumes and disks. For my application,
> > the drive will be a fixed disk in a USB enclosure, and it will be
> > uninitialized/unpartitioned/unformatted. I'm accessing it RAW using
> > ReadFile() and WriteFile(), so it will be accessed with "\\.
> > \PhysicalDriveX" rather than "\\.\X:". Will that change anything? Or
> > will it only eliminate the volume level(child of the disk)?
>
> This saves the step to determine the device number
> of the storage volume. Obviously you know the device
> number, it's the X in "\\.\PhysicalDriveX".
>
> On fixed disks the volume is not the child device of
> the disk.
>
> Uwe

Hi again,

OK so I fixed the compile error and after a bit of coding, I'm able to
get the information of the USB bridge(parent of the disk) with a
CM_Get_Device_ID() call, and this includes the USB VID, PID and SN,
exactly what I need. It however stores them as a string in the format
"USB\VID_XXXX&PID_YYYY\ZZZZ", where XXXX is the VID, YYYY is the PID
and ZZZZ is the SN. Is there any function I can call to extract the 3
fields into separate buffers, or must I write the function myself?

Thanks!

0
galapogos
5/11/2007 3:09:20 AM
galapogos wrote:
> On May 11, 12:27 am, Uwe Sieber <m...@uwe-sieber.de> wrote:
>> galapogos wrote:
>>> On May 10, 6:27 pm, Uwe Sieber <m...@uwe-sieber.de> wrote:
>>>> galapogos wrote:
>>>>> Hi,
>>>>> I'm wondering how I can scan the system for USB devices and read off
>>>>> the USB descriptors such as VID/PID/serial number in C? I have an
>>>>> external USB HDD that I wish to access, and I'm currently able to do
>>>>> so with DeviceIoControl by addressing it as PhysicalDriveX, which will
>>>>> return me information about the device, except it only does it for the
>>>>> attached fixed disk, rather than the USB device.
>>>> Use an SetupDi enumeration for DISKs, you find your drive by
>>>> its DeviceNumber. The USB device is the disk's parent device.
>>>> I've shown this in a sample:http://www.codeproject.com/system/RemoveDriveByLetter.asp
>>>> In the demo project there are some comment lines about
>>>> the USB serial.
>>>> Greetings from Germany
>>>> Uwe
>>> Hi Uwe,
>>> Thanks for the link. I'll try it out tomorrow, but I've taken a brief
>>> look at it. It seems to support volumes and disks. For my application,
>>> the drive will be a fixed disk in a USB enclosure, and it will be
>>> uninitialized/unpartitioned/unformatted. I'm accessing it RAW using
>>> ReadFile() and WriteFile(), so it will be accessed with "\\.
>>> \PhysicalDriveX" rather than "\\.\X:". Will that change anything? Or
>>> will it only eliminate the volume level(child of the disk)?
>> This saves the step to determine the device number
>> of the storage volume. Obviously you know the device
>> number, it's the X in "\\.\PhysicalDriveX".
>>
>> On fixed disks the volume is not the child device of
>> the disk.
>>
>> Uwe
> 
> Hi again,
> 
> OK so I fixed the compile error and after a bit of coding, I'm able to
> get the information of the USB bridge(parent of the disk) with a
> CM_Get_Device_ID() call, and this includes the USB VID, PID and SN,
> exactly what I need. It however stores them as a string in the format
> "USB\VID_XXXX&PID_YYYY\ZZZZ", where XXXX is the VID, YYYY is the PID
> and ZZZZ is the SN. Is there any function I can call to extract the 3
> fields into separate buffers, or must I write the function myself?

You must write it yourself.


Uwe
0
Uwe
5/11/2007 7:10:37 AM
Take a look at the following article... it's in VB.Net (but the
concepts are the same).   There is also a C# demonstration source code
project that is version of the USBView application.

http://home.hot.rr.com/graye/Articles/USB_SerialNumbers.htm


0
egray1
5/15/2007 10:14:08 PM
Reply: