0

The System.Management class uses WMI (windows Management Instrumentation) to fetch system information. WMI can fail sometimes, making the calls throw ManagementException.

Is there an alternative to WMI? I do not need much really; here is the CPU information I need:

    Private Sub GetNumberOfProcessors()
        For Each item In New System.Management.ManagementObjectSearcher("SELECT NumberOfProcessors FROM Win32_ComputerSystem").Get
            _NumberOfPhysicalProcessors = CInt(item("NumberOfProcessors"))
        Next
        For Each item In New System.Management.ManagementObjectSearcher("SELECT NumberOfCores,NumberOfLogicalProcessors FROM Win32_Processor").Get
            _NumberOfPhysicalCores = CInt(item("NumberOfCores"))
            _NumberOfLogicalCores = CInt(item("NumberOfLogicalProcessors"))
        Next
    End Sub

If not, I suppose catching the exception and use System.Environment.ProcessorCount as a fall back when that happens could be a worst-case scenario viable option.

EDIT: it seems System.Environment.ProcessorCount is not an option after all, as it relies on WMI too.

CTZStef
  • 1,675
  • 2
  • 18
  • 47
  • 1
    It'd be interesting to know what calls of yours are actually failing and for what stated reasons. If there were some other mechanism that was magically more reliable, why do you think WMI wouldn't be changed to make use of that other mechanism? – Damien_The_Unbeliever Jul 11 '22 at 12:41
  • @Damien_The_Unbeliever I can't easily reproduce the error on my end. Part of the call stack I received goes like this: System.Management.ManagementException: Invalid class at System.Management.ManagementException.ThrowWithExtendedInfo(ManagementStatus errorCode) at System.Management.ManagementObjectCollection.ManagementObjectEnumerator.MoveNext() I don't need magic, just an alternative when that exception is thrown. – CTZStef Jul 11 '22 at 12:46
  • (exception occurs in the CTor when I call the above code.) – CTZStef Jul 11 '22 at 12:56

1 Answers1

2

If you were only interested in logical processor, Environment.ProcessorCount would have helped. Since you need more details, then P/Invoke can help. You can make use of GetLogicalProcessorInformation function in Kernel32.dll to get the details.

This SO answer shows how to get the details.

Danish
  • 136
  • 5