1

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.

3
  • 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? Commented Jul 11, 2022 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. Commented Jul 11, 2022 at 12:46
  • (exception occurs in the CTor when I call the above code.) Commented Jul 11, 2022 at 12:56

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.