Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Windows 11 identification in wails doctor #3891

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion v2/cmd/wails/doctor.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package main

import (
"fmt"
"github.com/wailsapp/wails/v2/internal/shell"
"runtime"
"runtime/debug"
"strconv"
"strings"

"github.com/wailsapp/wails/v2/internal/shell"

"github.com/pterm/pterm"

"github.com/jaypipes/ghw"
Expand Down Expand Up @@ -78,6 +79,7 @@ func diagnoseEnvironment(f *flags.Doctor) error {
{pterm.Bold.Sprint("OS"), info.OS.Name},
{pterm.Bold.Sprint("Version"), info.OS.Version},
{pterm.Bold.Sprint("ID"), info.OS.ID},
{pterm.Bold.Sprint("Branding"), info.OS.Branding},
{pterm.Bold.Sprint("Go Version"), runtime.Version()},
{pterm.Bold.Sprint("Platform"), runtime.GOOS},
{pterm.Bold.Sprint("Architecture"), runtime.GOARCH},
Expand Down
7 changes: 4 additions & 3 deletions v2/internal/system/operatingsystem/os.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package operatingsystem

// OS contains information about the operating system
type OS struct {
ID string
Name string
Version string
ID string
Name string
Version string
Branding string
}

// Info retrieves information about the current platform
Expand Down
35 changes: 35 additions & 0 deletions v2/internal/system/operatingsystem/os_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,44 @@ package operatingsystem

import (
"fmt"
"strings"
"syscall"
"unsafe"

"golang.org/x/sys/windows"
"golang.org/x/sys/windows/registry"
)

func stripNulls(str string) string {
// Split the string into substrings at each null character
substrings := strings.Split(str, "\x00")

// Join the substrings back into a single string
strippedStr := strings.Join(substrings, "")

return strippedStr
}

func mustStringToUTF16Ptr(input string) *uint16 {
input = stripNulls(input)
result, err := syscall.UTF16PtrFromString(input)
if err != nil {
panic(err)
}
return result
}
Comment on lines +25 to +32
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Consider error handling instead of panic.

The mustStringToUTF16Ptr function panics on error, which could make the application unstable. Consider handling the error gracefully.

-func mustStringToUTF16Ptr(input string) *uint16 {
+func stringToUTF16Ptr(input string) (*uint16, error) {
     input = stripNulls(input)
     result, err := syscall.UTF16PtrFromString(input)
-    if err != nil {
-        panic(err)
-    }
-    return result
+    return result, err
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
func mustStringToUTF16Ptr(input string) *uint16 {
input = stripNulls(input)
result, err := syscall.UTF16PtrFromString(input)
if err != nil {
panic(err)
}
return result
}
func stringToUTF16Ptr(input string) (*uint16, error) {
input = stripNulls(input)
result, err := syscall.UTF16PtrFromString(input)
return result, err
}


func getBranding() string {
var modBranding = syscall.NewLazyDLL("winbrand.dll")
var brandingFormatString = modBranding.NewProc("BrandingFormatString")

windowsLong := mustStringToUTF16Ptr("%WINDOWS_LONG%\x00")
ret, _, _ := brandingFormatString.Call(
uintptr(unsafe.Pointer(windowsLong)),
)
return windows.UTF16PtrToString((*uint16)(unsafe.Pointer(ret)))
}

func platformInfo() (*OS, error) {
// Default value
var result OS
Expand All @@ -27,6 +61,7 @@ func platformInfo() (*OS, error) {
result.Name = productName
result.Version = fmt.Sprintf("%s (Build: %s)", releaseId, currentBuild)
result.ID = displayVersion
result.Branding = getBranding()

return &result, key.Close()
}
1 change: 1 addition & 0 deletions website/src/pages/changelog.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added
- Added option to set window class name on Windows. Added in [PR](https://github.com/wailsapp/wails/pull/3828) by @APshenkin
- Added "Branding" section to `wails doctor` to correctly identify Windows 11 [#3891](https://github.com/wailsapp/wails/pull/3891) by [@ronen25](https://github.com/ronen25)

### Fixed
- Fixed dev mode logging bug by @attperac in [#3972](https://wailsapp/wails/pull/3972)
Expand Down