How to list USB devices on Windows?

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

How to list USB devices on Windows?

Bo Berglund
I am writing a class that interfaces to a specific USB device HID type
and I would like to list all such devices currently connected to the
PC (usin Windows).

On Linux I can do this on the command line:

$ lsusb
Bus 001 Device 004: ID 07f2:0003 Microcomputer Applications, Inc.
Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp.
SMSC9512/9514 Fast Ethernet Adapter
Bus 001 Device 002: ID 0424:9514 Standard Microsystems Corp.
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

This gets me as the ID the vendorid:productid so I can scan for the
interesting device type:

$ lsusb | grep 07f2:
Bus 001 Device 004: ID 07f2:0003 Microcomputer Applications, Inc.

Is there some way I can do this on Windows so I can create a list of
the device type I work with?
Or is there an FPC command already?

--
Bo Berglund
Developer in Sweden

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: How to list USB devices on Windows?

R0b0t1
On Wed, Jun 27, 2018 at 12:32 AM, Bo Berglund <[hidden email]> wrote:

> I am writing a class that interfaces to a specific USB device HID type
> and I would like to list all such devices currently connected to the
> PC (usin Windows).
>
> On Linux I can do this on the command line:
>
> $ lsusb
> Bus 001 Device 004: ID 07f2:0003 Microcomputer Applications, Inc.
> Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp.
> SMSC9512/9514 Fast Ethernet Adapter
> Bus 001 Device 002: ID 0424:9514 Standard Microsystems Corp.
> Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
>
> This gets me as the ID the vendorid:productid so I can scan for the
> interesting device type:
>
> $ lsusb | grep 07f2:
> Bus 001 Device 004: ID 07f2:0003 Microcomputer Applications, Inc.
>
> Is there some way I can do this on Windows so I can create a list of
> the device type I work with?
> Or is there an FPC command already?
>

I don't know if there exists any functions to do it in FPC, but the
function (C++) below is the start of what you want. As is it uses the
SetupAPI to return a vector of paths of a device type which could each
be given to CreateFile, generating a handle to interact with the
device using DeviceIoControl.

In your case you should call it with GUID_DEVINTERFACE_USB_DEVICE.
Refer to https://msdn.microsoft.com/en-us/library/windows/hardware/ff553412(v=vs.85).aspx
for the GUIDs of other device types. You may want to consider
GUID_DEVINTERFACE_HID. Look at initguid.h and usbiodef.h.

For USB devices the returned paths will contain the VID and PID in a
regular manner so you could parse them out. In general you likely want
to use the following functions:

1) SetupDiEnumDeviceInfo
2) SetupDiGetDevicePropertyKeys
3) SetupDiGetDevicePropery
4) SetupDiGetDeviceInterfacePropertyKeys
5) SetupDiGetDeviceInterfaceProperty

("Interfaces" are instances of a device on a physical link type and a
"class" or device is an instance of a device type which serves some
purpose. Some class GUIDs are obsolete.)

You should also reference the full set at
https://docs.microsoft.com/en-us/windows/desktop/devinst/setupapi-h.
Realize the SetupAPI was made to programmatically install drivers. It
may or may not contain the information you want. In my experience it
always has, but you may need to resort to querying a device with
IOCTLs (in your case USB hubs).

---

vector<wstring> enum_devices(
    _In_    const GUID Guid
)
{
    vector<wstring> r;

    int index = 0;
    HDEVINFO hDevInfo = SetupDiGetClassDevs(&Guid, NULL, NULL,
DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);

    SP_DEVINFO_DATA DevInfoData;
    memset(&DevInfoData, 0, sizeof(SP_DEVINFO_DATA));
    DevInfoData.cbSize = sizeof(SP_DEVINFO_DATA);

    while (SetupDiEnumDeviceInfo(hDevInfo, index, &DevInfoData)) {
        index++;

        int jndex = 0;
        SP_DEVICE_INTERFACE_DATA DevIntData;
        memset(&DevIntData, 0, sizeof(SP_DEVICE_INTERFACE_DATA));
        DevIntData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);

        while (SetupDiEnumDeviceInterfaces(
            hDevInfo,
            &DevInfoData, &Guid, jndex, &DevIntData
        )) {
            jndex++;

            // Get the size required for the structure.
            DWORD RequiredSize;
            SetupDiGetDeviceInterfaceDetail(
                hDevInfo, &DevIntData, NULL, NULL, &RequiredSize, NULL
            );

            PSP_DEVICE_INTERFACE_DETAIL_DATA pDevIntDetData =
(PSP_DEVICE_INTERFACE_DETAIL_DATA)malloc(
                sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA) + RequiredSize
            );
            memset(pDevIntDetData, 0,
sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA) + RequiredSize);
            pDevIntDetData->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);

            SetupDiGetDeviceInterfaceDetail(
                hDevInfo,
                &DevIntData,
                pDevIntDetData, RequiredSize,
                NULL,
                &DevInfoData
            );

            r.push_back(wstring(pDevIntDetData->DevicePath));
            free(pDevIntDetData);
        }
    }

    return r;
}

---

Cheers,
     R0b0t1
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: How to list USB devices on Windows?

Marc Santhoff-2
In reply to this post by Bo Berglund
On Wed, 2018-06-27 at 07:32 +0200, Bo Berglund wrote:
> I am writing a class that interfaces to a specific USB device HID type
> and I would like to list all such devices currently connected to the
> PC (usin Windows).
>
[...]
> Is there some way I can do this on Windows so I can create a list of
> the device type I work with?
> Or is there an FPC command already?

I have some code in Object Pascal doing such stuff. The problem with it is, it
has no copyright string or author anywhere. I even don't know where it is
from, it's one of the pieces of code collected when stumbled upon, thinking
"this will be useful later"...

I can send it in private or post it here on the list or on pastebin. Maybe the
author can be identified?

I hesitate calling it public domain, but the code snippets I collect are
exactly that, visible for everyone, from postings on forums and the like.

--
Marc Santhoff <[hidden email]>
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: How to list USB devices on Windows?

Marco van de Voort
In our previous episode, Marc Santhoff said:

> > Or is there an FPC command already?
>
> I have some code in Object Pascal doing such stuff. The problem with it is, it
> has no copyright string or author anywhere. I even don't know where it is
> from, it's one of the pieces of code collected when stumbled upon, thinking
> "this will be useful later"...
>
> I can send it in private or post it here on the list or on pastebin. Maybe the
> author can be identified?
>
> I hesitate calling it public domain, but the code snippets I collect are
> exactly that, visible for everyone, from postings on forums and the like.

JVCL also has such code. I use it to get descriptive names for usb serials.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: How to list USB devices on Windows?

José Mejuto
El 28/06/2018 a las 13:44, Marco van de Voort escribió:

>> I hesitate calling it public domain, but the code snippets I collect are
>> exactly that, visible for everyone, from postings on forums and the like.
>
> JVCL also has such code. I use it to get descriptive names for usb serials.

Hello,

Maybe this code may help browsing USB HIDs across platforms:

https://github.com/JoshyFun/VUSBRelayPascal


--

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal