package win32api import ( "fmt" "reflect" "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 fmt.Println("lenghth", len(formats)) 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 = f fmt.Println("DataObject:", tp) } if f > 20000 { tp = f } } 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) } fmt.Println("Size:", size) 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:", "ok") fmt.Println("Clipboard Test Done") } func TestGetUpdatedClipboardFormatsAll(t *testing.T) { d, e := GetUpdatedClipboardFormatsAll() if e != nil { t.Error(e) } fmt.Println(d) } func TestGetUpdatedClipboardFormatsAllRetriesOnInsufficientBuffer(t *testing.T) { calls := 0 got, err := getUpdatedClipboardFormatsAll(func(lpuiFormats unsafe.Pointer, cFormats int, pcFormats unsafe.Pointer) (int, error) { calls++ count := (*uint32)(pcFormats) switch calls { case 1: if cFormats != 32 { t.Fatalf("first call cFormats = %d, want 32", cFormats) } *count = 40 return 0, syscall.ERROR_INSUFFICIENT_BUFFER case 2: if cFormats != 40 { t.Fatalf("second call cFormats = %d, want 40", cFormats) } *count = 3 formats := (*[1 << 12]uint32)(lpuiFormats)[:cFormats:cFormats] formats[0] = uint32(CF_TEXT) formats[1] = uint32(CF_UNICODETEXT) formats[2] = 0xC000 return 1, nil default: t.Fatalf("unexpected call count %d", calls) return 0, nil } }) if err != nil { t.Fatalf("getUpdatedClipboardFormatsAll failed: %v", err) } want := []DWORD{CF_TEXT, CF_UNICODETEXT, 0xC000} if !reflect.DeepEqual(got, want) { t.Fatalf("formats = %v, want %v", got, want) } if calls != 2 { t.Fatalf("calls = %d, want 2", calls) } }