具有相同供应商ID和不同产品ID的Mac OS X通知

时间:2018-12-10 03:22:51

标签: macos cocoa usb foundation iokit

从stackfow链接尝试了此解决方案:

CFMutableDictionaryRef  matchingDict = IOServiceMatching ( kIOUSBDeviceClassName );
if ( matchingDict )
{
    UInt32        usbVendor = k_MyVendorID;
    CFNumberRef   refVendorId = CFNumberCreate ( kCFAllocatorDefault, kCFNumberIntType, &usbVendor );
    CFDictionarySetValue ( matchingDict, CFSTR ( kUSBVendorID ), refVendorId );
    CFRelease ( refVendorID );
    CFDictionarySetValue ( matchingDict, CFSTR ( kUSBProductID ), CFSTR ( "*" ) );   // This is a wildcard, so we find any device.
}

在“添加新设备”上似乎可以使用:

kr = IOServiceAddMatchingNotification(gNotifyPort,kIOMatchedNotification, matchingDict, RawDeviceAdded, NULL, &gRawAddedIter);

使用这些设备,它可能崩溃或列表无法更新,而使用remove device似乎可以正常工作。 //终止通知:

kr = IOServiceAddMatchingNotification(gNotifyPort, kIOTerminatedNotification, matchingDict, RawDeviceRemoved, NULL, &gRawRemovedIter);

//终止通知:

kr = IOServiceAddMatchingNotification(gNotifyPort, kIOTerminatedNotification, matchingDict, NonRawDeviceRemoved, NULL, &gRawRemovedIter);

处理此问题的最佳方法是什么?

1 个答案:

答案 0 :(得分:0)

您是否要在对matchingDict的两次调用中重复使用相同的IOServiceAddMatchingNotificationIOServiceAddMatchingNotification和类似功能都会释放传入的匹配字典,因此,如果要使用它两次,则必须CFRetain(matchingDict);一次才能开始使用它。

(构建->分析理论上应该为您捕获此错误。)