Advertisement
Guest User

Windows console

a guest
Sep 16th, 2012
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 6.00 KB | None | 0 0
  1. package main
  2.  
  3. // +build windows
  4.  
  5. import (
  6.     "fmt"
  7.     "log"
  8.     "syscall"
  9.     "time"
  10.     "unsafe"
  11. )
  12.  
  13. var (
  14.     modkernel32 = syscall.MustLoadDLL("kernel32.dll")
  15.     moduser32   = syscall.MustLoadDLL("user32.dll")
  16.  
  17.     procCallWindowProcW            = moduser32.MustFindProc("CallWindowProcW")
  18.     procSetWindowLongW             = moduser32.MustFindProc("SetWindowLongW")
  19.     procGetMessageW                = moduser32.MustFindProc("GetMessageW")
  20.     procTranslateMessage           = moduser32.MustFindProc("TranslateMessage")
  21.     procDispatchMessageW           = moduser32.MustFindProc("DispatchMessageW")
  22.     procGetConsoleTitleW           = modkernel32.MustFindProc("GetConsoleTitleW")
  23.     procSetConsoleTitleW           = modkernel32.MustFindProc("SetConsoleTitleW")
  24.     procFindWindowW                = moduser32.MustFindProc("FindWindowW")
  25.     procAllocConsole               = modkernel32.MustFindProc("AllocConsole")
  26.     procFreeConsole                = modkernel32.MustFindProc("FreeConsole")
  27. )
  28.  
  29. func callWindowProc(lpPrevWndFunc uintptr, hWnd syscall.Handle, Msg uint32, wParam uintptr, lParam uintptr) (ret uintptr) {
  30.     r0, _, _ := syscall.Syscall6(procCallWindowProcW.Addr(), 5, uintptr(lpPrevWndFunc), uintptr(hWnd), uintptr(Msg), uintptr(wParam), uintptr(lParam), 0)
  31.     ret = uintptr(r0)
  32.     return
  33. }
  34.  
  35. func setWindowLong(hWnd syscall.Handle, nIndex int32, dwNewLong uintptr) (ret uintptr, err error) {
  36.     r0, _, e1 := syscall.Syscall(procSetWindowLongW.Addr(), 3, uintptr(hWnd), uintptr(nIndex), uintptr(dwNewLong))
  37.     ret = uintptr(r0)
  38.     if ret == 0 {
  39.         if e1 != 0 {
  40.             err = error(e1)
  41.         } else {
  42.             err = syscall.EINVAL
  43.         }
  44.     }
  45.     return
  46. }
  47.  
  48. func getMessage(lpMsg *_MSG, hWnd syscall.Handle, wMsgFilterMin uint32, wMsgFilterMax uint32) (ret bool, err error) {
  49.     r0, _, e1 := syscall.Syscall6(procGetMessageW.Addr(), 4, uintptr(unsafe.Pointer(lpMsg)), uintptr(hWnd), uintptr(wMsgFilterMin), uintptr(wMsgFilterMax), 0, 0)
  50.     ret = bool(r0 != 0)
  51.     if !ret {
  52.         if e1 != 0 {
  53.             err = error(e1)
  54.         } else {
  55.             err = syscall.EINVAL
  56.         }
  57.     }
  58.     return
  59. }
  60.  
  61. func translateMessage(lpMsg *_MSG) (ret bool) {
  62.     r0, _, _ := syscall.Syscall(procTranslateMessage.Addr(), 1, uintptr(unsafe.Pointer(lpMsg)), 0, 0)
  63.     ret = bool(r0 != 0)
  64.     return
  65. }
  66.  
  67. func dispatchMessage(lpMsg *_MSG) (ret uintptr) {
  68.     r0, _, _ := syscall.Syscall(procDispatchMessageW.Addr(), 1, uintptr(unsafe.Pointer(lpMsg)), 0, 0)
  69.     ret = uintptr(r0)
  70.     return
  71. }
  72.  
  73. func getConsoleTitle(lpConsoleTitle []uint16, nSize uint32) (ret uint32, err error) {
  74.     var _p0 *uint16
  75.     if len(lpConsoleTitle) > 0 {
  76.         _p0 = &lpConsoleTitle[0]
  77.     }
  78.     r0, _, e1 := syscall.Syscall(procGetConsoleTitleW.Addr(), 3, uintptr(unsafe.Pointer(_p0)), uintptr(len(lpConsoleTitle)), uintptr(nSize))
  79.     ret = uint32(r0)
  80.     if ret == 0 {
  81.         if e1 != 0 {
  82.             err = error(e1)
  83.         } else {
  84.             err = syscall.EINVAL
  85.         }
  86.     }
  87.     return
  88. }
  89.  
  90. func setConsoleTitle(lpConsoleTitle []uint16) (err error) {
  91.     var _p0 *uint16
  92.     if len(lpConsoleTitle) > 0 {
  93.         _p0 = &lpConsoleTitle[0]
  94.     }
  95.     r1, _, e1 := syscall.Syscall(procSetConsoleTitleW.Addr(), 2, uintptr(unsafe.Pointer(_p0)), uintptr(len(lpConsoleTitle)), 0)
  96.     if int(r1) == 0 {
  97.         if e1 != 0 {
  98.             err = error(e1)
  99.         } else {
  100.             err = syscall.EINVAL
  101.         }
  102.     }
  103.     return
  104. }
  105.  
  106. func findWindow(lpClassName []uint16, lpWindowName []uint16) (ret syscall.Handle, err error) {
  107.     var _p0 *uint16
  108.     if len(lpClassName) > 0 {
  109.         _p0 = &lpClassName[0]
  110.     }
  111.     var _p1 *uint16
  112.     if len(lpWindowName) > 0 {
  113.         _p1 = &lpWindowName[0]
  114.     }
  115.     r0, _, e1 := syscall.Syscall6(procFindWindowW.Addr(), 4, uintptr(unsafe.Pointer(_p0)), uintptr(len(lpClassName)), uintptr(unsafe.Pointer(_p1)), uintptr(len(lpWindowName)), 0, 0)
  116.     ret = syscall.Handle(r0)
  117.     if ret == 0 {
  118.         if e1 != 0 {
  119.             err = error(e1)
  120.         } else {
  121.             err = syscall.EINVAL
  122.         }
  123.     }
  124.     return
  125. }
  126.  
  127. func allocConsole() (err error) {
  128.     r1, _, e1 := syscall.Syscall(procAllocConsole.Addr(), 0, 0, 0, 0)
  129.     if int(r1) == 0 {
  130.         if e1 != 0 {
  131.             err = error(e1)
  132.         } else {
  133.             err = syscall.EINVAL
  134.         }
  135.     }
  136.     return
  137. }
  138.  
  139. func freeConsole() (err error) {
  140.     r1, _, e1 := syscall.Syscall(procFreeConsole.Addr(), 0, 0, 0, 0)
  141.     if int(r1) == 0 {
  142.         if e1 != 0 {
  143.             err = error(e1)
  144.         } else {
  145.             err = syscall.EINVAL
  146.         }
  147.     }
  148.     return
  149. }
  150.  
  151. // * * *
  152.  
  153. type _MSG struct {
  154.     hwnd syscall.Handle
  155.     message uint32
  156.     wParam uintptr
  157.     lParam uintptr
  158.     time uint32
  159.     pt _POINT
  160. }
  161.  
  162. type _POINT struct {
  163.     x, y int32
  164. }
  165.  
  166. const (
  167.     _WM_SIZE        = 5
  168.     _SIZE_MAXIMIZED = 2
  169.     _GWLP_WNDPROC   = -4
  170. )
  171.  
  172. // * * *
  173.  
  174. var callBack, oldWinProc uintptr
  175. var message _MSG
  176.  
  177. func getConsoleHandle() (syscall.Handle, error) {
  178.     oldTitle := make([]uint16, 128)
  179.  
  180.     _, err := getConsoleTitle(oldTitle, 128)
  181.     if err != nil {
  182.         return 0, err
  183.     }
  184.  
  185.     // Change the consolte title to a unique title
  186.     newTitle := syscall.StringToUTF16(time.Now().String())
  187.     if err = setConsoleTitle(newTitle); err != nil {
  188.         return 0, err
  189.     }
  190.  
  191.     handle, err := findWindow(nil, newTitle)
  192.     if err != nil {
  193.         return 0, err
  194.     }
  195.     return handle, nil
  196. }
  197.  
  198. func winProc(hwnd syscall.Handle, msg uint32, wparam, lparam uintptr) (ret uintptr) {
  199.     switch msg {
  200.     case _WM_SIZE:
  201.         fmt.Println("Size")
  202.         if wparam == _SIZE_MAXIMIZED {
  203.             fmt.Println("Changed!")
  204.         }
  205.     //default:
  206.         //return callWindowProcW(oldWindowProc, hwnd, msg, wparam, lparam)
  207.     }
  208.     //return 0
  209.     // Call original procedure
  210.     return callWindowProc(uintptr(oldWinProc), hwnd, msg, wparam, lparam)
  211. }
  212.  
  213. func TrapSize() {
  214.     handle, err := getConsoleHandle()
  215.     if err != nil {
  216.         panic("getConsoleHandle " + err.Error())
  217.     }
  218.  
  219.     callBack = syscall.NewCallback(winProc)
  220.     oldWinProc, err = setWindowLong(handle, _GWLP_WNDPROC, callBack)
  221.     if err != nil {
  222.         log.Fatal("setWindowLong", err)
  223.     }
  224.  
  225.     // dispatch message
  226.     go func() {
  227.         for {
  228.             if ok, _ := getMessage(&message, 0, 0, 0); ok {
  229.                 //
  230.             }
  231.             translateMessage(&message)
  232.             dispatchMessage(&message)
  233.         }
  234.     }()
  235. }
  236.  
  237. // * * *
  238.  
  239. func main() {
  240.     err := freeConsole()
  241.     if err != nil {
  242.         log.Fatal("freeConsole:", err)
  243.     }
  244.  
  245.     err = allocConsole()
  246.     if err != nil {
  247.         log.Fatal("allocConsole:", err)
  248.     }
  249.  
  250.     TrapSize()
  251.     for {
  252.    
  253.     }
  254. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement