private static IEnumerable <Guid> EnumerateClasses(CmClassType flags) { Guid guid = Guid.Empty; int index = 0; while (true) { var result = DeviceNativeMethods.CM_Enumerate_Classes(index++, ref guid, flags); if (result == CrError.NO_SUCH_VALUE) { break; } if (result == CrError.INVALID_DATA) { continue; } if (result != CrError.SUCCESS) { throw new ArgumentException($"Failed to enumerate device class. Error: {result}"); } yield return(guid); } }
internal static extern CrError CM_Enumerate_Classes(int ulClassIndex, ref Guid ClassGuid, CmClassType ulFlags);
private static NtResult <SafeHGlobalBuffer> GetClassProperty(Guid class_guid, CmClassType flags, DEVPROPKEY key, out DEVPROPTYPE type, bool throw_on_error) { int length = 0; var result = DeviceNativeMethods.CM_Get_Class_PropertyW(class_guid, key, out type, SafeHGlobalBuffer.Null, ref length, flags); if (result != CrError.BUFFER_SMALL) { return(result.ToNtStatus().CreateResultFromError <SafeHGlobalBuffer>(throw_on_error)); } using (var buffer = new SafeHGlobalBuffer(length)) { return(DeviceNativeMethods.CM_Get_Class_PropertyW(class_guid, key, out type, buffer, ref length, flags).ToNtStatus().CreateResult(throw_on_error, () => buffer.Detach())); } }