0

I am working in VB.NET and trying to automate a driver install process. I am stuck on finding a way (if even possible?) to check the device manager. The driver in question makes it's own section/category (For lack of a better term. Similar to the Batteries, Monitors, Processors, Ports, etc. level in the device manager list). It is proprietary, so if the "Custom Driver" section/category is listed I just want to have my WinForm show/hide some buttons if the driver is already installed.

I have made the reference to System.Management in project resources and added the namespace Imports System.Management to the top of my code but I cannot find an example that's even close to what I would like to do.

'Pseudo Code:
DIM name as String = "Custom Driver"
DIM DevMangr as New System.Management.Reader()
DIM Category as String = DevMangr.ReadLine()
Do While DevMangr.Peek <> -1
    If Category.StartsWith(name)
        Button1.Visible = False
        Label1.Visible = True
        Button2.Visible = True
        Label2.Visible = False
    End If
Loop
DevMan.Close()
Mike
  • 33
  • 8
  • 2
    With System.Management you query a WMI class. You could test [Win32_PnPEntity](https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/win32-pnpentity) and filter by `Name` or `Manufacturer` or `Description` (or whatever detail you know about this Driver). Or `Win32_PnPSignedDriver` and use `DeviceName` or `Description` (`Name` is usually empty). Or `Win32_SystemDriver`, depending of what kind of Driver that is. – Jimi Jun 09 '22 at 17:55
  • 2
    A VB.Net example of a query (+ configuration): [WmiMonitorID - Converting the results to ASCII](https://stackoverflow.com/a/51672117/7444103) -- The Scope there is defined as `$"\\{Environment.MachineName}\root\WMI"`, you need to replace `WMI` with `CIMV2`. The query is `"SELECT * FROM WmiMonitorID"`, you should have something like `"SELECT * FROM Win32_PnPEntity WHERE Name = '[Some Name]'"` (of course replace `[Some Name]` with what you have). You can also use `LIKE '%[Some Name]%'` for partial matches. – Jimi Jun 09 '22 at 18:02
  • That is actually really helpful. Where would my check go set the Visibility of my objects if the name of the custom driver is found? At the end of the day I just want the Labels and Buttons Visible/hidden based on if the "Custom Driver" is there. I'm OK incorporating an IF statement to keep it generic. I know my pseudo code is garbage but that's kind of what I am looking for. – Mike Jun 10 '22 at 13:36
  • 1
    You probably just need to check that `moSearcher.Get().Count > 0`, once you have determine that you can get the Driver by name or whatever with that query. – Jimi Jun 10 '22 at 13:43

1 Answers1

0

Thanks to help from Jimi in the Comments, I was able to solve my problem. Actual code for anyone interested is below:

Dim ConnOptions As New ConnectionOptions() With {.EnablePrivileges = True, .Timeout = EnumerationOptions.InfiniteTimeout}
Dim mOptions As New EnumerationOptions() With {.Rewindable = False, .ReturnImmediately = True, .DirectRead = True, .EnumerateDeep = False}
Dim mQuery As New SelectQuery("SELECT * FROM Win32_PnPEntity WHERE Name LIKE '%Custom Driver%'")
Dim mScope As New ManagementScope($"\\{Environment.MachineName}\root\CIMV2", ConnOptions)
mScope.Connect()
    Using moSearcher As New ManagementObjectSearcher(mScope, mQuery, mOptions)
        For Each Item As ManagementObject In moSearcher.[Get]()
        Next
        If moSearcher.Get.Count > 0 Then
            Label1.Visible = True
        End If
    End Using

Essentially what this does is check Device Manager for "Custom Driver" and if it is there, turn Label1 on, which is a message about the driver being all set.

Mike
  • 33
  • 8
  • 1
    Remove the For Each loop, it's irrelevant. -- It's `moSearcher.Get().Count`, not `moSearcher.Get.Count`: don't ever confuse Methods with Properties, doesn't matter whether VB.Net (wrongly) allows it. This can generate hard to track exceptions at run-time. – Jimi Jun 10 '22 at 14:25