A new "driver" provider installs Windows printer and scanner drivers through the same catalog, UI and profiles as everything else: stage the package into the driver store with pnputil, then register it with the print spooler so it turns up in Add Printer. Driver names are matched by PREFIX against the names read out of the package's own INF files. Vendors stamp the version into the name -- "HP Universal Printing PCL 6 (v7.1.0)" -- so an exact name in a catalog goes stale at the next release, and a wrong one is unrecoverable without knowing what the package contains. When a prefix matches nothing, the error lists the names the package actually provides, which turns a dead end into a one-line fix. INF parsing handles what real files are, not what the spec suggests: Windows ships its own printer INFs as UTF-16LE and vendor packages follow suit, so reading them as UTF-8 finds nothing at all. Verified against every prn*.inf on a stock Windows install -- that test is what caught the encoding bug, since the hand-written sample parsed fine. Scanners are TWAIN/WIA rather than spooler drivers, so there is nothing to register: leaving driver_match unset means stage-only. Detection falls back to the uninstall entry, because a TWAIN data source has no spooler record. Packages come from a driver share rather than the web. Vendor URLs are version-pinned and several are EULA-gated, so nothing stable can be shipped in a catalog; a UNC path or local folder is used in place with no download at all, which is faster and is what a shop wants anyway. $DEPLOYR_DRIVERS names the share, and an unset variable is reported by name rather than surfacing three layers down as "no such file". Ships universal drivers for HP, Xerox, Ricoh, Sharp, Canon, Kyocera, Konica Minolta, Lexmark, Brother, Toshiba, Epson, Muratec and Zebra, plus scanner packages for the fi series, ScanSnap, Canon DR, Epson, Brother, Visioneer and Kodak Alaris. Profiles: @printers, @printers-all, @scanners. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
78 lines
2.1 KiB
Go
78 lines
2.1 KiB
Go
package detect
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"os/exec"
|
|
"strings"
|
|
"sync"
|
|
"syscall"
|
|
"time"
|
|
)
|
|
|
|
// PrinterDriver is one driver registered with the Windows print subsystem.
|
|
type PrinterDriver struct {
|
|
Name string `json:"Name"`
|
|
MajorVersion int `json:"MajorVersion"`
|
|
Manufacturer string `json:"Manufacturer"`
|
|
}
|
|
|
|
var (
|
|
printerOnce sync.Once
|
|
printerList []PrinterDriver
|
|
)
|
|
|
|
// PrinterDrivers returns the printer drivers installed on this machine, read
|
|
// once per process. A printer driver has no uninstall registry entry and no
|
|
// file on PATH, so the spooler is the only source of truth for whether one is
|
|
// present.
|
|
func PrinterDrivers() []PrinterDriver {
|
|
printerOnce.Do(func() {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
|
|
defer cancel()
|
|
|
|
cmd := exec.CommandContext(ctx, "powershell", "-NoProfile", "-NonInteractive", "-Command",
|
|
"Get-PrinterDriver | Select-Object Name,MajorVersion,Manufacturer | ConvertTo-Json -Compress")
|
|
cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
|
|
out, err := cmd.Output()
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
trimmed := strings.TrimSpace(string(out))
|
|
if trimmed == "" {
|
|
return
|
|
}
|
|
// ConvertTo-Json emits a bare object when there is exactly one result.
|
|
if strings.HasPrefix(trimmed, "{") {
|
|
var one PrinterDriver
|
|
if json.Unmarshal([]byte(trimmed), &one) == nil {
|
|
printerList = []PrinterDriver{one}
|
|
}
|
|
return
|
|
}
|
|
_ = json.Unmarshal([]byte(trimmed), &printerList)
|
|
})
|
|
return printerList
|
|
}
|
|
|
|
// FindPrinterDriver returns the first installed driver whose name starts with
|
|
// one of the prefixes. Prefix matching is deliberate: vendors stamp a version
|
|
// into the driver name ("HP Universal Printing PCL 6 (v7.1.0)") and it changes
|
|
// with every release.
|
|
func FindPrinterDriver(prefixes []string) (PrinterDriver, bool) {
|
|
drivers := PrinterDrivers()
|
|
for _, prefix := range prefixes {
|
|
p := strings.ToLower(strings.TrimSpace(prefix))
|
|
if p == "" {
|
|
continue
|
|
}
|
|
for _, d := range drivers {
|
|
if strings.HasPrefix(strings.ToLower(d.Name), p) {
|
|
return d, true
|
|
}
|
|
}
|
|
}
|
|
return PrinterDriver{}, false
|
|
}
|