1. 程式人生 > >叠代所有連接的 USB 硬件設備

叠代所有連接的 USB 硬件設備

disk %s 配對 argc driver ucc with str gdi

// // main.c // DriverIterator // #include <CoreFoundation/CoreFoundation.h> #include <IOKit/IOKitLib.h> int main (int argc, const char * argv[]) { CFDictionaryRef matchingDict = NULL; io_iterator_t iter = 0; io_service_t service = 0; kern_return_t kr; //創建一個匹配字典,查找任意的 USB 設備 matchingDict = IOServiceMatching("IOUSBDevice"); //為匹配該字典的所有 I/O Registry 對象創建一個叠代對象 kr = IOServiceGetMatchingServices(kIOMasterPortDefault, matchingDict, &iter); if (kr != KERN_SUCCESS) return -1; //叠代所有的匹配對象 while ((service = IOIteratorNext(iter)) != 0) { CFStringRef className; io_name_t name; //列出所有的IOUSBDevice對象 className = IOObjectCopyClass(service); if (CFEqual(className, CFSTR("IOUSBDevice")) == true) { IORegistryEntryGetName(service, name); printf("Found device with name: %s\n", name); } CFRelease(className); IOObjectRelease(service); } //釋放叠代器 IOObjectRelease(iter); return 0; }
liuhailong:~ liuhailong$ /Users/liuhailong/Library/Developer/Xcode/DerivedData/DriverIterator-hddbxkfinzzofxbrorottekflawo/Build/Products/Debug/DriverIterator 
Found device with name: BRCM20702 Hub
Found device with name: Bluetooth USB Host Controller
Found device with name: USB DISK
Found device with name: IOUSBHostDevice
liuhailong:~ liuhailong$ 

叠代所有連接的 USB 硬件設備