1
votes

I managed to extract the device path of all connected USB devices on my computer using SetupDiGetDeviceInterfaceDetail. They usually look like the following:

\\?\hid#vid_0461&pid_4d15#6&219e7220&1&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}

Given this, what's the cleanest way of extracting vid (0461), pid (4d15) and serial number (6&219e7220&1&0000)?

I thought about using IndexOf(vid_) and reading 4 characters from there (similarly for others as well), but this solution looks pretty dirty.

Or is there a known Win32 API that extracts that information nicely without having to parse that info?

2

2 Answers

1
votes

I think I heard somewhere on StackOverflow that the device instance path is supposed to be an opaque string and parsing it is not recommended. Instead, you should look at the device's Hardware IDs. Sorry this isn't a complete answer but it should get you looking in the right places.

0
votes

You can use RegularExpressions. For example to extract vid, pid and serial number:

string input ="\\?\hid#vid_0461&pid_4d15#6&219e7220&1&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}";
Regex rx = new Regex(@"_[0-9a-fA-F]+|\{[0-9a-fA-F\-]+\}");
MatchCollection matches = rx.Matches(input);

for (int i = 0; i< matches.Count; i++){
        console.writeln(matches[i].Value);
}