Compare commits

...

8 Commits

@ -282,3 +282,120 @@ func DeviceIoControlPtr(hDevice HANDLE, dwIoControlCode DWORD, lpInBuffer uintpt
} }
return true, nil return true, nil
} }
func GlobalLock(hMem HGLOBAL) (unsafe.Pointer, error) {
kernel32, err := syscall.LoadLibrary("kernel32.dll")
if err != nil {
return nil, err
}
defer syscall.FreeLibrary(kernel32)
gl, err := syscall.GetProcAddress(syscall.Handle(kernel32), "GlobalLock")
if err != nil {
return nil, err
}
r, _, errno := syscall.Syscall(gl, 1, uintptr(hMem), 0, 0)
if r == 0 {
if errno != 0 {
return nil, error(errno)
}
return nil, syscall.EINVAL
}
return unsafe.Pointer(r), nil
}
func GlobalUnlock(hMem HGLOBAL) error {
kernel32, err := syscall.LoadLibrary("kernel32.dll")
if err != nil {
return err
}
defer syscall.FreeLibrary(kernel32)
gu, err := syscall.GetProcAddress(syscall.Handle(kernel32), "GlobalUnlock")
if err != nil {
return err
}
r, _, errno := syscall.Syscall(gu, 1, uintptr(hMem), 0, 0)
if r == 0 {
if errno != 0 {
return error(errno)
}
return syscall.EINVAL
}
return nil
}
func GlobalSize(hMem HGLOBAL) (DWORD, error) {
kernel32, err := syscall.LoadLibrary("kernel32.dll")
if err != nil {
return 0, err
}
defer syscall.FreeLibrary(kernel32)
gs, err := syscall.GetProcAddress(syscall.Handle(kernel32), "GlobalSize")
if err != nil {
return 0, err
}
r, _, errno := syscall.Syscall(gs, 1, uintptr(hMem), 0, 0)
if r == 0 {
if errno != 0 {
return 0, error(errno)
}
return 0, syscall.EINVAL
}
return DWORD(r), nil
}
func GlobalAlloc(uFlags DWORD, dwBytes uintptr) (HGLOBAL, error) {
kernel32, err := syscall.LoadLibrary("kernel32.dll")
if err != nil {
return 0, err
}
defer syscall.FreeLibrary(kernel32)
ga, err := syscall.GetProcAddress(syscall.Handle(kernel32), "GlobalAlloc")
if err != nil {
return 0, err
}
r, _, errno := syscall.Syscall(ga, 2, uintptr(uFlags), dwBytes, 0)
if r == 0 {
if errno != 0 {
return 0, error(errno)
}
return 0, syscall.EINVAL
}
return HGLOBAL(r), nil
}
func GlobalFree(hMem HGLOBAL) error {
kernel32, err := syscall.LoadLibrary("kernel32.dll")
if err != nil {
return err
}
defer syscall.FreeLibrary(kernel32)
gf, err := syscall.GetProcAddress(syscall.Handle(kernel32), "GlobalFree")
if err != nil {
return err
}
r, _, errno := syscall.Syscall(gf, 1, uintptr(hMem), 0, 0)
if r == 0 {
if errno != 0 {
return error(errno)
}
return syscall.EINVAL
}
return nil
}
func RtlMoveMemory(Destination, Source unsafe.Pointer, Length uintptr) error {
kernel32, err := syscall.LoadLibrary("kernel32.dll")
if err != nil {
return err
}
defer syscall.FreeLibrary(kernel32)
rmv, err := syscall.GetProcAddress(syscall.Handle(kernel32), "RtlMoveMemory")
if err != nil {
return err
}
_, _, errno := syscall.Syscall(rmv, 3, uintptr(unsafe.Pointer(Destination)), uintptr(unsafe.Pointer(Source)), Length)
if errno != 0 {
return error(errno)
}
return nil
}

