package win32api import ( "fmt" "syscall" "testing" "unsafe" ) func TestClipboardReadText(t *testing.T) { if err := OpenClipboard(0); err != nil { t.Error(err) } formats, err := EnumAllClipboardFormats() if err != nil { t.Error(err) } tp := CF_UNICODETEXT for _, f := range formats { fmt.Println("Clipboard Format:", f) d, e := GetClipboardFormatName(f) fmt.Println("Clipboard Format Name:", d, e) if d == "HTML Format" { tp = RegisterClipboardFormat("HTML Format") fmt.Println("HTML Format:", tp) } if d == "DataObject" { tp = CF_HDROP fmt.Println("DataObject:", tp) } } mem, err := GetClipboardData(tp) if err != nil { t.Error(err) } p, err := GlobalLock(mem) if err != nil { t.Error(err) } defer GlobalUnlock(mem) //n := 0 if tp == 13 { var buf []uint16 for ptr := unsafe.Pointer(p); *(*uint16)(ptr) != 0; ptr = unsafe.Pointer(uintptr(ptr) + unsafe.Sizeof(*((*uint16)(unsafe.Pointer(p))))) { buf = append(buf, *(*uint16)(ptr)) } str := syscall.UTF16ToString(buf) fmt.Println("\n\nClipboard Data:", str) if err := CloseClipboard(); err != nil { t.Error(err) } return } size, err := GlobalSize(mem) if err != nil { t.Error(err) } var buf []byte for i := 0; i < int(size); i++ { buf = append(buf, *(*byte)(unsafe.Pointer(uintptr(p) + uintptr(i)))) } fmt.Println("\n\nClipboard Data:", string(buf)) fmt.Println("Clipboard Test Done") }