1
0

fix: Windows writeAll error

This commit is contained in:
502647092 2018-08-30 22:04:24 +08:00
parent 3029507430
commit 04b2fb1805
2 changed files with 42 additions and 113 deletions

View File

@ -7,126 +7,55 @@
package clipboard package clipboard
import ( import (
"errors"
"syscall" "syscall"
"time"
"unsafe" "unsafe"
"github.com/lxn/win"
) )
const (
cfUnicodetext = 13
// gmemFixed = 0x0000
gmemMoveable = 0x0002
)
var (
user32 = syscall.MustLoadDLL("user32")
openClipboard = user32.MustFindProc("OpenClipboard")
closeClipboard = user32.MustFindProc("CloseClipboard")
emptyClipboard = user32.MustFindProc("EmptyClipboard")
getClipboardData = user32.MustFindProc("GetClipboardData")
setClipboardData = user32.MustFindProc("SetClipboardData")
kernel32 = syscall.NewLazyDLL("kernel32")
globalAlloc = kernel32.NewProc("GlobalAlloc")
globalFree = kernel32.NewProc("GlobalFree")
globalLock = kernel32.NewProc("GlobalLock")
globalUnlock = kernel32.NewProc("GlobalUnlock")
lstrcpy = kernel32.NewProc("lstrcpyW")
)
// waitOpenClipboard opens the clipboard, waiting for up to a second to do so.
func waitOpenClipboard() error {
started := time.Now()
limit := started.Add(time.Second)
var r uintptr
var err error
for time.Now().Before(limit) {
r, _, err = openClipboard.Call(0)
if r != 0 {
return nil
}
time.Sleep(time.Millisecond)
}
return err
}
func readAll() (string, error) { func readAll() (string, error) {
// r, _, err := openClipboard.Call(0) if !win.OpenClipboard(0) {
err := waitOpenClipboard() return "", errors.New("OpenClipboard")
if err != nil {
return "", err
} }
defer closeClipboard.Call() defer win.CloseClipboard()
hMem := win.HGLOBAL(win.GetClipboardData(win.CF_UNICODETEXT))
h, _, err := getClipboardData.Call(cfUnicodetext) if hMem == 0 {
if h == 0 { return "", errors.New("GetClipboardData")
return "", err
} }
p := win.GlobalLock(hMem)
l, _, err := globalLock.Call(h) if p == nil {
if l == 0 { return "", errors.New("GlobalLock()")
return "", err
} }
defer win.GlobalUnlock(hMem)
text := syscall.UTF16ToString((*[1 << 20]uint16)(unsafe.Pointer(l))[:]) text := win.UTF16PtrToString((*uint16)(p))
r, _, err := globalUnlock.Call(h)
if r == 0 {
return "", err
}
return text, nil return text, nil
} }
func writeAll(text string) error { func writeAll(text string) error {
err := waitOpenClipboard() if !win.OpenClipboard(0) {
return errors.New("OpenClipboard")
}
defer win.CloseClipboard()
utf16, err := syscall.UTF16FromString(text)
if err != nil { if err != nil {
return err return err
} }
defer closeClipboard.Call() hMem := win.GlobalAlloc(win.GMEM_MOVEABLE, uintptr(len(utf16)*2))
if hMem == 0 {
r, _, err := emptyClipboard.Call(0) return errors.New("GlobalAlloc")
if r == 0 {
return err
} }
p := win.GlobalLock(hMem)
data := syscall.StringToUTF16(text) if p == nil {
return errors.New("GlobalLock()")
// "If the hMem parameter identifies a memory object, the object must have
// been allocated using the function with the GMEM_MOVEABLE flag."
h, _, err := globalAlloc.Call(gmemMoveable,
uintptr(len(data)*int(unsafe.Sizeof(data[0]))))
if h == 0 {
return err
} }
defer func() { win.MoveMemory(p, unsafe.Pointer(&utf16[0]), uintptr(len(utf16)*2))
if h != 0 { win.GlobalUnlock(hMem)
globalFree.Call(h) if 0 == win.SetClipboardData(win.CF_UNICODETEXT, win.HANDLE(hMem)) {
} // We need to free hMem.
}() defer win.GlobalFree(hMem)
return errors.New("SetClipboardData")
l, _, err := globalLock.Call(h)
if l == 0 {
return err
} }
// The system now owns the memory referred to by hMem.
r, _, err = lstrcpy.Call(l, uintptr(unsafe.Pointer(&data[0])))
if r == 0 {
return err
}
r, _, err = globalUnlock.Call(h)
if r == 0 {
return err
}
r, _, err = setClipboardData.Call(cfUnicodetext, h)
if r == 0 {
if err.(syscall.Errno) != 0 {
return err
}
}
h = 0 // suppress deferred cleanup
return nil return nil
} }

20
main.go
View File

@ -27,22 +27,22 @@ func main() {
} }
const ( const (
TextHeader = "Go-Clipboard-Text" textHeader = "Go-Clipboard-Text"
TimeHeader = "Go-Clipboard-Time" timeHeader = "Go-Clipboard-Time"
) )
func startServer(b string) { func startServer(b string) {
text := "" text := ""
time := time.Now().Unix() time := time.Now().Unix()
var err error g := gin.New()
g := gin.Default()
g.GET("/", func(c *gin.Context) { g.GET("/", func(c *gin.Context) {
c.Header(TextHeader, text) c.Header(textHeader, text)
c.Header(TimeHeader, strconv.FormatInt(time, 10)) c.Header(timeHeader, strconv.FormatInt(time, 10))
}) })
g.POST("/", func(c *gin.Context) { g.POST("/", func(c *gin.Context) {
text = c.Query("text") text = c.Query("text")
time, err = strconv.ParseInt(c.Query("time"), 10, 64) time, _ = strconv.ParseInt(c.Query("time"), 10, 64)
fmt.Printf("Update Clipboard: %s\n", text)
}) })
g.Run(b) g.Run(b)
} }
@ -63,11 +63,11 @@ func readClipboard(a string) {
client.Do(request) client.Do(request)
} else { } else {
response, _ := http.Get(a) response, _ := http.Get(a)
texts, ok := response.Header[TextHeader] texts, ok := response.Header[textHeader]
if ok { if ok {
sText := texts[0] sText := texts[0]
sTime, _ := strconv.ParseInt(response.Header[TimeHeader][0], 10, 64) sTime, _ := strconv.ParseInt(response.Header[timeHeader][0], 10, 64)
if sTime > updateTime { if sTime > updateTime && sText != old {
old = sText old = sText
updateTime = time.Now().Unix() updateTime = time.Now().Unix()
clipboard.WriteAll(sText) clipboard.WriteAll(sText)