get GUID for a device for which I know BUS / VID / and PID

gelonida
gelonida used Ask the Experts™
on
Hi,

I'd like to know whether it is possible to directly find a device's GUID
if I know the BUS / VID and PID  (or BUS VEN / DEV) of it


The only option that I have so far is to retrieve all devices withWMI (in my case only usb network devices), get their PNPDeviceID and parse for it.

Is there anything faster? WMI is rather slow and my code doesn't really seem elegant.
Are there options using directly the win32 module?

My current code works for python2 and python3, but as mentioned above.
I manually search through all network devices and parse the PNPDeviceID
my code:

import wmi

c = wmi.WMI()

def find_by(bus, vid, pid):
    nw_adapters = (
        c.Win32_NetworkAdapter(AdapterTypeId=0)
        + c.Win32_NetworkAdapter(AdapterTypeId=9)
        )
    pnp_string_start = r"%s\VID_%s&PID_%s" % (bus, vid, pid)
    for device in nw_adapters:
        if device.PNPDeviceID.startswith(pnp_string_start):
            return device
    return None


def get_guid(bus, vid, pid):
    device = find_by(bus, vid, pid)
    return None if device is None else device.GUID

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Distinguished Expert 2018
Commented:
Just a guess: might be faster to use devcon.exe - download it and use it like this:
devcon findall * | findstr yourvendoridORdeviceID

Open in new window

See if you can make something out of the findings and if it is indeed faster.
Hi McKnife

Will try this and see whether there is some big performance improvement.
However for many projects this could only be a fallback (falling back to WMI in case devcon cannot be found)

At the moment I create depending on the project:
either source code published on
- pypi
- github
and one only has to type
pip install -r requirements.txt

Open in new window


or I create one file with   py2exe / pyinstaller
and extracting it and running the .exe is enough.

Not sure about whether / how devcon can be distributed (autoinstalled for such cases)
Might open a legal can of worms.
Distinguished Expert 2018
Commented:
Devcon is a single executable (from microsoft) and as such, can be easily deployed.
How to Generate Services Revenue the Easiest Way

This Tuesday! Learn key insights about modern cyber protection services & gain practical strategies to skyrocket business:

- What it takes to build a cloud service portfolio
- How to determine which services will help your unique business grow
- Various use-cases and examples

OK I see.
I must have mixed up the licensing with another tool.

What I'm confused about now is:
https://docs.microsoft.com/en-us/windows-hardware/drivers/devtest/devcon seems to state, that the way to download is:

Download  Windows Driver Kit (WDK) 8 and Windows Driver Kit (WDK) 8.1

Will try it out and keep you updated
Distinguished Expert 2018

Commented:
I can provide the executable. rename it from .doc to .exe
This is the x64 version.
devcon.doc
Hi McKnife. Thanks a lot.
Already installing the SDK (without visual studio).
and it finished now.

file can be found after installation on

C:\Program Files (x86)\Windows Kits\10\Tools\x64\devcon.exe # for the 64 bit version

Open in new window

and
C:\Program Files (x86)\Windows Kits\10\Tools\x86\devcon.exe # for the 32 bit version

Open in new window


Thanks for your help. Will play with devcon

I'll leave this question still open a little longer.

Perhaps there is (though I'm afraid there's not) a win32 / ctypes / cffi  solution, that's a little faster than WMI and that doesn't require subprocess calls
OK with
devcon findall *
devcon listclass net

Open in new window

I get a list of all PNP ids and can search.

list of classes at docs.microsoft.com

But now back to my question: how to display now the GUID of the device?
Distinguished Expert 2018

Commented:
Good question. So far, I see only ways to retrieve the hardware ID, but not device ID, sorry.
Is there really no better / faster solution than what I suggested in my initial question:

import wmi

c = wmi.WMI()

def find_by(bus, vid, pid):
    nw_adapters = (
        c.Win32_NetworkAdapter(AdapterTypeId=0)
        + c.Win32_NetworkAdapter(AdapterTypeId=9)
        )
    pnp_string_start = r"%s\VID_%s&PID_%s" % (bus, vid, pid)
    for device in nw_adapters:
        if device.PNPDeviceID.startswith(pnp_string_start):
            return device
    return None


def get_guid(bus, vid, pid):
    device = find_by(bus, vid, pid)
    return None if device is None else device.GUID

Open in new window


Devcon seems faster, but I didn't find an option to retrieve the GUID.
Distinguished Expert 2018

Commented:
I have no other idea, sorry.
Thanks a lot for your help. learned about devcon. Might come in handy another time.
Will continue with slow WMI solution.
devcon is faster but can't return a GUID (at least I didn't find out how)
thanks

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial