golang-design / clipboard

📋 cross-platform clipboard package that supports accessing text and image in Go (macOS/Linux/Windows/Android/iOS)
https://golang.design/x/clipboard
MIT License
579 stars 64 forks source link

Windows 32-bit system will get stuck when executing. #45

Open c17abab opened 1 year ago

c17abab commented 1 year ago

The compilation parameters are as follows: $env:GOARCH="amd64"; $env:GOOS="windows"; $env:GOARCH="386"; # 32

$env:GOARCH="amd64"; # 64

and the program will be stucked at line 324 in the file clipborad_windows.go:

println("test in read 2")
// try again until open clipboard successed
   // will stuck here
for {
    r, _, _ = openClipboard.Call()
    if r == 0 {
        continue
    }
    break
}
    //  the following code will not be executed.
println("test in read 3")
changkun commented 1 year ago

Good point. Is there any specific use case for 32-bit windows? I am 99% sure that this package didn't consider 32-bit windows.

c17abab commented 1 year ago

Good point. Is there any specific use case for 32-bit windows? I am 99% sure that this package didn't consider 32-bit windows.

Found the cause of the problem, the user32.dll function openClipboard need a parameter(A handle to the window to be associated with the open clipboard. If this parameter is NULL, the open clipboard is associated with the current task). So maybe you need to get the window handle first, or set the parameter to 0: https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-openclipboard

eg:


    // Here you need to get the handle of the clipboard window first
    getOpenClipboardWindow := user32.MustFindProc("GetOpenClipboardWindow")
    for {
        r, _, _ = getOpenClipboardWindow.Call()
                // set the parameter to 0 is also ok
        r, _, _ = openClipboard.Call(r)
        if r == 0 {
            continue
        }
        break
    }
    defer closeClipboard.Call()
changkun commented 1 year ago

Thanks for the investigation. As you already investigated the issue, any PR would be very welcome 👍