@ -113,3 +113,19 @@ type FILE_ID_DESCRIPTOR struct {
ObjectId DWORDLONG ObjectId DWORDLONG
ExtendedFileId DWORDLONG ExtendedFileId DWORDLONG
} }
const (
GMEM_MOVEABLE = 0x0002
GMEM_ZEROINIT = 0x0040
GMEM_DDESHARE = 0x2000
GMEM_SHARE = GMEM_DDESHARE
GMEM_FIXED = 0x0000
GMEM_DISCARDABLE = 0x0100
GMEM_NOT_BANKED = 0x1000
GMEM_NOTIFY = 0x4000
GMEM_LOWER = GMEM_NOT_BANKED
GMEM_VALID_FLAGS = 0x7F72
GMEM_INVALID_HANDLE = 0x8000
GHND = (GMEM_MOVEABLE | GMEM_ZEROINIT)
GPTR = (GMEM_FIXED | GMEM_ZEROINIT)
)

@ -86,3 +86,17 @@ func ShellExecuteEx(muzika *SHELLEXECUTEINFOW) error {
} }
return nil return nil
} }
func DragQueryFile(hDrop HDROP, iFile DWORD, lpszFile *uint16, cch DWORD) (DWORD, error) {
shell32, err := syscall.LoadLibrary("shell32.dll")
if err != nil {
return 0, err
}
defer syscall.FreeLibrary(shell32)
dqf, err := syscall.GetProcAddress(syscall.Handle(shell32), "DragQueryFileW")
if err != nil {
return 0, err
}
r, _, _ := syscall.Syscall6(dqf, 4, uintptr(hDrop), uintptr(iFile), uintptr(unsafe.Pointer(lpszFile)), uintptr(cch), 0, 0)
return DWORD(r), nil
}

