Seite zurück / previous page
links
Seite vor / next page
rechts
 
Kapitel zurück / previous Chapter
zurück
Kapitel vor / next Chapter
vor
 
Seitenanfang/TopOfPage
oben
Seitenende/EndOfPage
unten
DD380100.HTM
DD-Software.UDFCON
Add this page to your favorites
Save this document

udflib Console


udflib.Console.wbt   Version 1.02  2002:03:03

- A selection of the MS SDK console functions wrapped into WinBatch User Defined Functions.

- Almost all functions which works with Win 98.

Console.Tour.1
Console.Tour.2
Console.Tour.3
Console.Tour.4
Console.Tour.5
Console.Tour.6


If you have questions about WinBatch, you are encouraged to use online WebBoard BBS at http://webboard.windowware.com


;================================================================================================================================
; udflib.console.wbt
; DOS console functions for use with WinBatch 2001 and Windows 98
; Detlev Dalitz.20020216.20020220
;================================================================================================================================


;----------------------------------------------------------------------------------------------------
; udfConAllocConsole ()                                                      ; 2002:02:20:16:53:31
; udfConFreeConsole ()                                                       ; 2002:02:20:16:53:31
; udfConSetConsoleTitle (str)                                                ; 2002:02:20:16:53:31
; udfConGetConsoleTitle ()                                                   ; 2002:02:20:16:53:31
; udfConIsConsoleFullScreen ()                                               ; 2002:02:20:16:53:31
; udfConToggleConsoleState ()                                                ; 2002:02:20:16:53:31
; udfConSetConsoleMode (hConsoleHandle, mode)                                ; 2002:02:20:16:53:31
; udfConGetConsoleMode (hConsoleHandle)                                      ; 2002:02:20:16:53:31
; udfConConsoleExists ()                                                     ; 2002:02:20:16:53:31

; udfConGetHandleStdIn ()                                                    ; 2002:02:20:16:53:31
; udfConGetHandleStdOut ()                                                   ; 2002:02:20:16:53:31
; udfConGetHandleStdErr ()                                                   ; 2002:02:20:16:53:31
; udfCloseHandle (handle)                                                    ; 2002:02:20:16:53:31
; udfConSetStdHandle (StdHandle, HandleToSetAsStd)                           ; 2002:02:20:16:53:31
; udfConSetHandleStdIn (HandleToSetAsStd)                                    ; 2002:02:20:16:53:31
; udfConSetHandleStdOut (HandleToSetAsStd)                                   ; 2002:02:20:16:53:31
; udfConSetHandleStdErr (HandleToSetAsStd)                                   ; 2002:02:20:16:53:31
; udfConIsInputHandle (handle)                                               ; 2002:02:20:16:53:31

