wailsapp/wails · error

GetSysColorBrush failed

Error message

GetSysColorBrush failed

What it means

NewSystemColorBrush wraps GetSysColorBrush and panics when the Win32 call returns NULL for the given system color index. GetSysColorBrush returns NULL only for an out-of-range index; valid indices are the COLOR_* constants 0..30 (COLOR_BTNFACE=15, etc.). Note that the winc default DefaultBackgroundBrush is created at package init with COLOR_BTNFACE, so a bad constant would crash the whole program at import time.

Source

Thrown at v2/internal/frontend/desktop/windows/winc/brush.go:36

	logBrush w32.LOGBRUSH
}

func NewSolidColorBrush(color Color) *Brush {
	lb := w32.LOGBRUSH{LbStyle: w32.BS_SOLID, LbColor: w32.COLORREF(color)}
	hBrush := w32.CreateBrushIndirect(&lb)
	if hBrush == 0 {
		panic("Faild to create solid color brush")
	}

	return &Brush{hBrush, lb}
}

func NewSystemColorBrush(colorIndex int) *Brush {
	//lb := w32.LOGBRUSH{LbStyle: w32.BS_SOLID, LbColor: w32.COLORREF(colorIndex)}
	lb := w32.LOGBRUSH{LbStyle: w32.BS_NULL}
	hBrush := w32.GetSysColorBrush(colorIndex)
	if hBrush == 0 {
		panic("GetSysColorBrush failed")
	}
	return &Brush{hBrush, lb}
}

func NewHatchedColorBrush(color Color) *Brush {
	lb := w32.LOGBRUSH{LbStyle: w32.BS_HATCHED, LbColor: w32.COLORREF(color)}
	hBrush := w32.CreateBrushIndirect(&lb)
	if hBrush == 0 {
		panic("Faild to create solid color brush")
	}

	return &Brush{hBrush, lb}
}

func NewNullBrush() *Brush {
	lb := w32.LOGBRUSH{LbStyle: w32.BS_NULL}
	hBrush := w32.CreateBrushIndirect(&lb)
	if hBrush == 0 {

View on GitHub (pinned to 0e754b1b40)

Solutions

  1. Pass only w32.COLOR_* constants (COLOR_BTNFACE, COLOR_WINDOW, COLOR_HIGHLIGHT, ...) — never RGB() values — to NewSystemColorBrush
  2. Bounds-check the index against 0..30 before the call (see validation code)
  3. If a system color is unavailable on the target OS, fall back to NewSolidColorBrush with an explicit RGB color instead

Example fix

// before
br := winc.NewSystemColorBrush(0x00FF0000) // RGB mistakenly used as index -> panic

// after
br := winc.NewSystemColorBrush(w32.COLOR_BTNFACE) // valid COLOR_* index 0..30
Defensive patterns

Strategy: validation

Validate before calling

func validSysColorIndex(i int) bool { return i >= 0 && i <= 30 } // COLOR_* range

if validSysColorIndex(idx) {
    br := winc.NewSystemColorBrush(idx)
}

Prevention

When it happens

Trigger: Calling winc.NewSystemColorBrush(n) with an index outside 0..30 (e.g. passing a raw RGB value or a COLORREF instead of a COLOR_* index); passing an index valid on newer Windows versions to an older OS that does not define it.

Common situations: Confusing system color indices (int, 0..30) with Color values (RGB); copy-pasting a magic number from docs for a constant the target OS does not support; typo'd constants defined by hand in the w32 package.

Related errors


AI-assisted analysis of wailsapp/wails@0e754b1b40 (2026-08-15). Data as JSON: /api/errors/44a299ceea1f22df. Report an issue: GitHub.