@ -3,6 +3,7 @@ package win32api
import ( import (
"errors" "errors"
"syscall" "syscall"
"unsafe"
) )
func Keybd_event(keyname string, keydown bool) error { func Keybd_event(keyname string, keydown bool) error {
@ -47,3 +48,341 @@ func Keybd_event_origin(key, keyenv, down, extra uintptr) error {
syscall.Syscall6(keyevent, 4, key, keyenv, down, extra, 0, 0) syscall.Syscall6(keyevent, 4, key, keyenv, down, extra, 0, 0)
return nil return nil
} }
func OpenClipboard(hWnd HWND) error {
user32, err := syscall.LoadLibrary("user32.dll")
if err != nil {
return errors.New("Can't Load User32 API")
}
defer syscall.FreeLibrary(user32)
oc, err := syscall.GetProcAddress(syscall.Handle(user32), "OpenClipboard")
if err != nil {
return errors.New("Can't Load OpenClipboard API")
}
if hWnd != 0 {
if r, _, errno := syscall.Syscall(oc, 1, uintptr(hWnd), 0, 0); r == 0 {
return error(errno)
}
return nil
}
if r, _, errno := syscall.Syscall(oc, 0, 0, 0, 0); r == 0 {
return error(errno)
}
return nil
}
func CloseClipboard() error {
user32, err := syscall.LoadLibrary("user32.dll")
if err != nil {
return errors.New("Can't Load User32 API")
}
defer syscall.FreeLibrary(user32)
cc, err := syscall.GetProcAddress(syscall.Handle(user32), "CloseClipboard")
if err != nil {
return errors.New("Can't Load CloseClipboard API")
}
if r, _, errno := syscall.Syscall(cc, 0, 0, 0, 0); r == 0 {
return error(errno)
}
return nil
}
func GetClipboardData(uFormat DWORD) (HGLOBAL, error) {
user32, err := syscall.LoadLibrary("user32.dll")
if err != nil {
return 0, errors.New("Can't Load User32 API")
}
defer syscall.FreeLibrary(user32)
gcd, err := syscall.GetProcAddress(syscall.Handle(user32), "GetClipboardData")
if err != nil {
return 0, errors.New("Can't Load GetClipboardData API")
}
r, _, errno := syscall.Syscall(gcd, 1, uintptr(uFormat), 0, 0)
if r == 0 {
return 0, error(errno)
}
return HGLOBAL(r), nil
}
func EnumClipboardFormats(uFormat DWORD) (DWORD, error) {
user32, err := syscall.LoadLibrary("user32.dll")
if err != nil {
return 0, errors.New("Can't Load User32 API")
}
defer syscall.FreeLibrary(user32)
ecf, err := syscall.GetProcAddress(syscall.Handle(user32), "EnumClipboardFormats")
if err != nil {
return 0, errors.New("Can't Load EnumClipboardFormats API")
}
r, _, errno := syscall.Syscall(ecf, 1, uintptr(uFormat), 0, 0)
if r == 0 {
return 0, error(errno)
}
return DWORD(r), nil
}
func EnumAllClipboardFormats() ([]DWORD, error) {
var formats []DWORD
for i := 0; ; i++ {
format, err := EnumClipboardFormats(DWORD(i))
if err != nil {
break
}
formats = append(formats, format)
}
return formats, nil
}
func GetClipboardFormatName(uFormat DWORD) (string, error) {
user32, err := syscall.LoadLibrary("user32.dll")
if err != nil {
return "", errors.New("Can't Load User32 API")
}
defer syscall.FreeLibrary(user32)
gcfn, err := syscall.GetProcAddress(syscall.Handle(user32), "GetClipboardFormatNameW")
if err != nil {
return "", errors.New("Can't Load GetClipboardFormatName API")
}
var buffer [256]uint16
r, _, errno := syscall.Syscall6(gcfn, 3, uintptr(uFormat), uintptr(unsafe.Pointer(&buffer)), uintptr(len(buffer)), 0, 0, 0)
if r == 0 {
return "", error(errno)
}
return syscall.UTF16ToString(buffer[:]), nil
}
func RegisterClipboardFormat(lpszFormat string) DWORD {
user32, err := syscall.LoadLibrary("user32.dll")
if err != nil {
return 0
}
defer syscall.FreeLibrary(user32)
rcf, err := syscall.GetProcAddress(syscall.Handle(user32), "RegisterClipboardFormatW")
if err != nil {
return 0
}
r, _, _ := syscall.Syscall(rcf, 1, uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(lpszFormat))), 0, 0)
return DWORD(r)
}
func EmptyClipboard() error {
user32, err := syscall.LoadLibrary("user32.dll")
if err != nil {
return errors.New("Can't Load User32 API")
}
defer syscall.FreeLibrary(user32)
ec, err := syscall.GetProcAddress(syscall.Handle(user32), "EmptyClipboard")
if err != nil {
return errors.New("Can't Load EmptyClipboard API")
}
if r, _, errno := syscall.Syscall(ec, 0, 0, 0, 0); r == 0 {
return error(errno)
}
return nil
}
func CountClipboardFormats() (int, error) {
user32, err := syscall.LoadLibrary("user32.dll")
if err != nil {
return 0, errors.New("Can't Load User32 API")
}
defer syscall.FreeLibrary(user32)
ccf, err := syscall.GetProcAddress(syscall.Handle(user32), "CountClipboardFormats")
if err != nil {
return 0, errors.New("Can't Load CountClipboardFormats API")
}
r, _, errno := syscall.Syscall(ccf, 0, 0, 0, 0)
if r == 0 {
return 0, error(errno)
}
return int(r), nil
}
func GetClipboardOwner() (HWND, error) {
user32, err := syscall.LoadLibrary("user32.dll")
if err != nil {
return 0, errors.New("Can't Load User32 API")
}
defer syscall.FreeLibrary(user32)
gco, err := syscall.GetProcAddress(syscall.Handle(user32), "GetClipboardOwner")
if err != nil {
return 0, errors.New("Can't Load GetClipboardOwner API")
}
r, _, errno := syscall.Syscall(gco, 0, 0, 0, 0)
if r == 0 {
return 0, error(errno)
}
return HWND(r), nil
}
func GetUpdatedClipboardFormats(lpuiFormats unsafe.Pointer, cFormats int, pcFormats unsafe.Pointer) (int, error) {
user32, err := syscall.LoadLibrary("user32.dll")
if err != nil {
return 0, errors.New("Can't Load User32 API")
}
defer syscall.FreeLibrary(user32)
gucf, err := syscall.GetProcAddress(syscall.Handle(user32), "GetUpdatedClipboardFormats")
if err != nil {
return 0, errors.New("Can't Load GetUpdatedClipboardFormats API")
}
r, _, errno := syscall.Syscall(gucf, 3, uintptr(lpuiFormats), uintptr(cFormats), uintptr(pcFormats))
if r == 0 {
return 0, error(errno)
}
return int(r), nil
}
func GetUpdatedClipboardFormatsAll() ([]DWORD, error) {
var res []DWORD
formats := make([]uint32, 32)
var count uint32
_, err := GetUpdatedClipboardFormats(unsafe.Pointer(&formats[0]), len(formats), unsafe.Pointer(&count))
if err != nil {
return nil, err
}
for i := 0; i < int(count); i++ {
res = append(res, DWORD(formats[i]))
}
return res, err
}
func IsClipboardFormatAvailable(uFormat DWORD) (bool, error) {
user32, err := syscall.LoadLibrary("user32.dll")
if err != nil {
return false, err
}
defer syscall.FreeLibrary(user32)
icfa, err := syscall.GetProcAddress(syscall.Handle(user32), "IsClipboardFormatAvailable")
if err != nil {
return false, err
}
r, _, _ := syscall.Syscall(icfa, 1, uintptr(uFormat), 0, 0)
return r != 0, nil
}
func AddClipboardFormatListener(hWnd HWND) (bool, error) {
user32, err := syscall.LoadLibrary("user32.dll")
if err != nil {
return false, err
}
defer syscall.FreeLibrary(user32)
acfl, err := syscall.GetProcAddress(syscall.Handle(user32), "AddClipboardFormatListener")
if err != nil {
return false, err
}
r, _, _ := syscall.Syscall(acfl, 1, uintptr(hWnd), 0, 0)
return r != 0, nil
}
func RemoveClipboardFormatListener(hWnd HWND) (bool, error) {
user32, err := syscall.LoadLibrary("user32.dll")
if err != nil {
return false, err
}
defer syscall.FreeLibrary(user32)
rcfl, err := syscall.GetProcAddress(syscall.Handle(user32), "RemoveClipboardFormatListener")
if err != nil {
return false, err
}
r, _, _ := syscall.Syscall(rcfl, 1, uintptr(hWnd), 0, 0)
return r != 0, nil
}
func SetClipboardData(uFormat DWORD, hMem HGLOBAL) (HGLOBAL, error) {
user32, err := syscall.LoadLibrary("user32.dll")
if err != nil {
return 0, errors.New("Can't Load User32 API")
}
defer syscall.FreeLibrary(user32)
scd, err := syscall.GetProcAddress(syscall.Handle(user32), "SetClipboardData")
if err != nil {
return 0, errors.New("Can't Load SetClipboardData API")
}
r, _, errno := syscall.Syscall(scd, 2, uintptr(uFormat), uintptr(hMem), 0)
if r == 0 {
return 0, error(errno)
}
return HGLOBAL(r), nil
}
func SetClipboardViewer(hWndNewViewer HWND) (HWND, error) {
user32, err := syscall.LoadLibrary("user32.dll")
if err != nil {
return 0, errors.New("Can't Load User32 API")
}
defer syscall.FreeLibrary(user32)
scv, err := syscall.GetProcAddress(syscall.Handle(user32), "SetClipboardViewer")
if err != nil {
return 0, errors.New("Can't Load SetClipboardViewer API")
}
r, _, errno := syscall.Syscall(scv, 1, uintptr(hWndNewViewer), 0, 0)
if r == 0 {
return 0, error(errno)
}
return HWND(r), nil
}
func GetClipboardSequenceNumber() (DWORD, error) {
user32, err := syscall.LoadLibrary("user32.dll")
if err != nil {
return 0, errors.New("Can't Load User32 API")
}
defer syscall.FreeLibrary(user32)
gcsn, err := syscall.GetProcAddress(syscall.Handle(user32), "GetClipboardSequenceNumber")
if err != nil {
return 0, errors.New("Can't Load GetClipboardSequenceNumber API")
}
r, _, errno := syscall.Syscall(gcsn, 0, 0, 0, 0)
if r == 0 {
return 0, error(errno)
}
return DWORD(r), nil
}
func CreateWindowEx(dwExStyle DWORD, lpClassName, lpWindowName string, dwStyle DWORD, x, y, nWidth, nHeight int, hWndParent HWND, hMenu HMENU, hInstance HINSTANCE, lpParam unsafe.Pointer) (HWND, error) {
user32, err := syscall.LoadLibrary("user32.dll")
if err != nil {
return 0, errors.New("Can't Load User32 API")
}
defer syscall.FreeLibrary(user32)
cwe, err := syscall.GetProcAddress(syscall.Handle(user32), "CreateWindowExW")
if err != nil {
return 0, errors.New("Can't Load CreateWindowEx API")
}
r, _, errno := syscall.Syscall12(cwe, 11, uintptr(dwExStyle), uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(lpClassName))), uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(lpWindowName))), uintptr(dwStyle), uintptr(x), uintptr(y), uintptr(nWidth), uintptr(nHeight), uintptr(hWndParent), uintptr(hMenu), uintptr(hInstance), uintptr(lpParam))
if r == 0 {
return 0, error(errno)
}
return HWND(r), nil
}
func DestoryWindow(hWnd HWND) (bool, error) {
user32, err := syscall.LoadLibrary("user32.dll")
if err != nil {
return false, errors.New("Can't Load User32 API")
}
defer syscall.FreeLibrary(user32)
dw, err := syscall.GetProcAddress(syscall.Handle(user32), "DestroyWindow")
if err != nil {
return false, errors.New("Can't Load DestroyWindow API")
}
r, _, _ := syscall.Syscall(dw, 1, uintptr(hWnd), 0, 0)
return r != 0, nil
}
func GetMessage(lpMsg *MSG, hWnd HWND, wMsgFilterMin, wMsgFilterMax DWORD) (DWORD, error) {
user32, err := syscall.LoadLibrary("user32.dll")
if err != nil {
return 0, errors.New("Can't Load User32 API")
}
defer syscall.FreeLibrary(user32)
gm, err := syscall.GetProcAddress(syscall.Handle(user32), "GetMessageW")
if err != nil {
return 0, errors.New("Can't Load GetMessage API")
}
r, _, errno := syscall.Syscall6(gm, 4, uintptr(unsafe.Pointer(lpMsg)), uintptr(hWnd), uintptr(wMsgFilterMin), uintptr(wMsgFilterMax), 0, 0)
if r == 0 {
return 0, error(errno)
}
return DWORD(r), nil
}

