public void RunScanner() { int usbVendor = (int)TreehopperUsb.Settings.Vid; int usbProduct = (int)TreehopperUsb.Settings.Pid; IntPtr gAddedIter = IntPtr.Zero; var matchingDict = NativeMethods.IOServiceMatching(IOKitFramework.kIOUSBDeviceClassName); if (matchingDict == IntPtr.Zero) { throw new Exception("IOServiceMatching returned NULL"); } var numberRef = NativeMethods.CFNumberCreate(IOKitFramework.kCFAllocatorDefault, CFNumberType.kCFNumberSInt32Type, ref usbVendor); NativeMethods.CFDictionarySetValue(matchingDict, NativeMethods.__CFStringMakeConstantString(IOKitFramework.kUSBVendorID), numberRef); NativeMethods.CFRelease(numberRef); numberRef = NativeMethods.CFNumberCreate(IOKitFramework.kCFAllocatorDefault, CFNumberType.kCFNumberSInt32Type, ref usbProduct); NativeMethods.CFDictionarySetValue(matchingDict, NativeMethods.__CFStringMakeConstantString(IOKitFramework.kUSBProductID), numberRef); NativeMethods.CFRelease(numberRef); gNotifyPort = NativeMethods.IONotificationPortCreate(IOKitFramework.kIOMasterPortDefault); var runLoopSource = NativeMethods.IONotificationPortGetRunLoopSource(gNotifyPort); gRunLoop = NativeMethods.CFRunLoopGetCurrent(); NativeMethods.CFRunLoopAddSource(gRunLoop, runLoopSource, NativeMethods.__CFStringMakeConstantString(IOKitFramework.kCFRunLoopDefaultMode)); IOServiceMatchingCallback deviceAdded = new IOServiceMatchingCallback(DeviceAdded); DeviceWatcherCallbackReference callbackRef = new DeviceWatcherCallbackReference(); var kr = NativeMethods.IOServiceAddMatchingNotification( gNotifyPort, // notifyPort IOKitFramework.kIOFirstMatchNotification, // notificationType matchingDict, // matching DeviceAdded, // callback callbackRef, // refCon ref gAddedIter // notification ); DeviceAdded(callbackRef, gAddedIter); NativeMethods.CFRunLoopRun(); }
public static IOIterator <T> AddMatchingNotification <T> (IONotificationPort notifyPort, NotificationType notificationType, NSDictionary matchingDictionary, MatchingCallback <T> callback) where T : IOService { var notificationTypeString = notificationType.GetKey(); IOServiceMatchingCallback nativeCallback = (refCon, iteratorRef) => { var iterator = MarshalNativeObject <IOIterator <T> > (iteratorRef, false); callback.Invoke(iterator); }; callbackStore.Add(nativeCallback); IntPtr iteratorRef2; CFType.Retain(matchingDictionary.Handle); var result = IOServiceAddMatchingNotification(notifyPort.Handle, notificationTypeString, matchingDictionary.Handle, nativeCallback, IntPtr.Zero, out iteratorRef2); ThrowIfError(result); var iterator2 = new IOIterator <T> (iteratorRef2, true); iterator2.Disposed += (sender, e) => callbackStore.Remove(nativeCallback); return(iterator2); }
public static extern IOReturn IOServiceAddMatchingNotification(IntPtr notifyPort, IntPtr notifyType, IntPtr matching, IOServiceMatchingCallback callback, IntPtr context, out int iterator);
extern static kern_return_t IOServiceAddMatchingNotification(IONotificationPortRef notifyPort, io_name_t notificationType, CFDictionaryRef matching, IOServiceMatchingCallback callback, IntPtr refCon, out io_iterator_t notification);
public static extern IOKitError IOServiceAddMatchingNotification(IntPtr notifyPort, string notificationType, IntPtr matching, IOServiceMatchingCallback callback, DeviceWatcherCallbackReference refCon, ref IntPtr notification);