From 596ff6d9d43e250e60bf4d3c714c2ca1f8a3ade4 Mon Sep 17 00:00:00 2001 From: ema Date: Sun, 22 Dec 2024 00:51:02 +0800 Subject: [PATCH] Prevent QL from crashing when WMI error #1379 --- QuickLook/NativeMethods/WMI.cs | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/QuickLook/NativeMethods/WMI.cs b/QuickLook/NativeMethods/WMI.cs index 8e7446e..a88ee52 100644 --- a/QuickLook/NativeMethods/WMI.cs +++ b/QuickLook/NativeMethods/WMI.cs @@ -1,4 +1,4 @@ -// Copyright © 2019 Paddy Xu +// Copyright © 2024 QL-Win Contributors // // This file is part of QuickLook program. // @@ -15,32 +15,40 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . +using System; using System.Collections.Generic; using System.Diagnostics; using System.Management; +using System.Runtime.InteropServices; namespace QuickLook.NativeMethods; -internal class WMI +internal static class WMI { public static List GetGPUNames() { try { - using (var searcher = new ManagementObjectSearcher(@"root\CIMV2", @"SELECT * FROM Win32_VideoController")) - { - var names = new List(); + using var searcher = new ManagementObjectSearcher(@"root\CIMV2", @"SELECT * FROM Win32_VideoController"); + List names = []; - foreach (var obj in searcher.Get()) - names.Add(obj["Name"] as string); + foreach (var obj in searcher.Get()) + names.Add(obj["Name"] as string); - return names; - } + return names; } catch (ManagementException e) { - Debug.WriteLine(e.Message); - return new List(); + Debug.WriteLine($"ManagementException caught: {e.Message}"); } + catch (COMException e) + { + Debug.WriteLine($"COMException caught: {e.Message}"); + } + catch (Exception e) + { + Debug.WriteLine($"General exception caught: {e.Message}"); + } + return []; } }