Go wrapper around the Windows Device Console (devcon.exe
).
go install github.com/mikerourke/go-devcon
Here's a brief overview of DevCon taken from the Windows Hardware Developer documentation site:
DevCon (Devcon.exe), the Device Console, is a command-line tool that displays detailed information about devices on computers running Windows. You can use DevCon to enable, disable, install, configure, and remove devices.
DevCon runs on Microsoft Windows 2000 and later versions of Windows.
This package provides a handy mechanism for querying devices and performing various operations on said devices.
It provides methods that map directly to the devcon.exe
commands.
You'll need the devcon.exe
executable. It's not included in the package because Redistribution of Microsoft software without consent is usually a violation of Microsoft's End User License Agreements.
For information regarding how to get your hands on devcon.exe
, check out the Microsoft documentation page.
There are other ways to get it, but you didn't hear it from me.
There is extensive documentation and examples available on the docs site, but here's a quick example of how to log all the devices on the computer:
package main
import (
"fmt"
"github.com/mikerourke/go-devcon"
)
func main() {
dc := devcon.New(`\path\to\devcon.exe`)
devs, err := dc.FindAll("=net")
if err != nil {
panic(err)
}
for _, dev := range devs {
fmt.Printf("ID: %s, Name: %s\n", dev.ID, dev.Name)
}
}
Aren't you supposed to use the PnPUtil
now?
Yes, but PnPUtil
is only supported on Windows Vista and later. You can't use it on older operating systems.
Yes, but one of my other projects requires me to check for the existence of a device and install a driver on Windows XP. I wrote a bunch of parsing code to get the output, so I figured why not open source it?
Yes, I'm using only using Go APIs available in version 1.10.7
(or earlier?), since executables built with that version of Go still run on Windows XP.
It's possible that earlier versions may work, but I used 1.10.7
during development, so I'd advise sticking with that version.
To target Windows XP 32-bit, build your project like so:
GOOS=windows GOARCH=386 go1.10.7 build -o myproject.exe myproject.go