@ -0,0 +1,78 @@
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
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)
}

@ -1,5 +1,7 @@
package win32api package win32api
import "syscall"
const ( const (
VK_SHIFT = 0x10 VK_SHIFT = 0x10
VK_CTRL = 0x11 VK_CTRL = 0x11
@ -178,3 +180,51 @@ const (
VK_PA1 = 0xFD VK_PA1 = 0xFD
VK_OEM_CLEAR = 0xFE VK_OEM_CLEAR = 0xFE
) )
// 剪贴板标准格式
const (
CF_BITMAP DWORD = 2
CF_DIB DWORD = 8
CF_DIBV5 DWORD = 17
CF_DIF DWORD = 5
CF_DSPBITMAP DWORD = 130
CF_DSPENHMETAFILE DWORD = 142
CF_DSPMETAFILEPICT DWORD = 131
CF_DSPTEXT DWORD = 129
CF_ENHMETAFILE DWORD = 14
CF_GDIOBJFIRST DWORD = 0x0300
CF_GDIOBJLAST DWORD = 0x03FF
CF_HDROP DWORD = 15
CF_LOCALE DWORD = 16
CF_METAFILEPICT DWORD = 3
CF_OEMTEXT DWORD = 7
CF_OWNERDISPLAY DWORD = 0x0080
CF_PALETTE DWORD = 9
CF_PENDATA DWORD = 10
CF_PRIVATEFIRST DWORD = 0x0200
CF_PRIVATELAST DWORD = 0x02FF
CF_RIFF DWORD = 11
CF_SYLK DWORD = 4
CF_TEXT DWORD = 1
CF_TIFF DWORD = 6
CF_UNICODETEXT DWORD = 13
CF_WAVE DWORD = 12
CF_HENHMETAFILE DWORD = 14
)
type MSG struct {
Hwnd syscall.Handle
Message uint32
WParam uintptr
LParam uintptr
Time uint32
Pt POINT
}
type POINT struct {
X, Y int32
}
const (
WM_CLIPBOARDUPDATE DWORD = 0x031D
)

Loading…
Cancel
Save