string GetString(SPDRP property) { var resultString = new StringBuilder(KERNEL32.DefaultStructBufferSize); var devInfo = deviceInfo; var result = SETUPAPI.SetupDiGetDeviceRegistryProperty(Handle, ref devInfo, property, out var valueKind, resultString, (uint)resultString.Capacity, out _); if (!result) { var error = Marshal.GetLastWin32Error(); if (error == 13) { return(null); } if (error != 0) { throw new Win32ErrorException(error); } } switch (valueKind) { case RegistryValueKind.Binary: throw new InvalidCastException(string.Format("Please use GetData() for BINARY values!")); case RegistryValueKind.DWord: throw new InvalidCastException(string.Format("Please use GetUInt32() for DWORD values!")); case RegistryValueKind.QWord: throw new InvalidCastException(string.Format("Please use GetUInt64() for QWORD values!")); case RegistryValueKind.MultiString: break; case RegistryValueKind.String: break; default: throw new NotImplementedException(string.Format("Not implemented registry value kind {0}!", valueKind)); } return(resultString.ToString()); }
/// <summary> /// Releases the handle /// </summary> /// <returns></returns> protected override bool ReleaseHandle() { if (IsInvalid) { return(true); } SETUPAPI.SetupDiDestroyDeviceInfoList(DangerousGetHandle()); SetHandle(IntPtr.Zero); return(true); }
/// <summary> /// Obtains a list of all present (active and inactive) devices /// </summary> /// <returns>Returns an array of <see cref="DEVICE"/>s</returns> public static DEVICE[] GetAllPresentDevices() { var devices = new List <DEVICE>(); var guid = new Guid(); var handle = SETUPAPI.SetupDiGetClassDevs(ref guid, null, IntPtr.Zero, DIGCF.ALLCLASSES | DIGCF.PRESENT); if (handle.IsInvalid) { throw new Win32ErrorException(); } var info = new SP_DEVINFO_DATA(); info.cbSize = (uint)Marshal.SizeOf(info); for (uint i = 0; SETUPAPI.SetupDiEnumDeviceInfo(handle, i, ref info); i++) { var device = new DEVICE(handle, info); devices.Add(device); } return(devices.ToArray()); }