Link to home
Start Free TrialLog in
Avatar of gelonida
gelonidaFlag for France

asked on

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

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

SOLUTION
Avatar of McKnife
McKnife
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gelonida

ASKER

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.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
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?
Good question. So far, I see only ways to retrieve the hardware ID, but not device ID, sorry.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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