{"record":{"id":"ef9dda8a838c806b","repo":"wailsapp/wails","slug":"syscall-getlasterror","errorCode":null,"errorMessage":"syscall.GetLastError()","messagePattern":"syscall\\.GetLastError\\(\\)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"v3/pkg/w32/window.go","lineNumber":298,"sourceCode":"\t\treturn classInstance, nil\n\t}\n\tapplicationInstance := GetModuleHandle(\"\")\n\tif applicationInstance == 0 {\n\t\treturn 0, fmt.Errorf(\"get module handle failed\")\n\t}\n\n\tvar wc WNDCLASSEX\n\twc.Size = uint32(unsafe.Sizeof(wc))\n\twc.WndProc = syscall.NewCallback(proc)\n\twc.Instance = applicationInstance\n\twc.Icon = LoadIconWithResourceID(0, uint16(IDI_APPLICATION))\n\twc.Cursor = LoadCursorWithResourceID(0, uint16(IDC_ARROW))\n\twc.Background = COLOR_BTNFACE + 1\n\twc.ClassName = MustStringToUTF16Ptr(name)\n\n\tatom := RegisterClassEx(&wc)\n\tif atom == 0 {\n\t\tpanic(syscall.GetLastError())\n\t}\n\n\tsetWindowClass(name, applicationInstance)\n\n\treturn applicationInstance, nil\n}\n\nfunc FlashWindow(hwnd HWND, enabled bool) {\n\tvar flashInfo FLASHWINFO\n\tflashInfo.CbSize = uint32(unsafe.Sizeof(flashInfo))\n\tflashInfo.Hwnd = hwnd\n\tif enabled {\n\t\tflashInfo.DwFlags = FLASHW_ALL | FLASHW_TIMERNOFG\n\t} else {\n\t\tflashInfo.DwFlags = FLASHW_STOP\n\t}\n\t_, _, _ = procFlashWindowEx.Call(uintptr(unsafe.Pointer(&flashInfo)))\n}","sourceCodeStart":280,"sourceCodeEnd":316,"githubUrl":"https://github.com/wailsapp/wails/blob/0e754b1b40ba9044c2a1460e23b7c3de20fc5cf3/v3/pkg/w32/window.go#L280-L316","documentation":"RegisterWindow panicked because RegisterClassEx returned atom 0. The most common cause is ERROR_CLASS_ALREADY_EXISTS (1410): the window class name is already registered in the process — by a previous, not-yet-unregistered registration (the guard map in window.go only tracks classes registered through this helper). The panic value is syscall.GetLastError(), which is also unreliable in Go because the runtime may issue syscalls between the failing call and the check.","triggerScenarios":"Calling w32.RegisterWindow twice with the same class name from different code paths that bypass the windowClasses map (or after the map was cleared in tests); registering a class after the process previously used the same name without UnregisterClass; the ClassName UTF-16 buffer being invalid.","commonSituations":"Running the Wails app and an embedded second window system in the same process; test binaries that start/stop the application multiple times; class-name collisions when two libraries pick the same default name.","solutions":["Route every registration through the same name (the helper already dedupes via getWindowClass), or check GetLastError for ERROR_CLASS_ALREADY_EXISTS and treat it as success","Call UnregisterClass(name, instance) during shutdown before re-registering in the same process","Use a unique class name (append PID or a random suffix) when collisions are possible","Capture the error with the windows errno immediately (e.g. use w32 helper or syscall.Errno captured right after the call), not a deferred GetLastError"],"exampleFix":"// before\natom := RegisterClassEx(&wc)\nif atom == 0 {\n    panic(syscall.GetLastError())\n}\n\n// after\natom := RegisterClassEx(&wc)\nif atom == 0 {\n    if errno.ERROR_CLASS_ALREADY_EXISTS == w32.GetLastErrorImmediate() {\n        setWindowClass(name, applicationInstance)\n        return applicationInstance, nil\n    }\n    return 0, fmt.Errorf(\"RegisterClassEx failed: %w\", err)\n}","handlingStrategy":"validation","validationCode":"// dedupe before registering\nif _, exists := getWindowClass(name); exists {\n    return existing, nil\n}\n// treat already-registered as success\natom := w32.RegisterClassEx(&wc)\nif atom == 0 && w32.GetLastError() == 1410 { // ERROR_CLASS_ALREADY_EXISTS\n    setWindowClass(name, hInst)\n    return hInst, nil\n}","typeGuard":null,"tryCatchPattern":"defer func() {\n    if r := recover(); r != nil {\n        err = fmt.Errorf(\"RegisterClassEx(%q) failed: %v\", name, r)\n    }\n}()","preventionTips":["Route all class registrations through one deduplicating helper","Unregister classes on shutdown if the process restarts windowing","Use unique class names (suffix PID) when colliding with other libraries"],"tags":["windows","win32","window-class","startup","panic"],"backgroundTag":null,"analyzedSha":"0e754b1b40ba9044c2a1460e23b7c3de20fc5cf3","analyzedAt":"2026-08-15T14:17:36.034Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}