2017-07-14 4 views
0

J'essaie de parcourir les périphériques USB pour trouver le stockage de masse USB et obtenir PID et VID. Pour cela, j'essaie d'obtenir une référence sur IOUSBDeviceInterface, mais IOCreatePlugInInterfaceForService échoue avec un code d'erreur étrange: 0x2C7 - "fonction non prise en charge". Quelqu'un pourrait-il dire, qu'est-ce que je fais de mal? Voici mon code:Obtient des informations sur les périphériques USB (OSX, C++), IOCreatePlugInInterfaceForService échoue

#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; 
} 

Répondre

1

Vous IOUSBDevice correspondant à des services, mais en essayant de se connecter avec un IOUSBInterfaceUserClient. Si vous souhaitez vous connecter à un service IOUSBDevice, le type de client utilisateur doit être kIOUSBDeviceUserClientTypeID. Si vous voulez un IOUSBInterfaceUserClient, vous devez faire correspondre IOUSBInterface services.

+0

Oui, en effet! Une erreur stupide du mien, merci beaucoup! –