Skip to content

Commit

Permalink
V2 bugfix/windows syscall go 1.23 (#3707)
Browse files Browse the repository at this point in the history
* Initial test

* Fix calls

* Update changelog.mdx

* Free allocated strings

* Misc fixes
  • Loading branch information
leaanthony authored Aug 28, 2024
1 parent 8e810d5 commit 475534c
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 65 deletions.
8 changes: 8 additions & 0 deletions .replit
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
modules = ["go-1.21", "web", "nodejs-20"]
run = "go run v2/cmd/wails/main.go"

[nix]
channel = "stable-24_05"

[deployment]
run = ["sh", "-c", "go run v2/cmd/wails/main.go"]
6 changes: 6 additions & 0 deletions v2/internal/go-common-file-dialog/cfd/DialogConfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

package cfd

import "fmt"

type FileFilter struct {
// The display name of the filter (That is shown to the user)
DisplayName string
Expand Down Expand Up @@ -102,6 +104,10 @@ func (config *DialogConfig) apply(dialog Dialog) (err error) {
}

if config.SelectedFileFilterIndex != 0 {
if config.SelectedFileFilterIndex > uint(len(fileFilters)) {
err = fmt.Errorf("selected file filter index out of range")
return
}
err = dialog.SetSelectedFileFilterIndex(config.SelectedFileFilterIndex)
if err != nil {
return
Expand Down
9 changes: 6 additions & 3 deletions v2/internal/go-common-file-dialog/cfd/iShellItem.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ type iShellItemVtbl struct {
func newIShellItem(path string) (*iShellItem, error) {
var shellItem *iShellItem
pathPtr := ole.SysAllocString(path)
defer func(v *int16) {
_ = ole.SysFreeString(v)
}(pathPtr)

ret, _, _ := procSHCreateItemFromParsingName.Call(
uintptr(unsafe.Pointer(pathPtr)),
0,
Expand All @@ -40,10 +44,9 @@ func newIShellItem(path string) (*iShellItem, error) {

func (vtbl *iShellItemVtbl) getDisplayName(objPtr unsafe.Pointer) (string, error) {
var ptr *uint16
ret, _, _ := syscall.Syscall(vtbl.GetDisplayName,
2,
ret, _, _ := syscall.SyscallN(vtbl.GetDisplayName,
uintptr(objPtr),
0x80058000, // SIGDN_FILESYSPATH
0x80058000, // SIGDN_FILESYSPATH,
uintptr(unsafe.Pointer(&ptr)))
if err := hresultToError(ret); err != nil {
return "", err
Expand Down
9 changes: 3 additions & 6 deletions v2/internal/go-common-file-dialog/cfd/iShellItemArray.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,9 @@ type iShellItemArrayVtbl struct {

func (vtbl *iShellItemArrayVtbl) getCount(objPtr unsafe.Pointer) (uintptr, error) {
var count uintptr
ret, _, _ := syscall.Syscall(vtbl.GetCount,
1,
ret, _, _ := syscall.SyscallN(vtbl.GetCount,
uintptr(objPtr),
uintptr(unsafe.Pointer(&count)),
0)
uintptr(unsafe.Pointer(&count)))
if err := hresultToError(ret); err != nil {
return 0, err
}
Expand All @@ -51,8 +49,7 @@ func (vtbl *iShellItemArrayVtbl) getCount(objPtr unsafe.Pointer) (uintptr, error

func (vtbl *iShellItemArrayVtbl) getItemAt(objPtr unsafe.Pointer, index uintptr) (string, error) {
var shellItem *iShellItem
ret, _, _ := syscall.Syscall(vtbl.GetItemAt,
2,
ret, _, _ := syscall.SyscallN(vtbl.GetItemAt,
uintptr(objPtr),
index,
uintptr(unsafe.Pointer(&shellItem)))
Expand Down
110 changes: 54 additions & 56 deletions v2/internal/go-common-file-dialog/cfd/vtblCommonFunc.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//go:build windows
// +build windows

package cfd

Expand All @@ -19,20 +18,16 @@ func hresultToError(hr uintptr) error {
}

func (vtbl *iUnknownVtbl) release(objPtr unsafe.Pointer) error {
ret, _, _ := syscall.Syscall(vtbl.Release,
0,
ret, _, _ := syscall.SyscallN(vtbl.Release,
uintptr(objPtr),
0,
0)
return hresultToError(ret)
}

func (vtbl *iModalWindowVtbl) show(objPtr unsafe.Pointer, hwnd uintptr) error {
ret, _, _ := syscall.Syscall(vtbl.Show,
1,
ret, _, _ := syscall.SyscallN(vtbl.Show,
uintptr(objPtr),
hwnd,
0)
hwnd)
return hresultToError(ret)
}

Expand All @@ -49,8 +44,16 @@ func (vtbl *iFileDialogVtbl) setFileTypes(objPtr unsafe.Pointer, filters []FileF
pszSpec: ole.SysAllocString(filter.Pattern),
}
}
ret, _, _ := syscall.Syscall(vtbl.SetFileTypes,
2,

// Ensure memory is freed after use
defer func() {
for _, spec := range comDlgFilterSpecs {
ole.SysFreeString(spec.pszName)
ole.SysFreeString(spec.pszSpec)
}
}()

ret, _, _ := syscall.SyscallN(vtbl.SetFileTypes,
uintptr(objPtr),
uintptr(cFileTypes),
uintptr(unsafe.Pointer(&comDlgFilterSpecs[0])))
Expand Down Expand Up @@ -82,21 +85,17 @@ func (vtbl *iFileDialogVtbl) setFileTypes(objPtr unsafe.Pointer, filters []FileF
// FOS_FORCEPREVIEWPANEON = 0x40000000,
// FOS_SUPPORTSTREAMABLEITEMS = 0x80000000
func (vtbl *iFileDialogVtbl) setOptions(objPtr unsafe.Pointer, options uint32) error {
ret, _, _ := syscall.Syscall(vtbl.SetOptions,
1,
ret, _, _ := syscall.SyscallN(vtbl.SetOptions,
uintptr(objPtr),
uintptr(options),
0)
uintptr(options))
return hresultToError(ret)
}

func (vtbl *iFileDialogVtbl) getOptions(objPtr unsafe.Pointer) (uint32, error) {
var options uint32
ret, _, _ := syscall.Syscall(vtbl.GetOptions,
1,
ret, _, _ := syscall.SyscallN(vtbl.GetOptions,
uintptr(objPtr),
uintptr(unsafe.Pointer(&options)),
0)
uintptr(unsafe.Pointer(&options)))
return options, hresultToError(ret)
}

Expand All @@ -122,11 +121,9 @@ func (vtbl *iFileDialogVtbl) setDefaultFolder(objPtr unsafe.Pointer, path string
return err
}
defer shellItem.vtbl.release(unsafe.Pointer(shellItem))
ret, _, _ := syscall.Syscall(vtbl.SetDefaultFolder,
1,
ret, _, _ := syscall.SyscallN(vtbl.SetDefaultFolder,
uintptr(objPtr),
uintptr(unsafe.Pointer(shellItem)),
0)
uintptr(unsafe.Pointer(shellItem)))
return hresultToError(ret)
}

Expand All @@ -136,40 +133,32 @@ func (vtbl *iFileDialogVtbl) setFolder(objPtr unsafe.Pointer, path string) error
return err
}
defer shellItem.vtbl.release(unsafe.Pointer(shellItem))
ret, _, _ := syscall.Syscall(vtbl.SetFolder,
1,
ret, _, _ := syscall.SyscallN(vtbl.SetFolder,
uintptr(objPtr),
uintptr(unsafe.Pointer(shellItem)),
0)
uintptr(unsafe.Pointer(shellItem)))
return hresultToError(ret)
}

func (vtbl *iFileDialogVtbl) setTitle(objPtr unsafe.Pointer, title string) error {
titlePtr := ole.SysAllocString(title)
ret, _, _ := syscall.Syscall(vtbl.SetTitle,
1,
defer ole.SysFreeString(titlePtr) // Ensure the string is freed
ret, _, _ := syscall.SyscallN(vtbl.SetTitle,
uintptr(objPtr),
uintptr(unsafe.Pointer(titlePtr)),
0)
uintptr(unsafe.Pointer(titlePtr)))
return hresultToError(ret)
}

func (vtbl *iFileDialogVtbl) close(objPtr unsafe.Pointer) error {
ret, _, _ := syscall.Syscall(vtbl.Close,
1,
uintptr(objPtr),
0,
0)
ret, _, _ := syscall.SyscallN(vtbl.Close,
uintptr(objPtr))
return hresultToError(ret)
}

func (vtbl *iFileDialogVtbl) getResult(objPtr unsafe.Pointer) (*iShellItem, error) {
var shellItem *iShellItem
ret, _, _ := syscall.Syscall(vtbl.GetResult,
1,
ret, _, _ := syscall.SyscallN(vtbl.GetResult,
uintptr(objPtr),
uintptr(unsafe.Pointer(&shellItem)),
0)
uintptr(unsafe.Pointer(&shellItem)))
return shellItem, hresultToError(ret)
}

Expand All @@ -186,42 +175,51 @@ func (vtbl *iFileDialogVtbl) getResultString(objPtr unsafe.Pointer) (string, err
}

func (vtbl *iFileDialogVtbl) setClientGuid(objPtr unsafe.Pointer, guid *ole.GUID) error {
ret, _, _ := syscall.Syscall(vtbl.SetClientGuid,
1,
// Ensure the GUID is not nil
if guid == nil {
return fmt.Errorf("guid cannot be nil")
}

// Call the SetClientGuid method
ret, _, _ := syscall.SyscallN(vtbl.SetClientGuid,
uintptr(objPtr),
uintptr(unsafe.Pointer(guid)),
0)
uintptr(unsafe.Pointer(guid)))

// Convert the HRESULT to a Go error
return hresultToError(ret)
}

func (vtbl *iFileDialogVtbl) setDefaultExtension(objPtr unsafe.Pointer, defaultExtension string) error {
if defaultExtension[0] == '.' {
// Ensure the string is not empty before accessing the first character
if len(defaultExtension) > 0 && defaultExtension[0] == '.' {
defaultExtension = strings.TrimPrefix(defaultExtension, ".")
}

// Allocate memory for the default extension string
defaultExtensionPtr := ole.SysAllocString(defaultExtension)
ret, _, _ := syscall.Syscall(vtbl.SetDefaultExtension,
1,
defer ole.SysFreeString(defaultExtensionPtr) // Ensure the string is freed

// Call the SetDefaultExtension method
ret, _, _ := syscall.SyscallN(vtbl.SetDefaultExtension,
uintptr(objPtr),
uintptr(unsafe.Pointer(defaultExtensionPtr)),
0)
uintptr(unsafe.Pointer(defaultExtensionPtr)))

// Convert the HRESULT to a Go error
return hresultToError(ret)
}

func (vtbl *iFileDialogVtbl) setFileName(objPtr unsafe.Pointer, fileName string) error {
fileNamePtr := ole.SysAllocString(fileName)
ret, _, _ := syscall.Syscall(vtbl.SetFileName,
1,
defer ole.SysFreeString(fileNamePtr) // Ensure the string is freed
ret, _, _ := syscall.SyscallN(vtbl.SetFileName,
uintptr(objPtr),
uintptr(unsafe.Pointer(fileNamePtr)),
0)
uintptr(unsafe.Pointer(fileNamePtr)))
return hresultToError(ret)
}

func (vtbl *iFileDialogVtbl) setSelectedFileFilterIndex(objPtr unsafe.Pointer, index uint) error {
ret, _, _ := syscall.Syscall(vtbl.SetFileTypeIndex,
1,
ret, _, _ := syscall.SyscallN(vtbl.SetFileTypeIndex,
uintptr(objPtr),
uintptr(index+1), // SetFileTypeIndex counts from 1
0)
uintptr(index+1)) // SetFileTypeIndex counts from 1
return hresultToError(ret)
}
1 change: 1 addition & 0 deletions website/src/pages/changelog.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added nil check for Drag-n-Drop on Windows. Fixed by in [PR](https://github.com/wailsapp/wails/pull/3597) by @leaanthony based on the suggestion by @Alpa-1 in [#3596](https://github.com/wailsapp/wails/issues/3596).
- Fixed typos in various .mdx files. [PR #3628](https://github.com/wailsapp/wails/pull/3628) by [@deining](https://github.com/deining)
- Fixed `notifyListeners()` race condition when terminated mid-emission [PR](https://github.com/wailsapp/wails/pull/3695) by [@mrf345](https://github.com/mrf345)
- Fixed dialogs in Windows when using Go 1.23 in [PR](https://github.com/wailsapp/wails/pull/3707) by [@leaanthony](https://github.com/leaanthony)
- Fixed drag and drop missing cursor icon [PR](https://github.com/wailsapp/wails/pull/3703) by [@mrf345](https://github.com/mrf345)

### Changed
Expand Down

0 comments on commit 475534c

Please sign in to comment.