/// <summary>
		/// Releases a CFObjects handle.
		/// </summary>
		/// <param name="obj">The object to release.</param>
		internal static IOObject IOIteratorNext(IOObject obj) {
			var result = NativeMethods.IOIteratorNext(obj.Handle);
			if (result != IntPtr.Zero) {
				return new IOObject(result);
			}

			return null;
		}
示例#2
0
 /// <summary>
 /// Releases a CFObjects handle.
 /// </summary>
 /// <param name="obj">The object to release.</param>
 internal static void IOObjectRelease(IOObject obj)
 {
     NativeMethods.IOObjectRelease(obj.Handle);
 }
		/// <summary>
		/// Gets a dictionary value.
		/// </summary>
		/// <param name="dictionary">The dictionary to use.</param>
		/// <param name="key">The key to look up.</param>
		/// <param name="value">Receives the value.</param>
		/// <returns>An integer.</returns>
		public static int GetCFPropertyInt(IOObject dictionary, string key, out int value) {
			var kernResult = KERN_FAILURE;
			var bsdPathAsCFValue = NativeMethods.IORegistryEntrySearchCFProperty(dictionary.Handle, kIOServicePlane, NativeMethods.__CFStringMakeConstantString(key), kCFAllocatorDefault, kIORegistryIterateRecursively);
			//var bsdPathAsCFString = IORegistryEntryCreateCFProperty(modemService, __CFStringMakeConstantString(key), kCFAllocatorDefault, 0);

			if (bsdPathAsCFValue != IntPtr.Zero) {
				// Convert the value from a CFNumber to an integer.
				try {
					var bsdValue = IntPtr.Zero;
					var result = NativeMethods.CFNumberGetValue(bsdPathAsCFValue, CFNumberType.kCFNumberIntType, out bsdValue);

					if (result) {
						kernResult = KERN_SUCCESS;
						value = bsdValue.ToInt32();
					} else {
						value = 0;
					}
				} finally {
					NativeMethods.CFRelease (bsdPathAsCFValue);
				}
			} else {
				value = 0;
			}

			return kernResult;
		}
		/// <summary>
		/// Releases a CFObjects handle.
		/// </summary>
		/// <param name="obj">The object to release.</param>
		internal static void IOObjectRelease(IOObject obj) {
			NativeMethods.IOObjectRelease(obj.Handle);
		}
		/// <summary>
		/// Gets a dictionary value.
		/// </summary>
		/// <param name="dictionary">The dictionary to use.</param>
		/// <param name="key">The key to look up.</param>
		/// <returns>A string on sucess; Otherwise, null.</returns>
		public static int GetCFPropertyInt(IOObject dictionary, string key) {
			var result = 0;
			var kernResult = GetCFPropertyInt(dictionary, key, out result);
			if (kernResult == KERN_SUCCESS) {
				return result;
			}

			return 0;
		}
		/// <summary>
		/// Gets a dictionary value.
		/// </summary>
		/// <param name="dictionary">The dictionary to use.</param>
		/// <param name="key">The key to look up.</param>
		/// <param name="value">Receives the value.</param>
		/// <returns>KERN_SUCCESS on success.</returns>
		public static int GetCFPropertyString(IOObject dictionary, string key, out string value) {
			var maxValueSize = 4096;
			var kernResult = KERN_FAILURE;
			var bsdPathAsCFValue = NativeMethods.IORegistryEntrySearchCFProperty(dictionary.Handle, kIOServicePlane, NativeMethods.__CFStringMakeConstantString(key), kCFAllocatorDefault, kIORegistryIterateRecursively);
			// var bsdPathAsCFString = IORegistryEntryCreateCFProperty(modemService, __CFStringMakeConstantString(key), kCFAllocatorDefault, 0);

			if (bsdPathAsCFValue != IntPtr.Zero) {
				// Convert the value from a CFString to a C (NULL-terminated) string.
				try {
					unsafe {
						fixed (char* bsdValue = new char[maxValueSize]) {
							var result = NativeMethods.CFStringGetCString (bsdPathAsCFValue, bsdValue, maxValueSize, CFStringEncoding.kCFStringEncodingUTF8);

							if (result) {
								kernResult = KERN_SUCCESS;
								value = Marshal.PtrToStringAnsi ((IntPtr)bsdValue);
							} else {
								value = null;
							}
						}
					}
				} finally {
					NativeMethods.CFRelease (bsdPathAsCFValue);
				}
			} else {
				value = null;
			}
				
			return kernResult;
		}
		/// <summary>
		/// Gets a dictionary value.
		/// </summary>
		/// <param name="dictionary">The dictionary to use.</param>
		/// <param name="key">The key to look up.</param>
		/// <returns>A string on sucess; Otherwise, null.</returns>
		public static string GetCFPropertyString(IOObject dictionary, string key) {
			var value = String.Empty;
			var kernResult = GetCFPropertyString(dictionary, key, out value);
			if (kernResult == KERN_SUCCESS) {
				return value;
			}

			return null;
		}
		/// <summary>
		/// Gets the corresponding USB device of a modem service.
		/// </summary>
		/// <param name="modemService">A modem service.</param>
		/// <returns>An instance of a <see cref="CFObject"/> on success; Otherwise, false.</returns>
		public static IOObject GetUSBDevice(IOObject modemService) {
			var device = NativeMethods.GetUsbDevice(modemService.Handle);
			if (device != IntPtr.Zero) {
				return new IOObject(device);
			}

			return null;
		}
		/// <summary>
		/// Gets the path of the given modem service.
		/// </summary>
		/// <param name="modemService">The modem service whose path will be looked up.</param>
		/// <returns>A string.</returns>
		public static string GetModemPath(IOObject modemService) {
			var modemPath = String.Empty;
			var kernResult = NativeMethods.GetModemPath(modemService.Handle, out modemPath);
			if (kernResult != KERN_SUCCESS) {
				return null;
			}

			return modemPath;
		}