Console Clear issue

Windows specific questions.
Post Reply
UEZ
Posts: 972
Joined: May 05, 2017 19:59
Location: Germany

Console Clear issue

Post by UEZ »

I've ported example 2 and example 3 from here https://docs.microsoft.com/en-us/window ... the-screen to FB but the result is not the same.

In line 23 (ClrConsole2) and line 39 (ClrConsole3) you can set the character which will be used to fill the console window. When I compile the code C code then it works as expected. The character will be used for the "clearing" of the console window.

Code: Select all

#Include "windows.bi"

Dim Shared As HANDLE hStdout
hStdout = GetStdHandle(STD_OUTPUT_HANDLE)

Sub ClrConsole(iSleep As Ushort = 100)
	Shell("Cls")
	Sleep(iSleep)
End Sub

Sub ClrConsole2(hConsole As HANDLE) 'https://docs.microsoft.com/en-us/windows/console/clearing-the-screen example 2
	Dim As CONSOLE_SCREEN_BUFFER_INFO csbi
	Dim As SMALL_RECT scrollRect
	Dim As COORD scrollTarget
	Dim As CHAR_INFO fill
	GetConsoleScreenBufferInfo(hConsole, @csbi)
	scrollRect.Left = 0
    scrollRect.Top = 0
    scrollRect.Right = csbi.dwSize.X
    scrollRect.Bottom = csbi.dwSize.Y
	scrollTarget.X = 0
	scrollTarget.Y = (0 - csbi.dwSize.Y)
	fill.Char.UnicodeChar = Cast(WCHAR, "_")
	fill.Attributes = csbi.wAttributes
	ScrollConsoleScreenBuffer(hConsole, @scrollRect, NULL, scrollTarget, @fill)
    csbi.dwCursorPosition.X = 0
    csbi.dwCursorPosition.Y = 0
	SetConsoleCursorPosition(hConsole, csbi.dwCursorPosition)
End Sub

Sub ClrConsole3(hConsole As HANDLE) 'https://docs.microsoft.com/en-us/windows/console/clearing-the-screen example 3
	Dim As CONSOLE_SCREEN_BUFFER_INFO csbi
	Dim As COORD coordScreen = Type(0, 0)
	Dim As DWORD cCharsWritten
	Dim As CONSOLE_SCREEN_BUFFER_INFO csb
	Dim As DWORD dwConSize
	GetConsoleScreenBufferInfo(hConsole, @csbi)
	dwConSize = csbi.dwSize.X * csbi.dwSize.Y
	FillConsoleOutputCharacter(hConsole, Cast(CHAR, "."), dwConSize, coordScreen, @cCharsWritten)
	GetConsoleScreenBufferInfo(hConsole, @csbi)
	FillConsoleOutputAttribute(hConsole, csbi.wAttributes, dwConSize, coordScreen, @cCharsWritten)
	SetConsoleCursorPosition(hConsole, coordScreen)
End Sub


For i As Ubyte = 1 To 200
? "Line1",
Next
Sleep(500)
ClrConsole2(hStdout)
? "Clr"
Sleep
Why it is not working with FB?
SARG
Posts: 1755
Joined: May 27, 2005 7:15
Location: FRANCE

Re: Console Clear issue

Post by SARG »

Hi UEZ,

It took me a bit of time for finding the reason as all worked fine (no error).

Replace Cast(WCHAR, "_") by asc("_")
Replace Cast(CHAR, ".") by asc(".")
UEZ
Posts: 972
Joined: May 05, 2017 19:59
Location: Germany

Re: Console Clear issue

Post by UEZ »

Thank you SARG for investigating. :-)

As I'm absolute a C/C++ novice I would never get the idea that character to write to the buffer should be the ASCII code rather than the character itself, particularly there is nothing written on the website about the ASCII code.

Anyhow, the clearing of the window behavior seems to be a little more different than

Code: Select all

Sub ClrConsole(iSleep As Ushort = 100)
   Shell("Cls")
   Sleep(iSleep)
End Sub
Because when I use one of the examples (example 2 and 3) in my Radio Station Player then the icon will be overwritten with blanks chars and the gfx flickers at least for Windows 7. Windows 10 seems to be working properly.
SARG
Posts: 1755
Joined: May 27, 2005 7:15
Location: FRANCE

Re: Console Clear issue

Post by SARG »

Just an idea : try with less characters : dwConSize=25*80 or even less 100.
UEZ
Posts: 972
Joined: May 05, 2017 19:59
Location: Germany

Re: Console Clear issue

Post by UEZ »

SARG wrote:Just an idea : try with less characters : dwConSize=25*80 or even less 100.
Hmm, I need all the lines to provide some information.

Anothing thing is that from time to time the program hangs for approx. 1 second and afterward it continues. Rarely it closes completely. I don't know how to debug it properly.

What I see in the event log:

Code: Select all

Faulting application name: Radio Station remix.kwed.org.exe, version: 0.0.0.0, time stamp: 0x60a3fbb8
Faulting module name: OLEAUT32.dll, version: 10.0.19041.985, time stamp: 0xfc8f0d5b
Exception code: 0xc0000005
Fault offset: 0x0001e774
Faulting process id: 0x531c
Faulting application start time: 0x01d74c0cb29b3598
Faulting application path: C:\...\Radio Station remix.kwed.org\Radio Station remix.kwed.org.exe
Faulting module path: C:\WINDOWS\System32\OLEAUT32.dll
Report Id: dd15a11b-74b6-4c60-b22e-129cce839f84
Faulting package full name: 
Faulting package-relative application ID: 
OLEAUT32.dll will not be used by my includes / program nor by bass.dll but conhost.exe seems to use it. Maybe to use the console for such things is too heavy for it...
adeyblue
Posts: 299
Joined: Nov 07, 2019 20:08

Re: Console Clear issue

Post by adeyblue »

OLEAUt32 is where COM strings/BSTRs come from. Something might be corrupting one of those somewhere.
Maybe try setting the OANOCACHE environment variable to 1 then run your program (i.e. it has to be set before your app starts, your app can't set it for itself). That turns off the BSTR caching and might make crashes more reproducable, if that is the problem anyway.
UEZ
Posts: 972
Joined: May 05, 2017 19:59
Location: Germany

Re: Console Clear issue

Post by UEZ »

adeyblue wrote:OLEAUt32 is where COM strings/BSTRs come from. Something might be corrupting one of those somewhere.
Maybe try setting the OANOCACHE environment variable to 1 then run your program (i.e. it has to be set before your app starts, your app can't set it for itself). That turns off the BSTR caching and might make crashes more reproducable, if that is the problem anyway.
Never heard something about CSTRs/BSTRs.

Anyhow, you mean open a cmd window, type "set oanocache=1" and then start the exe?

You mean I cannot use SetOaNoCache calling it from OleAut32.dll directly inside my program? -> https://docs.microsoft.com/en-us/previo ... toanocache
Post Reply