Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- // +build windows
- import (
- "fmt"
- "log"
- "syscall"
- "time"
- "unsafe"
- )
- var (
- modkernel32 = syscall.MustLoadDLL("kernel32.dll")
- moduser32 = syscall.MustLoadDLL("user32.dll")
- procCallWindowProcW = moduser32.MustFindProc("CallWindowProcW")
- procSetWindowLongW = moduser32.MustFindProc("SetWindowLongW")
- procGetMessageW = moduser32.MustFindProc("GetMessageW")
- procTranslateMessage = moduser32.MustFindProc("TranslateMessage")
- procDispatchMessageW = moduser32.MustFindProc("DispatchMessageW")
- procGetConsoleTitleW = modkernel32.MustFindProc("GetConsoleTitleW")
- procSetConsoleTitleW = modkernel32.MustFindProc("SetConsoleTitleW")
- procFindWindowW = moduser32.MustFindProc("FindWindowW")
- procAllocConsole = modkernel32.MustFindProc("AllocConsole")
- procFreeConsole = modkernel32.MustFindProc("FreeConsole")
- )
- func callWindowProc(lpPrevWndFunc uintptr, hWnd syscall.Handle, Msg uint32, wParam uintptr, lParam uintptr) (ret uintptr) {
- r0, _, _ := syscall.Syscall6(procCallWindowProcW.Addr(), 5, uintptr(lpPrevWndFunc), uintptr(hWnd), uintptr(Msg), uintptr(wParam), uintptr(lParam), 0)
- ret = uintptr(r0)
- return
- }
- func setWindowLong(hWnd syscall.Handle, nIndex int32, dwNewLong uintptr) (ret uintptr, err error) {
- r0, _, e1 := syscall.Syscall(procSetWindowLongW.Addr(), 3, uintptr(hWnd), uintptr(nIndex), uintptr(dwNewLong))
- ret = uintptr(r0)
- if ret == 0 {
- if e1 != 0 {
- err = error(e1)
- } else {
- err = syscall.EINVAL
- }
- }
- return
- }
- func getMessage(lpMsg *_MSG, hWnd syscall.Handle, wMsgFilterMin uint32, wMsgFilterMax uint32) (ret bool, err error) {
- r0, _, e1 := syscall.Syscall6(procGetMessageW.Addr(), 4, uintptr(unsafe.Pointer(lpMsg)), uintptr(hWnd), uintptr(wMsgFilterMin), uintptr(wMsgFilterMax), 0, 0)
- ret = bool(r0 != 0)
- if !ret {
- if e1 != 0 {
- err = error(e1)
- } else {
- err = syscall.EINVAL
- }
- }
- return
- }
- func translateMessage(lpMsg *_MSG) (ret bool) {
- r0, _, _ := syscall.Syscall(procTranslateMessage.Addr(), 1, uintptr(unsafe.Pointer(lpMsg)), 0, 0)
- ret = bool(r0 != 0)
- return
- }
- func dispatchMessage(lpMsg *_MSG) (ret uintptr) {
- r0, _, _ := syscall.Syscall(procDispatchMessageW.Addr(), 1, uintptr(unsafe.Pointer(lpMsg)), 0, 0)
- ret = uintptr(r0)
- return
- }
- func getConsoleTitle(lpConsoleTitle []uint16, nSize uint32) (ret uint32, err error) {
- var _p0 *uint16
- if len(lpConsoleTitle) > 0 {
- _p0 = &lpConsoleTitle[0]
- }
- r0, _, e1 := syscall.Syscall(procGetConsoleTitleW.Addr(), 3, uintptr(unsafe.Pointer(_p0)), uintptr(len(lpConsoleTitle)), uintptr(nSize))
- ret = uint32(r0)
- if ret == 0 {
- if e1 != 0 {
- err = error(e1)
- } else {
- err = syscall.EINVAL
- }
- }
- return
- }
- func setConsoleTitle(lpConsoleTitle []uint16) (err error) {
- var _p0 *uint16
- if len(lpConsoleTitle) > 0 {
- _p0 = &lpConsoleTitle[0]
- }
- r1, _, e1 := syscall.Syscall(procSetConsoleTitleW.Addr(), 2, uintptr(unsafe.Pointer(_p0)), uintptr(len(lpConsoleTitle)), 0)
- if int(r1) == 0 {
- if e1 != 0 {
- err = error(e1)
- } else {
- err = syscall.EINVAL
- }
- }
- return
- }
- func findWindow(lpClassName []uint16, lpWindowName []uint16) (ret syscall.Handle, err error) {
- var _p0 *uint16
- if len(lpClassName) > 0 {
- _p0 = &lpClassName[0]
- }
- var _p1 *uint16
- if len(lpWindowName) > 0 {
- _p1 = &lpWindowName[0]
- }
- r0, _, e1 := syscall.Syscall6(procFindWindowW.Addr(), 4, uintptr(unsafe.Pointer(_p0)), uintptr(len(lpClassName)), uintptr(unsafe.Pointer(_p1)), uintptr(len(lpWindowName)), 0, 0)
- ret = syscall.Handle(r0)
- if ret == 0 {
- if e1 != 0 {
- err = error(e1)
- } else {
- err = syscall.EINVAL
- }
- }
- return
- }
- func allocConsole() (err error) {
- r1, _, e1 := syscall.Syscall(procAllocConsole.Addr(), 0, 0, 0, 0)
- if int(r1) == 0 {
- if e1 != 0 {
- err = error(e1)
- } else {
- err = syscall.EINVAL
- }
- }
- return
- }
- func freeConsole() (err error) {
- r1, _, e1 := syscall.Syscall(procFreeConsole.Addr(), 0, 0, 0, 0)
- if int(r1) == 0 {
- if e1 != 0 {
- err = error(e1)
- } else {
- err = syscall.EINVAL
- }
- }
- return
- }
- // * * *
- type _MSG struct {
- hwnd syscall.Handle
- message uint32
- wParam uintptr
- lParam uintptr
- time uint32
- pt _POINT
- }
- type _POINT struct {
- x, y int32
- }
- const (
- _WM_SIZE = 5
- _SIZE_MAXIMIZED = 2
- _GWLP_WNDPROC = -4
- )
- // * * *
- var callBack, oldWinProc uintptr
- var message _MSG
- func getConsoleHandle() (syscall.Handle, error) {
- oldTitle := make([]uint16, 128)
- _, err := getConsoleTitle(oldTitle, 128)
- if err != nil {
- return 0, err
- }
- // Change the consolte title to a unique title
- newTitle := syscall.StringToUTF16(time.Now().String())
- if err = setConsoleTitle(newTitle); err != nil {
- return 0, err
- }
- handle, err := findWindow(nil, newTitle)
- if err != nil {
- return 0, err
- }
- return handle, nil
- }
- func winProc(hwnd syscall.Handle, msg uint32, wparam, lparam uintptr) (ret uintptr) {
- switch msg {
- case _WM_SIZE:
- fmt.Println("Size")
- if wparam == _SIZE_MAXIMIZED {
- fmt.Println("Changed!")
- }
- //default:
- //return callWindowProcW(oldWindowProc, hwnd, msg, wparam, lparam)
- }
- //return 0
- // Call original procedure
- return callWindowProc(uintptr(oldWinProc), hwnd, msg, wparam, lparam)
- }
- func TrapSize() {
- handle, err := getConsoleHandle()
- if err != nil {
- panic("getConsoleHandle " + err.Error())
- }
- callBack = syscall.NewCallback(winProc)
- oldWinProc, err = setWindowLong(handle, _GWLP_WNDPROC, callBack)
- if err != nil {
- log.Fatal("setWindowLong", err)
- }
- // dispatch message
- go func() {
- for {
- if ok, _ := getMessage(&message, 0, 0, 0); ok {
- //
- }
- translateMessage(&message)
- dispatchMessage(&message)
- }
- }()
- }
- // * * *
- func main() {
- err := freeConsole()
- if err != nil {
- log.Fatal("freeConsole:", err)
- }
- err = allocConsole()
- if err != nil {
- log.Fatal("allocConsole:", err)
- }
- TrapSize()
- for {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement