internal static extern bool GetDeviceRegistryProperty( IntPtr hInfoList, ref SP_DEVINFO_DATA deviceInfoData, [MarshalAs(UnmanagedType.U4)] DeviceRegistryPropertyCode propertyKey, [MarshalAs(UnmanagedType.U4)] out DeviceRegistryPropertyType propertyType, IntPtr propertyBuffer, int propertyBufferSize, out int requiredBufferSize);
/// <summary> /// /// </summary> /// <param name="device"></param> /// <param name="propertyKey"></param> /// <param name="propertyType"></param> /// <param name="requiredSize"></param> /// <returns></returns> public static bool GetDeviceRegistryProperty(DeviceInfo device, DeviceRegistryPropertyKey propertyKey, out DeviceRegistryPropertyType propertyType, out int requiredSize) { var deviceInfoList = device.InfoSet.InfoSet; var deviceInfoData = device.InfoData; var success = GetDeviceRegistryProperty(deviceInfoList, ref deviceInfoData, propertyKey.PropertyCode, out propertyType, IntPtr.Zero, 0, out requiredSize); if (!success) { var lastError = ErrorHelpers.GetLastError(); return(lastError == ErrorCode.InsufficientBuffer); } return(true); }
/// <summary> /// /// </summary> /// <param name="device"></param> /// <param name="propertyKey"></param> /// <param name="propertyType"></param> /// <param name="propertyData"></param> /// <returns></returns> public static bool GetDeviceRegistryProperty(DeviceInfo device, DeviceRegistryPropertyKey propertyKey, out DeviceRegistryPropertyType propertyType, out Api.Buffer propertyData) { var deviceInfoList = device.InfoSet.InfoSet; var deviceInfoData = device.InfoData; propertyData = new Api.Buffer(); int requiredSize; // Keep trying until we've got success or an unrecoverable error. while (true) { var success = GetDeviceRegistryProperty(deviceInfoList, ref deviceInfoData, propertyKey.PropertyCode, out propertyType, propertyData.Data, propertyData.Length, out requiredSize); // If the data was read successfully, truncate the buffer to match the length read. if (success) { propertyData.Truncate(requiredSize); return(true); } // If the last error was for anything except the buffer being too small, cleanly get rid of the buffer // before returning failure. var lastError = ErrorHelpers.GetLastError(); if (lastError != ErrorCode.InsufficientBuffer) { propertyData.Dispose(); propertyData = null; return(false); } // Resize the buffer to the required length before trying again. propertyData.Resize(requiredSize); } }