【问题标题】:get information on usb devices (OSX, C++), IOCreatePlugInInterfaceForService fails获取有关 USB 设备(OSX、C++)的信息,IOCreatePlugInInterfaceForService 失败
【发布时间】:2017-07-14 06:51:12
【问题描述】:

我正在尝试遍历 USB 设备以查找 USB 大容量存储并获取 PID 和 VID。为此,我试图在 IOUSBDeviceInterface 上获得参考,但 IOCreatePlugInInterfaceForService 失败并出现奇怪的错误代码:0x2C7 - “不支持的功能”。有人可以告诉我,我做错了什么吗?这是我的代码:

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

【问题讨论】:

    标签: macos usb iokit


    【解决方案1】:

    您正在匹配IOUSBDevice 服务,但尝试连接IOUSBInterfaceUserClient。如果要连接到IOUSBDevice 服务,用户客户端类型必须是kIOUSBDeviceUserClientTypeID。如果你想要一个IOUSBInterfaceUserClient,你需要匹配IOUSBInterface服务。

    【讨论】:

    • 是的,确实如此!我的一个愚蠢的错误,非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-21
    • 1970-01-01
    • 1970-01-01
    • 2018-09-26
    • 1970-01-01
    相关资源
    最近更新 更多