Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

[Tips] Clear stdout


  • Please log in to reply
4 replies to this topic
nimda
  • Members
  • 4368 posts
  • Last active: Aug 09 2015 02:36 AM
  • Joined: 26 Dec 2010
I was writing a console app through DllCall("AllocConsole") etc. However, I found no way to clear the screen. Sending the "screen clear character" or Chr(12) did not work, neither did any character from 1-255

So here is the way you clear StdOut:
run command.com /C cls
:)
Writing to it still works afterwards, however you will be left with a hanging window when your program exits

(<!-- m -->http://php.net/manua...andline.php<!-- m --> where the last solution worked)
tags: clear screen, clear cmd screen, clear stdout, clear screen character, clear cmd, cls


engunneer
  • Moderators
  • 9162 posts
  • Last active: Sep 12 2014 10:36 PM
  • Joined: 30 Aug 2005
clearing it again might let the window close after exit

Lexikos
  • Administrators
  • 9844 posts
  • AutoHotkey Foundation
  • Last active:
  • Joined: 17 Oct 2006

however you will be left with a hanging window when your program exits

That shouldn't happen. I'd test it, but command.com doesn't exist on my system. This seems to work just fine for me:
RunWait %Comspec% /c cls
Alternatively,
[color=#666666]DllCall("AllocConsole")
FileAppend % "Bla, bla, bla`n", CONOUT$
Loop 5 {
    FileAppend % "`rClearing in " 6-A_Index "...", CONOUT$
    Sleep 1000
}[/color]

; Get a handle to the console screen buffer.
hconout := (conout := FileOpen("CONOUT$", "rw")).__handle

; Get size of console buffer.
VarSetCapacity(info, 22, 0)
DllCall("GetConsoleScreenBufferInfo", "ptr", hconout, "ptr", &info)
length := NumGet(info, 0, "short") * NumGet(info, 2, "short")
; Zero-fill entire console buffer.
DllCall("FillConsoleOutputCharacter", "ptr", hconout
        , "int", 0, "uint", length, "int", 0, "uint*", 0)
; Reset cursor to position 0,0.
DllCall("SetConsoleCursorPosition", "ptr", hconout, "int", 0)

[color=#666666]Sleep 3000[/color]
AutoHotkey_L required for FileOpen and ptr DllCall type.

CodeKiller
  • Members
  • 2067 posts
  • Last active: Feb 26 2016 09:30 AM
  • Joined: 10 Jul 2008
Since Windows XP :

cmd /c cls


cmd => cmd.exe

C:>cmd /?
Starts a new instance of the Windows XP command interpreter

CMD [/Q] [/C | /K] string

/C Carries out the command specified by string and then terminates
/K Carries out the command specified by string but remains
/Q Turns echo off


By the way, cls just mean "CLear Screen"... It exists since MS-DOS...

Lexikos
  • Administrators
  • 9844 posts
  • AutoHotkey Foundation
  • Last active:
  • Joined: 17 Oct 2006

cmd => cmd.exe

%ComSpec% => cmd.exe :roll: