2017-07-14 158 views
0

我試圖遍歷USB設備來查找USB大容量存儲並獲取PID和VID。爲此,我試圖獲得有關IOUSBDeviceInterface的參考,但IOCreatePlugInInterfaceForService失敗,出現一個奇怪的錯誤代碼:0x2C7 - 「不支持的函數」。有人可以告訴我,我做錯了什麼?這裏是我的代碼:獲取有關USB設備(OSX,C++)的信息,IOCreatePlugInInterfaceForService失敗

#include <iostream> 
#include <IOKit/IOkitLib.h> 
#include <IOKit/usb/IOUSBLib.h> 
#include <IOKit/IOCFPlugIn.h> 
#include <IOKit/usb/USBSpec.h> 
#include <CoreFoundation/CoreFoundation.h> 

int main(int argc, const char * argv[]) 
{ 
CFMutableDictionaryRef matchingDictionary = NULL; 
io_iterator_t foundIterator = 0; 
io_service_t usbDevice; 
matchingDictionary = IOServiceMatching(kIOUSBDeviceClassName); 

IOServiceGetMatchingServices(kIOMasterPortDefault, matchingDictionary, &foundIterator); 

for(usbDevice = IOIteratorNext(foundIterator); usbDevice; usbDevice = IOIteratorNext(foundIterator)) 
{ 
    IOCFPlugInInterface** plugin = NULL; 
    SInt32 theScore=0; 
    IOReturn err; 
    err = IOCreatePlugInInterfaceForService(usbDevice, kIOUSBInterfaceUserClientTypeID, kIOCFPlugInInterfaceID, &plugin, &theScore); 
    if (err!= 0){ 
     //for all the devices (including Mass Storage), I get the same 
     //error: system 0x38 (IOKit), code: 0x2C7 (unsupported function) 
     std::cout<<"error, error code: "<<err_get_code(err) <<std::endl; 
    } 
    else if (plugin && *plugin) 
    { 
     //never happens 
     IOUSBDeviceInterface** usbInterface = NULL; 
     (*plugin)->QueryInterface(plugin, CFUUIDGetUUIDBytes(kIOUSBDeviceInterfaceID),(LPVOID*)&usbInterface); 
     (*plugin)->Release(plugin); 
     if (usbInterface && *usbInterface) 
     { 
      //other actions with usbInterface 
     }  

    } 

} 
IOObjectRelease(foundIterator); 
return 0; 
} 

回答

1

你匹配IOUSBDevice服務,而是嘗試用IOUSBInterfaceUserClient連接。如果要連接到IOUSBDevice服務,則用戶客戶端類型必須爲kIOUSBDeviceUserClientTypeID。如果你想要一個IOUSBInterfaceUserClient,你需要匹配IOUSBInterface服務。

+0

是的,確實!我的一個愚蠢的錯誤,非常感謝你! –