Files
Reims-Go/internal/host/scheduling_windows.go
T
NwflowerandClaude Opus 5.5 4cf1e8ea37 feat(host): open Windows processor scheduling as administrator
The host page's optimizations list gains a "Windows processor scheduling"
row: it shows whether Windows favours programs or background services
(Win32PrioritySeparation, read without elevation) and a button that opens
System Properties' Performance Options through one UAC prompt, so the
choice saves without a second one. Declining the prompt is not an error.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-29 00:31:18 -07:00

72 lines
2.2 KiB
Go

//go:build windows
package host
import (
"errors"
"fmt"
"syscall"
"unsafe"
)
const (
regDWORD = 4
priorityControlKey = `SYSTEM\CurrentControlSet\Control\PriorityControl`
prioritySeparationValue = "Win32PrioritySeparation"
// ShellExecuteW's result when the person declines the UAC prompt.
seErrAccessDenied = 5
)
// ErrElevationDeclined: the person said no at the UAC prompt.
var ErrElevationDeclined = errors.New("UAC prompt declined")
// probeProcessorScheduling returns SchedulingPrograms or SchedulingBackground,
// or "" where the value cannot be read. Reading needs no elevation.
func probeProcessorScheduling() string {
v, ok := regReadDWORD(hkeyLocalMachine, priorityControlKey, prioritySeparationValue)
if !ok {
return ""
}
return SchedulingFromPrioritySeparation(v)
}
// OpenPerformanceOptionsElevated opens System Properties' Performance Options
// through a UAC prompt, so the processor scheduling choice saves without a
// second prompt. It returns once the dialog is launched.
func OpenPerformanceOptionsElevated() error {
verb, _ := syscall.UTF16PtrFromString("runas")
file, _ := syscall.UTF16PtrFromString("SystemPropertiesPerformance.exe")
r, _, err := procShellExecuteW.Call(0, uintptr(unsafe.Pointer(verb)), uintptr(unsafe.Pointer(file)), 0, 0, 1 /* SW_SHOWNORMAL */)
switch {
case r == seErrAccessDenied:
return ErrElevationDeclined
case r <= 32:
return fmt.Errorf("ShellExecuteW runas SystemPropertiesPerformance failed (code %d): %w", r, err)
}
return nil
}
func regReadDWORD(root uintptr, subKey, valueName string) (uint32, bool) {
sk, err := syscall.UTF16PtrFromString(subKey)
if err != nil {
return 0, false
}
var hKey uintptr
if r, _, _ := procRegOpenKeyExW.Call(root, uintptr(unsafe.Pointer(sk)), 0, keyRead, uintptr(unsafe.Pointer(&hKey))); r != 0 {
return 0, false
}
defer procRegCloseKey.Call(hKey)
vn, err := syscall.UTF16PtrFromString(valueName)
if err != nil {
return 0, false
}
var typ uint32
var v uint32
size := uint32(unsafe.Sizeof(v))
r, _, _ := procRegQueryValueExW.Call(hKey, uintptr(unsafe.Pointer(vn)), 0, uintptr(unsafe.Pointer(&typ)), uintptr(unsafe.Pointer(&v)), uintptr(unsafe.Pointer(&size)))
if r != 0 || typ != regDWORD {
return 0, false
}
return v, true
}