WMI方式检测:
wmi = WIN32OLE.connect("winmgmts://")
devices = wmi.ExecQuery("Select * From Win32_USBControllerDevice")
devices.each do |device|
device_name = device.Dependent.gsub('"', '').split('=')[1]
usb_devices = wmi.ExecQuery("Select * From Win32_PnPEntity Where DeviceID = '#{device_name}'")
for usb_device in usb_devices do
puts usb_device.Description
if usb_device.Description == 'USB Mass Storage Device'&&如是扫描枪的话应该是“XXXX SCANNER”之类
# DO SOMETHING HERE
end
end
end
wmi = WIN32OLE.connect("winmgmts://")
devices = wmi.ExecQuery("Select * From Win32_USBControllerDevice")
devices.each do |device|
device_name = device.Dependent.gsub('"', '').split('=')[1]
usb_devices = wmi.ExecQuery("Select * From Win32_PnPEntity Where DeviceID = '#{device_name}'")
for usb_device in usb_devices do
puts usb_device.Description
if usb_device.Description == 'USB Mass Storage Device'&&如是扫描枪的话应该是“XXXX SCANNER”之类
# DO SOMETHING HERE
end
end
end