2024-03-26 11:10:26 +08:00
|
|
|
package win32api
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
2026-06-06 17:46:02 +08:00
|
|
|
"reflect"
|
2024-03-26 11:10:26 +08:00
|
|
|
"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
|
2024-03-26 16:07:49 +08:00
|
|
|
fmt.Println("lenghth", len(formats))
|
2024-03-26 11:10:26 +08:00
|
|
|
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" {
|
2024-03-26 16:07:49 +08:00
|
|
|
tp = f
|
2024-03-26 11:10:26 +08:00
|
|
|
fmt.Println("DataObject:", tp)
|
|
|
|
|
}
|
2024-03-26 16:07:49 +08:00
|
|
|
if f > 20000 {
|
|
|
|
|
tp = f
|
|
|
|
|
}
|
2024-03-26 11:10:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
}
|
2024-03-26 16:07:49 +08:00
|
|
|
fmt.Println("Size:", size)
|
2024-03-26 11:10:26 +08:00
|
|
|
var buf []byte
|
|
|
|
|
for i := 0; i < int(size); i++ {
|
|
|
|
|
buf = append(buf, *(*byte)(unsafe.Pointer(uintptr(p) + uintptr(i))))
|
|
|
|
|
}
|
2024-03-26 16:07:49 +08:00
|
|
|
fmt.Println("\n\nClipboard Data:", "ok")
|
2024-03-26 11:10:26 +08:00
|
|
|
fmt.Println("Clipboard Test Done")
|
|
|
|
|
}
|
2024-03-26 16:07:49 +08:00
|
|
|
|
|
|
|
|
func TestGetUpdatedClipboardFormatsAll(t *testing.T) {
|
|
|
|
|
d, e := GetUpdatedClipboardFormatsAll()
|
|
|
|
|
if e != nil {
|
|
|
|
|
t.Error(e)
|
|
|
|
|
}
|
|
|
|
|
fmt.Println(d)
|
|
|
|
|
}
|
2026-06-06 17:46:02 +08:00
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
}
|