How do I get all the smart card readers on my system via WMI?

旧巷老猫 提交于 2019-12-08 06:24:31

问题


I want to get the DeviceID and Service of all PCSC smart card readers on my system using WMI. They're all connected via USB, so I query the WMI registry for all Win32_PnPEntitys. But I have no clue how to determine which devices are 'card readers'. Here's what I already have:

ManagementObjectSearcher mos =
new ManagementObjectSearcher(@"\root\cimv2", @"Select * From Win32_PnPEntity");

ManagementObjectCollection mob = mos.Get();

foreach (ManagementObject mo in mob)
{
    Console.WriteLine("DeviceID: " + mo["DeviceID"].ToString());
    Console.WriteLine("Service: " + mo["Service"].ToString());
}

I can't just filter on the device name, there's different brands/models of readers, and there's no common denominator. In the Device Manager they're all grouped under 'smart card readers', so there must be a(nother) way.


回答1:


I found the Device Class GUID on MSDN: {50dd5230-ba8a-11d1-bf5d-0000f805f530}

Smart Card Readers
Class = SmartCardReader
ClassGuid = {50dd5230-ba8a-11d1-bf5d-0000f805f530}
This class includes smart card readers.

So finally I came up with this:

ManagementObjectSearcher mos = new ManagementObjectSearcher(@"\root\cimv2",
@"SELECT* FROM Win32_PnPEntity WHERE ClassGuid = '{50DD5230-BA8A-11D1-BF5D-0000F805F530}'");

Which seems to give me what I want :)




回答2:


I don't have any smart card readers here, so this is theoretical:

  • What is the actual type (__CLASS property) of the returned instances. If a subtype, maybe looking for further instances of that type will help).

  • What associations exist for the smart card devices:

    associators of {__RELPATH}
    

    where __RELPATH is the same named property from the Win32_PnPEntity instance. Also include associators of the associators.

    Note that most associated instances will represent things like the USB host/hub device, but others can be other aspects of the device (like a HDD will have both a Win32_PhysicalDisk instance in addition to a Win32_PnPEntity).

Also, to speed exploring the WMI types and objects I would suggest one of the console or GUI tools, this will be much easier than writing C# code. Eg. in PowerShell:

gwmi -query "select * from Win32_PnpEntity" | ft -autosize __RELPATH, DeviceID

easier to work with than the C# code (however it will need a very wide console window :-)).



来源:https://stackoverflow.com/questions/10812729/how-do-i-get-all-the-smart-card-readers-on-my-system-via-wmi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!