Exemplo n.º 1
0
//		[DllImport(hid)]
//		internal static extern long AllocateHIDObjectFromIOHIDDeviceRef(IOHIDDeviceRef inIOHIDDeviceRef);

        internal static long AllocateHIDObjectFromIOHIDDeviceRef(IOHIDDeviceRef inIOHIDDeviceRef)
        {
            long result = 0;

            if (inIOHIDDeviceRef != IntPtr.Zero)
            {
                // Set up the matching criteria for the devices we're interested in.
                // We are interested in instances of class IOHIDDevice.
                // matchingDict is consumed below( in IOServiceGetMatchingService )
                // so we have no leak here.
                //CFMutableDictionaryRef matchingDict = IOServiceMatching(kIOHIDDeviceKey);
                byte[] utf8Bytes = Encoding.UTF8.GetBytes(Native.kIOHIDDeviceKey);
                //char[] charArr=Native.IOHIDDeviceKey.ToCharArray(
                IntPtr bufferIntPtr = Marshal.AllocHGlobal(utf8Bytes.Length);
                Marshal.Copy(utf8Bytes, 0, bufferIntPtr, utf8Bytes.Length);



                IntPtr matchingDictRef = Native.IOServiceMatching(bufferIntPtr);
                matchingDictRef = Native.IOServiceMatching(Native.CFSTR(Native.kIOHIDDeviceKey));
                Marshal.FreeHGlobal(bufferIntPtr);



                if (matchingDictRef != IntPtr.Zero)
                {
                    Native.CFDictionary dict = new Native.CFDictionary(matchingDictRef);

                    // Add a key for locationID to our matching dictionary.  This works for matching to
                    // IOHIDDevices, so we will only look for a device attached to that particular port
                    // on the machine.

                    IntPtr tCFTypeRef = Native.IOHIDDeviceGetProperty(inIOHIDDeviceRef, Native.CFSTR(Native.kIOHIDLocationIDKey));

                    if (tCFTypeRef != IntPtr.Zero)
                    {
                        dict[Native.kIOHIDLocationIDKey] = tCFTypeRef;



                        //CFDictionaryAddValue (matchingDictRef, CFSTR (Native.IOHIDLocationIDKey), tCFTypeRef);
                        // CFRelease( tCFTypeRef ); // don't release objects that we "Get".

                        // IOServiceGetMatchingService assumes that we already know that there is only one device
                        // that matches.  This way we don't have to do the whole iteration dance to look at each
                        // device that matches.  This is a new API in 10.2
                        //result = Native.IOServiceGetMatchingService (kIOMasterPortDefault, matchingDictRef);
                        result = Native.IOServiceGetMatchingService(IntPtr.Zero, matchingDictRef);
                    }

                    // Note: We're not leaking the matchingDict.
                    // One reference is consumed by IOServiceGetMatchingServices
                }
            }
            return(result);
        }