; udfConCreateScreenBuffer (DesiredAccess, ShareMode)                        ; 2002:02:20:16:53:31
; udfConSetActiveScreenBuffer (hOutput)                                      ; 2002:02:20:16:53:31
; udfConGetScreenBufferInfo_ (handle, request)                               ; 2002:02:20:16:53:31
; udfConGetScreenBufferInfo (handle, request)                                ; 2002:02:20:16:53:31
; udfConSetScreenBufferSize (hOutput, colx, rowy)                            ; 2002:02:20:16:53:31
; udfConGetMaxWindowSize (handle, mode)                                      ; 2002:02:20:16:53:31
; udfConScrollScreenBuffer (hOutput, stlx, stly, sbrx, sbry, ctlx, ctly, ... ; 2002:02:20:16:53:31
; udfConSetWindowInfo (hOutput, tlx, tly, brx, bry, bAbs)                    ; 2002:02:20:16:53:31
; udfConFillScreenBuffer (hOutput, char, attr)                               ; 2002:02:20:16:53:31

; udfConGetCursorInfo (hOutput)                                              ; 2002:02:20:16:53:31
; udfConSetCursorInfo (hOutput, infostr)                                     ; 2002:02:20:16:53:31
; udfConSetCursorPos (hOutput, colx, rowy)                                   ; 2002:02:20:16:53:31

; udfWriteFile (hOutput, str)                                                ; 2002:02:20:16:53:31
; udfReadFile (hInput)                                                       ; 2002:02:20:16:53:31

; udfConWriteConsole (hOutput, str)                                          ; 2002:02:20:16:53:31
; udfConReadConsole (hInput)                                                 ; 2002:02:20:16:53:31
; udfConFlushInputBuffer (hInput)                                            ; 2002:02:20:16:53:31
; udfConSetTextAttribute (hOutput, attr)                                     ; 2002:02:20:16:53:31
; udfBinaryAllocCharAttrBuf (tlx, tly, brx, bry)                             ; 2002:02:20:16:53:31
; udfBinaryAllocAttrBuf (len)                                                ; 2002:02:20:16:53:31
; udfConReadOutputRegion (hOutput, tlx, tly, brx, bry, bbCharAttr)           ; 2002:02:20:16:53:31
; udfConWriteOutputRegion (hOutput, tlx, tly, brx, bry, bbCharAttr)          ; 2002:02:20:16:53:31
; udfConReadOutputChar (hOutput, colx, rowy, len)                            ; 2002:02:20:16:53:31
; udfConReadOutputAttr (hOutput, colx, rowy, len, bbAttr)                    ; 2002:02:20:16:53:31
; udfConFillOutputChar (hOutput, colx, rowy, len, char)                      ; 2002:02:20:16:53:31
; udfConFillOutputAttr (hOutput, colx, rowy, len, attr)                      ; 2002:02:20:16:53:31
; udfConWriteCharStr (hOutput, colx, rowy, str)                              ; 2002:02:20:16:53:31
; udfConWriteAttr (hOutput, colx, rowy, bbattr)                              ; 2002:02:20:16:53:31

; udfConGetInputCP ()                                                        ; 2002:02:20:16:53:32
; udfConGetOutputCP ()                                                       ; 2002:02:20:16:53:32
; udfConSetInputCP (CodePageID)                                              ; 2002:02:20:16:53:32
; udfConSetOutputCP (CodePageID)                                             ; 2002:02:20:16:53:32

; udfConGetNumberMouseButtons ()                                             ; 2002:02:20:16:53:32

; udfConGetNumberInputEvents (hInput)                                        ; 2002:02:20:16:53:32
; udfConWriteConsoleInput (hInput, bbEvent)                                  ; 2002:02:20:16:53:32
; udfConReadConsoleInput (hInput, bbEvent)                                   ; 2002:02:20:16:53:32
; udfConPeekConsoleInput (hInput, bbEvent)                                   ; 2002:02:20:16:53:32

; udfConGetConsoleWindow ()                                                  ; 2002:02:20:16:53:32
; udfConGetDisplayMode ()                                                    ; 2002:02:20:16:53:32
; udfConSetCtrlHandler (HandlerRoutine, Add)                                 ; 2002:02:20:16:53:32

; udfConRandomFillWindow (hOutput)                                           ; 2002:02:20:16:53:32
; udfConSaveRegionToFile (hOutput, tlx, tly, brx, bry, filename)             ; 2002:02:20:16:53:32
; udfConLoadRegionFromFile (hOutput, tlx, tly, brx, bry, char, attr, ...     ; 2002:02:20:16:53:32
;----------------------------------------------------------------------------------------------------


; ----- CONSTANTS ---------------------------------------------------------------------------------------------------------------
;
;DefineConstant ColorAttributes
FOREGROUND_BLACK        =    0
FOREGROUND_BLUE         =    1
FOREGROUND_GREEN        =    2
FOREGROUND_CYAN         =    3 ; FOREGROUND_GREEN|FOREGROUND_BLUE
FOREGROUND_RED          =    4
FOREGROUND_MAGENTA      =    5 ; FOREGROUND_RED|FOREGROUND_BLUE
FOREGROUND_BROWN        =    6 ; FOREGROUND_RED|FOREGROUND_GREEN
FOREGROUND_LTGRAY       =    7 ; FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE
FOREGROUND_INTENSITY    =    8
BACKGROUND_BLACK        =    0
BACKGROUND_BLUE         =   16
BACKGROUND_GREEN        =   32
BACKGROUND_CYAN         =   48 ; BACKGROUND_GREEN|BACKGROUND_BLUE
BACKGROUND_RED          =   64
BACKGROUND_MAGENTA      =   80 ; BACKGROUND_RED|BACKGROUND_BLUE
BACKGROUND_BROWN        =   96 ; BACKGROUND_RED|BACKGROUND_GREEN|BACKGROUND_BLUE
BACKGROUND_LTGRAY       =  112 ; BACKGROUND_RED|BACKGROUND_GREEN|BACKGROUND_BLUE
BACKGROUND_INTENSITY    =  128

;DefineConstant WinState
CONSOLE_FULLSCREEN = 1
CONSOLE_FULLSCREEN_HARDWARE = 2

;DefineConstant ScreenBuffer
CONSOLE_TEXTMODE_BUFFER   = 1

ENABLE_PROCESSED_INPUT    = 1
ENABLE_LINE_INPUT         = 2
ENABLE_ECHO_INPUT         = 4
ENABLE_WINDOW_INPUT       = 8
ENABLE_MOUSE_INPUT        = 16

ENABLE_PROCESSED_OUTPUT   = 1
ENABLE_WRAP_AT_EOL_OUTPUT = 2

GENERIC_WRITE = 1073741824 ; 2**30
GENERIC_READ  = 2147483648 ; 2**31

FILE_SHARE_READ    = 1
FILE_SHARE_WRITE   = 2
FILE_SHARE_DELETE  = 4

;DefineConstant Handle
STD_INPUT_HANDLE  = -10
STD_OUTPUT_HANDLE = -11
STD_ERROR_HANDLE  = -12
INVALID_HANDLE_VALUE = -1
;
; ----- CONSTANTS ---------------------------------------------------------------------------------------------------------------



; ----- CONSOLE ALLOC FREE TITLE TOGGLE -----------------------------------------------------------------------------------------
;
#DefineFunction udfConAllocConsole ()
result = DllCall(StrCat(DirWindows(1),"KERNEL32.DLL"),long:"AllocConsole")
If (result==0) Then result=DllLastError()
Return (result)
; Allocates a new console for the calling process.
#EndFunction

#DefineFunction udfConFreeConsole ()
result = DllCall(StrCat(DirWindows(1),"KERNEL32.DLL"),long:"FreeConsole")
If (result==0) Then result=DllLastError()
Return (result)
; Detaches the calling process from its console.
#EndFunction


#DefineFunction udfConSetConsoleTitle (str)
result = DllCall(StrCat(DirWindows(1),"KERNEL32.DLL"),long:"SetConsoleTitleA",lpstr:str)
If (result==0) Then result=DllLastError()
Return (result)
; Sets the title bar string for the current console window.
#EndFunction


#DefineFunction udfConGetConsoleTitle ()
bbsize = 255
bb = BinaryAlloc(bbsize)
result = DllCall(StrCat(DirWindows(1),"KERNEL32.DLL"),long:"GetConsoleTitleA",lpbinary:bb,long:bbsize)
If (result==0)
   result=DllLastError()
Else
   BinaryEodSet(bb,bbsize)
   result = BinaryPeekStr(bb,0,bbsize)
EndIf
BinaryFree(bb)
Return (result)
; Retrieves the title bar string for the current console window.
#EndFunction


#DefineFunction udfConIsConsoleFullScreen ()
title = udfConGetConsoleTitle ()
Return (WinIsDos(title) && (WinState(title)==@ICON))
; maybe a hack ... returns @true if console session is in fullscreen mode
#EndFunction


#DefineFunction udfConToggleConsoleState ()
title = udfConGetConsoleTitle ()
Return(IntControl(22,DllHwnd(title),273,57359,0))
; WM_COMMAND  = 273   ; &H111
; MAGIC_CONSOLE_TOGGLE = 57359 ; 0xE00F
; toggles between fullscreen and window mode, only Win98
; Thanks to Nikolay N. Repin (Nick N. Repin), 1998
; Thanks to 1999 Ashot Oganesyan K, SmartLine, Inc
; mailto:ashot@aha.ru, http://www.protect-me.com, http://www.codepile.com
#EndFunction


#DefineFunction udfConSetConsoleMode (hConsoleHandle, mode)
result = DllCall(StrCat(DirWindows(1),"KERNEL32.DLL"),long:"SetConsoleMode",long:hConsoleHandle,long:mode)
If (result==0) Then result=DllLastError()
Return (result)
; Sets the input mode of a console's input buffer or the output mode of a console screen buffer.
#EndFunction


#DefineFunction udfConGetConsoleMode (hConsoleHandle)
bb = BinaryAlloc(4)
result = DllCall(StrCat(DirWindows(1),"KERNEL32.DLL"),long:"GetConsoleMode",long:hConsoleHandle,lpbinary:bb)
If (result==0)
   result=DllLastError()
Else
   BinaryEodSet(bb,4)
   result=BinaryPeek4(bb,0)
EndIf
BinaryFree(bb)
Return (result)
; Input handle  ; default=23
; ENABLE_PROCESSED_INPUT    = 1
; ENABLE_LINE_INPUT         = 2
; ENABLE_ECHO_INPUT         = 4
; ENABLE_WINDOW_INPUT       = 8  ; default=off
; ENABLE_MOUSE_INPUT        = 16
;
; Output handle ; default=3
; ENABLE_PROCESSED_OUTPUT   = 1
; ENABLE_WRAP_AT_EOL_OUTPUT = 2
;
; Retrieves the current input mode of a console's input buffer or the current output mode of a console screen buffer.
#EndFunction


#DefineFunction udfConConsoleExists ()
oldmode=ErrorMode(@OFF)
LastError()
handle=FileOpen("conout$","write")
ErrorMode(oldmode)
result=(LastError()<>1077)
If result Then FileClose(handle)
Return (result)
; returns 0..1 resp. @false..@true
#EndFunction
;
; ----- CONSOLE ALLOC FREE TITLE TOGGLE -----------------------------------------------------------------------------------------



; ----- GET SET STDHANDLE -------------------------------------------------------------------------------------------------------
;
#DefineFunction udfConGetHandleStdIn ()
result = DllCall(StrCat(DirWindows(1),"KERNEL32.DLL"),long:"GetStdHandle",long:-10)
If (result==0) Then result=DllLastError()
Return (result)
; STD_INPUT_HANDLE  = -10 ; INVALID_HANDLE_VALUE = -1
; Retrieves a handle for the standard input.
#EndFunction


#DefineFunction udfConGetHandleStdOut ()
result = DllCall(StrCat(DirWindows(1),"KERNEL32.DLL"),long:"GetStdHandle",long:-11)
If (result==0) Then result=DllLastError()
Return (result)
; STD_OUTPUT_HANDLE  = -11 ; INVALID_HANDLE_VALUE = -1
; Retrieves a handle for the standard output.
#EndFunction


#DefineFunction udfConGetHandleStdErr ()
result = DllCall(StrCat(DirWindows(1),"KERNEL32.DLL"),long:"GetStdHandle",long:-12)
If (result==0) Then result=DllLastError()
Return (result)
; STD_ERROR_HANDLE  = -12 ; INVALID_HANDLE_VALUE = -1
; Retrieves a handle for the standard error device.
#EndFunction


#DefineFunction udfCloseHandle (handle)
result = DllCall(StrCat(DirWindows(1),"KERNEL32.DLL"),long:"CloseHandle",long:handle)
If (result==0) Then result=DllLastError()
Return (result)
#EndFunction


#DefineFunction udfConSetStdHandle (StdHandle, HandleToSetAsStd)
result = DllCall(StrCat(DirWindows(1),"KERNEL32.DLL"),long:"SetStdHandle",long:StdHandle,long:HandleToSetAsStd)
If (result==0) Then result=DllLastError()
Return (result)
; BOOL SetStdHandle(
;  DWORD nStdHandle,  // input, output, or error device
;  HANDLE hHandle     // handle
; Sets the handle for the standard input, standard output, or standard error device.
#EndFunction


#DefineFunction udfConSetHandleStdIn (HandleToSetAsStd)
result = DllCall(StrCat(DirWindows(1),"KERNEL32.DLL"),long:"SetStdHandle",long:-10,long:HandleToSetAsStd)
If (result==0) Then result=DllLastError()
Return (result)
; Sets the handle for the standard input.
#EndFunction


#DefineFunction udfConSetHandleStdOut (HandleToSetAsStd)
result = DllCall(StrCat(DirWindows(1),"KERNEL32.DLL"),long:"SetStdHandle",long:-11,long:HandleToSetAsStd)
If (result==0) Then result=DllLastError()
Return (result)
; Sets the handle for the standard output.
#EndFunction


#DefineFunction udfConSetHandleStdErr (HandleToSetAsStd)
result = DllCall(StrCat(DirWindows(1),"KERNEL32.DLL"),long:"SetStdHandle",long:-12,long:HandleToSetAsStd)
If (result==0) Then result=DllLastError()
Return (result)
; Sets the handle for the standard error device.
#EndFunction


#DefineFunction udfConIsInputHandle (handle)
bb = BinaryAlloc(4)
result = DllCall(StrCat(DirWindows(1),"KERNEL32.DLL"),long:"GetNumberOfConsoleInputEvents",long:handle,lpbinary:bb)
BinaryFree(bb)
Return (result)
; Is this kind of tricky magic question ok?
; returns 0..1 resp. @false..@true
#EndFunction
;
; ----- GET SET STDHANDLE -------------------------------------------------------------------------------------------------------



; ----- SCREENBUFFER METRICS ----------------------------------------------------------------------------------------------------
;
#DefineFunction udfConCreateScreenBuffer (DesiredAccess, ShareMode)
result = DllCall(StrCat(DirWindows(1),"KERNEL32.DLL"),long:"CreateConsoleScreenBuffer",long:DesiredAccess,long:ShareMode,lpnull,long:1,lpnull)
If (result==0) Then result=DllLastError()
Return (result)
; dwDesiredAccess;
; GENERIC_WRITE = 1073741824 ; 2**30
; GENERIC_READ  = 2147483648 ; 2**31
; dwShareMode
; FILE_SHARE_READ   = 1
; FILE_SHARE_WRITE  = 2
; FILE_SHARE_DELETE = 4
; lpSecurityAttributes = lpnull;
; dwFlags
; CONSOLE_TEXTMODE_BUFFER = 1
; lpScreenBufferData = lpnull
; INVALID_HANDLE_VALUE = -1
;
; Creates a console screen buffer. Returns a handle.
#EndFunction



#DefineFunction udfConSetActiveScreenBuffer (hOutput)
result = DllCall(StrCat(DirWindows(1),"KERNEL32.DLL"),long:"SetConsoleActiveScreenBuffer",long:hOutput)
If (result==0) Then result=DllLastError()
Return (result)
; Sets the specified screen buffer to be the currently displayed console screen buffer.
#EndFunction


#DefineFunction udfConGetScreenBufferInfo_ (handle, request)
bb = BinaryAlloc(22)
result = DllCall(StrCat(DirWindows(1),"KERNEL32.DLL"),long:"GetConsoleScreenBufferInfo",long:handle,lpbinary:bb)
If (result==0)
   result=DllLastError()
Else
   BinaryEodSet(bb,22)
   result = ""
   Select mode
   Case 0
   Case 1
      result = ItemInsert(BinaryPeek2(bb,0),-1,result,@TAB) ; Screenbuffer Size X
      Continue
   Case 0
   Case 2
      result = ItemInsert(BinaryPeek2(bb,2),-1,result,@TAB) ; Screenbuffer Size Y
      Continue
   Case 0
   Case 3
      result = ItemInsert(BinaryPeek2(bb,4),-1,result,@TAB) ; Cursor Pos X
      Continue
   Case 0
   Case 4
      result = ItemInsert(BinaryPeek2(bb,6),-1,result,@TAB) ; Cursor Pos Y
      Continue
   Case 0
   Case 5
      result = ItemInsert(BinaryPeek2(bb,8),-1,result,@TAB) ; Char Attr
      Continue
   Case 0
   Case 6
      result = ItemInsert(BinaryPeek2(bb,10),-1,result,@TAB) ; Win top left Pos X
      Continue
   Case 0
   Case 7
      result = ItemInsert(BinaryPeek2(bb,12),-1,result,@TAB) ; Win top left  Pos Y
      Continue
   Case 0
   Case 8
      result = ItemInsert(BinaryPeek2(bb,14),-1,result,@TAB) ; Win bottom right Pos X
      Continue
   Case 0
   Case 9
      result = ItemInsert(BinaryPeek2(bb,16),-1,result,@TAB) ; Win bottom right  Pos Y
      Continue
   Case 0
   Case 10
      result = ItemInsert(BinaryPeek2(bb,18),-1,result,@TAB) ; Console Window Max Size X
      Continue
   Case 0
   Case 11
      result = ItemInsert(BinaryPeek2(bb,20),-1,result,@TAB) ; Console Window Max Size Y
      Break
   Case mode
      result = 0
      Break
   EndSelect
EndIf
BinaryFree(bb)
Return (result)
; dwSize
; COORD structure that contains the size, in character columns and rows, of the screen buffer.
; dwCursorPosition
; COORD structure that contains the column and row coordinates of the cursor in the screen buffer.
; wAttributes
; Attributes of the characters written to a screen buffer by the WriteFile and WriteConsole functions,
; or echoed to a screen buffer by the ReadFile and ReadConsole functions.
; For more information, see Character Attributes.
; srWindow
; SMALL_RECT structure that contains the screen buffer coordinates of the display window.
; Left   X-coordinate of the upper left corner of the rectangle.
; Top    Y-coordinate of the upper left corner of the rectangle.
; Right  X-coordinate of the lower right corner of the rectangle.
; Bottom Y-coordinate of the lower right corner of the rectangle.
; dwMaximumWindowSize
; COORD structure that contains the maximum size of the console window,
; given the current screen buffer size and font and the screen size.
;
; Retrieves information about the specified console screen buffer.
#EndFunction


#DefineFunction udfConGetScreenBufferInfo (handle, request)
bb = BinaryAlloc(22)
result = DllCall(StrCat(DirWindows(1),"KERNEL32.DLL"),long:"GetConsoleScreenBufferInfo",long:handle,lpbinary:bb)
If (result==0)
   result=DllLastError()
Else
   BinaryEodSet(bb,22)
   Select request
   Case 1
      result = BinaryPeek2(bb,0) ; Screenbuffer Size X
      Break
   Case 2
      result = BinaryPeek2(bb,2) ; Screenbuffer Size Y
      Break
   Case 3
      result = BinaryPeek2(bb,4) ; Cursor Pos X
      Break
   Case 4
      result = BinaryPeek2(bb,6) ; Cursor Pos Y
      Break
   Case 5
      result = BinaryPeek2(bb,8) ; Char Attr
      Break
   Case 6
      result = BinaryPeek2(bb,10) ; Win top left Pos X
      Break
   Case 7
      result = BinaryPeek2(bb,12) ; Win top left  Pos Y
      Break
   Case 8
      result = BinaryPeek2(bb,14) ; Win bottom right Pos X
      Break
   Case 9
      result = BinaryPeek2(bb,16) ; Win bottom right  Pos Y
      Break
   Case 10
      result = BinaryPeek2(bb,18) ; Console Window Max Size X
      Break
   Case 11
      result = BinaryPeek2(bb,20) ; Console Window Max Size Y
      Break
   Case 0
      result = ""
      For i=0 To 10
         result = ItemInsert(BinaryPeek2(bb,i+i),-1,result,@TAB)
      Next
      Break
   Case request
      result = 0
      Break
   EndSelect
EndIf
BinaryFree(bb)
Return (result)
; dwSize
; COORD structure that contains the size, in character columns and rows, of the screen buffer.
; dwCursorPosition
; COORD structure that contains the column and row coordinates of the cursor in the screen buffer.
; wAttributes
; Attributes of the characters written to a screen buffer by the WriteFile and WriteConsole functions,
; or echoed to a screen buffer by the ReadFile and ReadConsole functions.
; For more information, see Character Attributes.
; srWindow
; SMALL_RECT structure that contains the screen buffer coordinates of the display window.
; Left   X-coordinate of the upper left corner of the rectangle.
; Top    Y-coordinate of the upper left corner of the rectangle.
; Right  X-coordinate of the lower right corner of the rectangle.
; Bottom Y-coordinate of the lower right corner of the rectangle.
; dwMaximumWindowSize
; COORD structure that contains the maximum size of the console window,
; given the current screen buffer size and font and the screen size.
;
; Retrieves information about the specified console screen buffer.
#EndFunction


#DefineFunction udfConSetScreenBufferSize (hOutput, colx, rowy)
result = DllCall(StrCat(DirWindows(1),"KERNEL32.DLL"),long:"SetConsoleScreenBufferSize",long:hOutput,long:(colx+(rowy<<16)))
If (result==0) Then result=DllLastError()
Return (result)
; Changes the size of the specified console screen buffer.
#EndFunction


#DefineFunction udfConGetMaxWindowSize (handle, mode)
result = DllCall(StrCat(DirWindows(1),"KERNEL32.DLL"),long:"GetLargestConsoleWindowSize",long:handle)
If (result==0)
   result=DllLastError()
Else
   Select mode
   Case 1
      result = result&65535 ; colx
      Break
   Case 2
      result = result>>16 ; rowy
      Break
   Case 0
      result = StrCat(result&65535," ",result>>16)
      Break
   Case mode
      result = 0
      Break
   EndSelect
EndIf
Return (result)
; mode=0 returns string e.g. "125 55"
; mode=1 returns columns x
; mode=2 returns rows y
; Retrieves the size of the largest possible console window.
#EndFunction


#DefineFunction udfConScrollScreenBuffer (hOutput, stlx, stly, sbrx, sbry, ctlx, ctly, cbrx, cbry, dcolx, drowy, char, attr)
bbscroll = BinaryAlloc(8)
BinaryPoke2(bbscroll,0,stlx)
BinaryPoke2(bbscroll,2,stly)
BinaryPoke2(bbscroll,4,sbrx)
BinaryPoke2(bbscroll,6,sbry)
bbclip = BinaryAlloc(8)
BinaryPoke2(bbclip,0,ctlx)
BinaryPoke2(bbclip,2,ctly)
BinaryPoke2(bbclip,4,cbrx)
BinaryPoke2(bbclip,6,cbry)
bbfill = BinaryAlloc(4)
BinaryPoke2(bbfill,0,Char2Num(char))
BinaryPoke2(bbfill,2,attr)
result = DllCall(StrCat(DirWindows(1),"KERNEL32.DLL"),long:"ScrollConsoleScreenBufferA",long:hOutput,lpbinary:bbscroll,lpbinary:bbclip,long:(dcolx+(drowy<<16)),lpbinary:bbfill)
If (result==0) Then result=DllLastError()
BinaryFree(bbscroll)
BinaryFree(bbclip)
BinaryFree(bbfill)
Return (result)
; ScrollConsoleScreenBufferA(
;  hConsoleOutput     : Win32.Winnt.HANDLE;
;  lpScrollRectangle  : ac_SMALL_RECT_t;
;  lpClipRectangle    : ac_SMALL_RECT_t;
;  dwDestinationOrigin: COORD;
;  lpFill             : ac_CHAR_INFO_t)
; Moves a block of data in a screen buffer.
#EndFunction


#DefineFunction udfConSetWindowInfo (hOutput, tlx, tly, brx, bry, bAbs)
bAbs = Min(1,Max(0,bAbs))
bb = BinaryAlloc(8)
BinaryPoke2(bb,0,tlx)
BinaryPoke2(bb,2,tly)
BinaryPoke2(bb,4,brx)
BinaryPoke2(bb,6,bry)
result = DllCall(StrCat(DirWindows(1),"KERNEL32.DLL"),long:"SetConsoleWindowInfo",long:hOutput,long:bAbs,lpbinary:bb)
If (result==0) Then result=DllLastError()
BinaryFree(bb)
Return (result)
; hConsoleOutput Handle to a console screen buffer. The handle must have GENERIC_WRITE access.
; bAbsolute Specifies how the coordinates in the structure pointed to by the lpConsoleWindow parameter are used.
; If bAbsolute is TRUE, the coordinates specify the new upper-left and lower-right corners of the window.
; If it is FALSE, the coordinates are offsets to the current window-corner coordinates.
; lpConsoleWindow Pointer to a SMALL_RECT structure that contains values that determine
; the new upper-left and lower-right corners of the window.
;
; Sets the current size and position of a console screen buffer's window.
#EndFunction


#DefineFunction udfConFillScreenBuffer (hOutput, char, attr)
bb = BinaryAlloc(22)
result = DllCall(StrCat(DirWindows(1),"KERNEL32.DLL"),long:"GetConsoleScreenBufferInfo",long:hOutput,lpbinary:bb)
If (result==0)
   result=DllLastError()
Else
   BinaryEodSet(bb,22)
   len = BinaryPeek2(bb,0) * BinaryPeek2(bb,2)
   bbcount = BinaryAlloc(4)
   result = DllCall(StrCat(DirWindows(1),"KERNEL32.DLL"),long:"FillConsoleOutputAttribute",long:hOutput,long:attr,long:len,long:0,lpbinary:bbcount)
   If (result==0)
      result=DllLastError()
   Else
      result = DllCall(StrCat(DirWindows(1),"KERNEL32.DLL"),long:"FillConsoleOutputCharacterA",long:hOutput,long:Char2Num(char),long:len,long:0,lpbinary:bbcount)
      If (result==0)
         result=DllLastError()
      EndIf
   EndIf
EndIf
BinaryFree(bbcount)
BinaryFree(bb)
Return (result)
; Fills the entire screenbuffer with character and attribute, does a "cls" or "clearscreen".
#EndFunction
;
; ----- SCREENBUFFER METRICS ----------------------------------------------------------------------------------------------------



; ----- CURSOR ------------------------------------------------------------------------------------------------------------------
;
#DefineFunction udfConGetCursorInfo (hOutput)
bb = BinaryAlloc(5)
result = DllCall(StrCat(DirWindows(1),"KERNEL32.DLL"),long:"GetConsoleCursorInfo",long:hOutput,lpbinary:bb)
If (result==0)
   result=DllLastError()
Else
   BinaryEodSet(bb,5)
   result=StrCat(BinaryPeek4(bb,0)," ",BinaryPeek(bb,4))
EndIf
BinaryFree(bb)
Return (result)
; dwSize Percentage of the character cell that is filled by the cursor, value is between 1 and 100.
; The cursor appearance varies, ranging from completely filling the cell down to a horizontal line at the bottom of the cell.
; bVisible Visibility of the cursor. If the cursor is visible, this member is TRUE.
; This udf returns a string of two integer numbers, e.g. "12 1"
; dwSize 1..100
; bVisible 0..1
;
; Retrieves information about the size and visibility of the cursor for the specified console screen buffer.
#EndFunction


#DefineFunction udfConSetCursorInfo (hOutput, infostr)
If (ParseData(infostr)<>2) Then Return (0)
bb = BinaryAlloc(5)
BinaryPoke4(bb,0,Min(100,Max(1,param1))) ; percentage 1..100 (should be 1..99, because 100 is weird empty cell)
BinaryPoke(bb,4,Min(1,Max(0,param2)))  ; 0..1 resp @false..@true
result = DllCall(StrCat(DirWindows(1),"KERNEL32.DLL"),long:"SetConsoleCursorInfo",long:hOutput,lpbinary:bb)
If (result==0) Then result=DllLastError()
BinaryFree(bb)
Return (result)
; dwSize Percentage of the character cell that is filled by the cursor, value range is 1..100.
; The cursor appearance varies, ranging from completely filling the cell down to a horizontal line at the bottom of the cell.
; bVisible Visibility of the cursor. If the cursor is visible, this member is TRUE.
; infostr is a string e.g. "50,1" or "50|1", composed of "integer,delimiter,integer".
; If bVisible was set to false, then the dwSize is somewhat undefined (don't know for sure what happens there)
; therefore it needs always setting of both values at one time.
;
; Sets the size and visibility of the cursor for the specified console screen buffer.
#EndFunction


#DefineFunction udfConSetCursorPos (hOutput, colx, rowy)
result = DllCall(StrCat(DirWindows(1),"KERNEL32.DLL"),long:"SetConsoleCursorPosition",long:hOutput,long:(colx+(rowy<<16)))
If (result==0) Then result=DllLastError()
Return (result)
; colx = 0..maxx
; rowy = 0..maxy
; Sets the cursor position in the specified console screen buffer.
#EndFunction
;
; ----- CURSOR ------------------------------------------------------------------------------------------------------------------




; ----- WRITEFILE READFILE ------------------------------------------------------------------------------------------------------
;
#DefineFunction udfWriteFile (hOutput, str)
bbsize = StrLen(str)
bb = BinaryAlloc(bbsize)
BinaryPokeStr(bb,0,str)
bbcount = BinaryAlloc(4)
result = DllCall(StrCat(DirWindows(1),"KERNEL32.DLL"),long:"WriteFile",long:hOutput,lpbinary:bb,long:bbsize,lpbinary:bbcount,lpnull)
If (result==0) Then result=DllLastError()
BinaryFree(bbcount)
BinaryFree(bb)
Return (result)
; note: we do not use content of bbcount for anything in this implementation
#EndFunction


#DefineFunction udfReadFile (hInput)
bbsize = 4096 ; buffer is sized for file read, not for keyboard input
bb = BinaryAlloc(bbsize)
bbcount = BinaryAlloc(4)
result = DllCall(StrCat(DirWindows(1),"KERNEL32.DLL"),long:"ReadFile",long:hInput,lpbinary:bb,long:bbsize,lpbinary:bbcount,lpnull)
If (result==0)
   result=DllLastError()
Else
   BinaryEodSet(bb,bbsize)
   result = BinaryPeekStr(bb,0,bbsize)
EndIf
BinaryFree(bb)
BinaryFree(bbcount)
Return (result)
; note: we do not use content of bbcount for anything in this implementation
#EndFunction
;
;
; ----- WRITEFILE READFILE ------------------------------------------------------------------------------------------------------



; ----- CONSOLE WRITE READ ------------------------------------------------------------------------------------------------------
;
#DefineFunction udfConWriteConsole (hOutput, str)
bbsize = StrLen(str)
bb = BinaryAlloc(bbsize)
BinaryPokeStr(bb,0,str)
bbcount = BinaryAlloc(4)
result = DllCall(StrCat(DirWindows(1),"KERNEL32.DLL"),long:"WriteConsoleA",long:hOutput,lpbinary:bb,long:bbsize,lpbinary:bbcount,lpnull)
If (result==0) Then result=DllLastError()
BinaryFree(bbcount)
BinaryFree(bb)
Return (result)
; Writes a character string to a console screen buffer beginning at the current cursor location.
#EndFunction


#DefineFunction udfConReadConsole (hInput)
bbsize = 255 ; keyboard input buffer
bb = BinaryAlloc(bbsize)
bbcount = BinaryAlloc(4)
result = DllCall(StrCat(DirWindows(1),"KERNEL32.DLL"),long:"ReadConsoleA",long:hInput,lpbinary:bb,long:bbsize,lpbinary:bbcount,lpnull)
If (result==0)
   result=DllLastError()
Else
   BinaryEodSet(bb,bbsize)
   result=BinaryPeekStr(bb,0,bbsize)
EndIf
BinaryFree(bbcount)
BinaryFree(bb)
Return (result)
; Reads character input from the console input buffer and removes it from the buffer.
#EndFunction
;
; ----- CONSOLE WRITE READ FLUSH ------------------------------------------------------------------------------------------------



; ----- WRITE READ LOW LEVEL ----------------------------------------------------------------------------------------------------
;
#DefineFunction udfConFlushInputBuffer (hInput)
result = DllCall(StrCat(DirWindows(1),"KERNEL32.DLL"),long:"FlushConsoleInputBuffer",long:hInput)
If (result==0) Then result=DllLastError()
Return (result)
; Flushes the console input buffer.
#EndFunction


#DefineFunction udfConSetTextAttribute (hOutput, attr)
result = DllCall(StrCat(DirWindows(1),"KERNEL32.DLL"),long:"SetConsoleTextAttribute",long:hOutput,long:attr)
If (result==0) Then result=DllLastError()
Return (