Add UpdateDriverForPlugAndPlayDevicesW to driver install test tool

Test both methods: UpdateDriverForPlugAndPlayDevicesW (with Hardware ID
matching) and DiInstallDriverW. Also includes go.mod and pre-built exe.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
jim800121chen 2026-03-10 00:25:44 +08:00
parent faa4e86a19
commit 17110a1606
3 changed files with 73 additions and 24 deletions

View File

@ -0,0 +1,3 @@
module test_driver_install
go 1.25.0

View File

@ -1,6 +1,7 @@
// Small test tool to call DiInstallDriverW and print the result.
// Test tool for Windows USB driver installation.
// Build: GOOS=windows GOARCH=amd64 go build -o test_driver_install.exe .
// Run on Windows: .\test_driver_install.exe "C:\Users\User\AppData\Local\EdgeAIPlatform\drivers\kneron_winusb.inf"
// Run on Windows (as admin):
// .\test_driver_install.exe "C:\Users\User\AppData\Local\EdgeAIPlatform\drivers\kneron_winusb.inf"
package main
@ -20,55 +21,100 @@ func main() {
infPath := os.Args[1]
fmt.Printf("INF path: %s\n", infPath)
// Check file exists
if _, err := os.Stat(infPath); err != nil {
fmt.Printf("ERROR: file not found: %v\n", err)
os.Exit(1)
}
// Hardware ID for Kneron KL520
hardwareID := "USB\\VID_3231&PID_0100"
fmt.Println("")
fmt.Println("=== Method 1: UpdateDriverForPlugAndPlayDevicesW ===")
tryUpdateDriver(infPath, hardwareID)
fmt.Println("")
fmt.Println("=== Method 2: DiInstallDriverW ===")
tryDiInstallDriver(infPath)
}
func tryUpdateDriver(infPath, hardwareID string) {
newdev, err := syscall.LoadDLL("newdev.dll")
if err != nil {
fmt.Printf("ERROR: LoadDLL(newdev.dll): %v\n", err)
os.Exit(1)
fmt.Printf("ERROR: LoadDLL: %v\n", err)
return
}
defer newdev.Release()
fmt.Println("newdev.dll loaded OK")
proc, err := newdev.FindProc("DiInstallDriverW")
proc, err := newdev.FindProc("UpdateDriverForPlugAndPlayDevicesW")
if err != nil {
fmt.Printf("ERROR: FindProc(DiInstallDriverW): %v\n", err)
os.Exit(1)
}
fmt.Println("DiInstallDriverW found OK")
infPathUTF16, err := syscall.UTF16PtrFromString(infPath)
if err != nil {
fmt.Printf("ERROR: UTF16PtrFromString: %v\n", err)
os.Exit(1)
fmt.Printf("ERROR: FindProc: %v\n", err)
return
}
const DIIRFLAG_FORCE_INF = 0x00000002
infUTF16, _ := syscall.UTF16PtrFromString(infPath)
hwIDUTF16, _ := syscall.UTF16PtrFromString(hardwareID)
const INSTALLFLAG_FORCE = 0x00000001
var needReboot int32
fmt.Println("Calling DiInstallDriverW...")
fmt.Printf("Hardware ID: %s\n", hardwareID)
fmt.Println("Calling UpdateDriverForPlugAndPlayDevicesW...")
fmt.Println("(If a Windows Security dialog appears, click 'Install this driver software anyway')")
ret, _, lastErr := proc.Call(
0, // hwndParent = NULL
uintptr(unsafe.Pointer(infPathUTF16)),
uintptr(unsafe.Pointer(hwIDUTF16)),
uintptr(unsafe.Pointer(infUTF16)),
INSTALLFLAG_FORCE,
uintptr(unsafe.Pointer(&needReboot)),
)
fmt.Printf("Return value: %d\n", ret)
fmt.Printf("NeedReboot: %d\n", needReboot)
if ret == 0 {
errno := lastErr.(syscall.Errno)
fmt.Printf("FAILED - error: %v (code: %d / 0x%08X)\n", lastErr, uint32(errno), uint32(errno))
} else {
fmt.Println("SUCCESS - driver installed!")
}
}
func tryDiInstallDriver(infPath string) {
newdev, err := syscall.LoadDLL("newdev.dll")
if err != nil {
fmt.Printf("ERROR: LoadDLL: %v\n", err)
return
}
defer newdev.Release()
proc, err := newdev.FindProc("DiInstallDriverW")
if err != nil {
fmt.Printf("ERROR: FindProc: %v\n", err)
return
}
infUTF16, _ := syscall.UTF16PtrFromString(infPath)
const DIIRFLAG_FORCE_INF = 0x00000002
var needReboot int32
fmt.Println("Calling DiInstallDriverW (DIIRFLAG_FORCE_INF)...")
ret, _, lastErr := proc.Call(
0, // hwndParent = NULL
uintptr(unsafe.Pointer(infUTF16)),
DIIRFLAG_FORCE_INF,
uintptr(unsafe.Pointer(&needReboot)),
)
fmt.Printf("Return value: %d\n", ret)
fmt.Printf("NeedReboot: %d\n", needReboot)
fmt.Printf("LastError: %v\n", lastErr)
if ret == 0 {
fmt.Println("FAILED - DiInstallDriverW returned FALSE")
// Get actual error code
errCode := lastErr.(syscall.Errno)
fmt.Printf("Error code: %d (0x%X)\n", errCode, errCode)
errno := lastErr.(syscall.Errno)
fmt.Printf("FAILED - error: %v (code: %d / 0x%08X)\n", lastErr, uint32(errno), uint32(errno))
} else {
fmt.Println("SUCCESS - driver installed!")
}