代码之家  ›  专栏  ›  技术社区  ›  Anton Pilyak

获取有关usb设备(OSX、C++)的信息,IOCreatePlugInInterfaceForService失败

  •  0
  • Anton Pilyak  · 技术社区  · 8 年前

    #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 回复  |  直到 8 年前
        1
  •  1
  •   pmdj    8 年前

    你正在匹配 IOUSBDevice 服务,但正在尝试与 IOUSBInterfaceUserClient 。如果要连接到 IousB设备 服务,用户客户端类型必须为 kIOUSBDeviceUserClientTypeID .如果你想要 IOUSBInterface