internal static IEnumerable <string> GetDeviceIdList(string filter, CmGetIdListFlags flags) { int retry_count = 10; while (retry_count-- > 0) { DeviceNativeMethods.CM_Get_Device_ID_List_SizeW(out int length, filter, flags).ToNtStatus().ToNtException(); using (var buffer = new SafeHGlobalBuffer(length * 2)) { var result = DeviceNativeMethods.CM_Get_Device_ID_ListW(filter, buffer, length, flags); if (result == CrError.BUFFER_SMALL) { continue; } result.ToNtStatus().ToNtException(); return(buffer.ReadUnicodeString(length).Split(new char[] { '\0' }, StringSplitOptions.RemoveEmptyEntries)); } } throw new ArgumentException("Couldn't enumerate device ID list."); }