Conversion from C

Windows specific forum
Armoured
Enthusiast
Enthusiast
Posts: 346
Joined: Mon Jan 26, 2004 11:39 am
Location: ITALY
Contact:

Conversion from C

Post by Armoured »

Hi,
This code in C clear the console:

Code: Select all

#include <windows.h>

void ClearScreen()
  {
  HANDLE                     hStdOut;
  CONSOLE_SCREEN_BUFFER_INFO csbi;
  DWORD                      count;
  DWORD                      cellCount;
  COORD                      homeCoords = { 0, 0 };

  hStdOut = GetStdHandle( STD_OUTPUT_HANDLE );
  if (hStdOut == INVALID_HANDLE_VALUE) return;

  /* Get the number of cells in the current buffer */
  if (!GetConsoleScreenBufferInfo( hStdOut, &csbi )) return;
  cellCount = csbi.dwSize.X *csbi.dwSize.Y;

  /* Fill the entire buffer with spaces */
  if (!FillConsoleOutputCharacter(
    hStdOut,
    (TCHAR) ' ',
    cellCount,
    homeCoords,
    &count
    )) return;

  /* Fill the entire buffer with the current colors and attributes */
  if (!FillConsoleOutputAttribute(
    hStdOut,
    csbi.wAttributes,
    cellCount,
    homeCoords,
    &count
    )) return;

  /* Move the cursor home */
  SetConsoleCursorPosition( hStdOut, homeCoords );
  }
  
I have converted it to this:

Code: Select all

Procedure Cls()
  Protected hStdOut = GetStdHandle_(#STD_OUTPUT_HANDLE)
  Protected count
  Protected cellCount
  Protected homeCoords.COORD
  homeCoords\x = 0
  homeCoords\y = 0
  Protected csbi.CONSOLE_SCREEN_BUFFER_INFO
  
  If (hStdOut = #INVALID_HANDLE_VALUE)
    Debug "Handler console non valido."
    ProcedureReturn
  EndIf
      
  ;Get the number of character cells in the current buffer.
  If (GetConsoleScreenBufferInfo_(hStdOut, csbi.CONSOLE_SCREEN_BUFFER_INFO) = 0)
    ProcedureReturn
  EndIf
  
  cellCount = (csbi\dwSize\X) * (csbi\dwSize\Y)
  Debug "cellCount = " + Str(cellCount)
  
  ;Fill the entire screen With blanks.
  If (FillConsoleOutputCharacter_(hStdOut,' ', cellCount, homeCoords.COORD,@count) = 0)
    ProcedureReturn
  EndIf
  
   Debug "count = " + Str(count)
  
  ;Get the current text attribute.
  If (GetConsoleScreenBufferInfo_(hStdOut, csbi.CONSOLE_SCREEN_BUFFER_INFO) = 0)
    ProcedureReturn
  EndIf
  
  
  ;Set the buffer's attributes accordingly.
  If (FillConsoleOutputAttribute_(hStdOut,csbi\wAttributes,cellCount,homeCoords.COORD,@count) = 0)
    ProcedureReturn
  EndIf
  
  Debug "cellCount = " + Str(cellCount)
  
  ;Put the cursor at its home coordinates.
  SetConsoleCursorPosition_(hStdOut, homeCoords.COORD);
EndProcedure


OpenConsole()
PrintN("asdasdasdsadasdasdsadasdasdasdasdasdasdasdasdasdas")
PrintN("asdasdasdsadasdasdsadasdasdasdasdasdasdasdasdasdas")
Cls()
Delay(20000)
But doesn't work any idea of why? :|
Thanks.
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

Re: Conversion from C

Post by Mijikai »

Try this:

Code: Select all

Procedure.i Cls()
  Protected hout.i
  Protected csbi.CONSOLE_SCREEN_BUFFER_INFO  
  hout = GetStdHandle_(#STD_OUTPUT_HANDLE)
  If hout <> #INVALID_HANDLE_VALUE
    If GetConsoleScreenBufferInfo_(hout,@csbi)
      If FillConsoleOutputCharacter_(hout,#Null$,csbi\dwSize\x * csbi\dwSize\y,#Null,@csbi)
        SetConsoleCursorPosition_(hout,#Null)
        ProcedureReturn #True
      EndIf
    EndIf
  EndIf
  ProcedureReturn #False
EndProcedure
Armoured
Enthusiast
Enthusiast
Posts: 346
Joined: Mon Jan 26, 2004 11:39 am
Location: ITALY
Contact:

Re: Conversion from C

Post by Armoured »

Thanks Mijikai!
Ok your version works well but why in the fillconsoleoutputcharacter you have inserted at the last two parameters a #Null and @csbi?
If I read here (https://learn.microsoft.com/en-us/windo ... tcharacter):

BOOL WINAPI FillConsoleOutputCharacter(
_In_ HANDLE hConsoleOutput,
_In_ TCHAR cCharacter,
_In_ DWORD nLength,
_In_ COORD dwWriteCoord,
_Out_ LPDWORD lpNumberOfCharsWritten
);

The last two parameters are a COORD structure and a variable where memorize the number of written chars.
Do you have any books or sources of information to recommend?
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

Re: Conversion from C

Post by Mijikai »

#Null is coord 0x0 the API wants the struct to be passed as long so all is fine.
@csbi - reserving space for another int is just wasting memory (so i just use whats already there),
unless whats overwritten would be needed somewhere later.
Armoured
Enthusiast
Enthusiast
Posts: 346
Joined: Mon Jan 26, 2004 11:39 am
Location: ITALY
Contact:

Re: Conversion from C

Post by Armoured »

Ok thanks again!
Post Reply