3

Having looked around for the best way to perform the (supposedly) simple task of clearing a cmd.exe console window and finding out that simply using system('cls'); is wrong (in the line of "For every complex problem there is an answer that is clear, simple, and wrong") and finding that simply copying and pasting the win32 code into a function results in compilation errors, I then found the following function, but have no idea what I have to do to call it with a variable it can work with without spewing error messages:

#include <windows.h>
#include <stdlib.h>
#include <stdio.h>

/* Standard error macro for reporting API errors */ 
#define PERR(bSuccess, api){ if(!(bSuccess)) printf("%s:Error %d from %s \
  on line %d\n", __FILE__, GetLastError(), api, __LINE__); }

HWND GetConsoleHwnd(void) {
  #define MY_BUFSIZE 1024  // Buffer size for console window titles.
  HWND hwndFound;  // This is what is returned to the caller.
  char pszNewWindowTitle[MY_BUFSIZE]; // Contains fabricated WindowTitle.
  char pszOldWindowTitle[MY_BUFSIZE]; // Contains original WindowTitle.

  // Fetch current window title.
  GetConsoleTitle(pszOldWindowTitle, MY_BUFSIZE);
  // Format a "unique" NewWindowTitle.
  wsprintf(pszNewWindowTitle,"%d/%d", GetTickCount(), GetCurrentProcessId());
  // Change current window title.
  SetConsoleTitle(pszNewWindowTitle);
  // Ensure window title has been updated.
  Sleep(40);

  // Look for NewWindowTitle.
  hwndFound=FindWindow(NULL, pszNewWindowTitle);
  // Restore original window title.
  SetConsoleTitle(pszOldWindowTitle);

  return(hwndFound);
}

void cls( HANDLE hConsole ) {
  COORD coordScreen = { 0, 0 };  /* here's where we'll home the cursor */ 
  BOOL bSuccess;
  DWORD cCharsWritten;
  CONSOLE_SCREEN_BUFFER_INFO csbi;  /* to get buffer info */ 
  DWORD dwConSize;  /* number of character cells in the current buffer */ 

  /* get the number of character cells in the current buffer */ 
  bSuccess = GetConsoleScreenBufferInfo( hConsole, &csbi );
  PERR( bSuccess, "GetConsoleScreenBufferInfo" );
  dwConSize = csbi.dwSize.X * csbi.dwSize.Y;

  /* fill the entire screen with blanks */ 
  bSuccess = FillConsoleOutputCharacter( hConsole, (TCHAR) ' ',
    dwConSize, coordScreen, &cCharsWritten );
  PERR( bSuccess, "FillConsoleOutputCharacter" );

  /* get the current text attribute */ 
  bSuccess = GetConsoleScreenBufferInfo( hConsole, &csbi );
  PERR( bSuccess, "ConsoleScreenBufferInfo" );

  /* now set the buffer's attributes accordingly */ 
  bSuccess = FillConsoleOutputAttribute( hConsole, csbi.wAttributes,
    dwConSize, coordScreen, &cCharsWritten );
  PERR( bSuccess, "FillConsoleOutputAttribute" );

  /* put the cursor at (0, 0) */ 
  bSuccess = SetConsoleCursorPosition( hConsole, coordScreen );
  PERR( bSuccess, "SetConsoleCursorPosition" );

  return;
}

int main (void) {
  // why does this fail?
  HWND cons = GetConsoleHwnd();
  cls(cons);

  return 0;
}

My question is this: How do I set up a console handle to pass to the `cls` function?

Edit: Please note that I do not wish to simply call/invoke the cls or clear command(s) via a call to system as advised in the majority of answers to this question.

Agi Hammerthief
  • 2,114
  • 1
  • 22
  • 38
  • 1
    Perhaps if you told us the compiler errors you're getting... –  Dec 25 '13 at 22:21
  • 1
    [`AllocConsole`](http://msdn.microsoft.com/en-us/library/windows/desktop/ms681944.aspx), for example. However, it is a common misconception, that programming consists mainly of clicking together snippets found on the intarwebs, without knowing what you're doing. Sorry to break the news, this isn't going to fly. – IInspectable Dec 25 '13 at 22:36
  • The listing compiles fine, but I get the following errors, which mean nothing to me: "cls.c:Error 6 from GetConsoleScreenBufferInfo on line 59 ; cls.c:Error 6 from FillConsoleOutputCharacter on line 66 ; cls.c:Error 6 from ConsoleScreenBufferInfo on line 71 ; cls.c:Error 6 from FillConsoleOutputAttribute on line 77 ; cls.c:Error 6 from SetConsoleCursorPosition on line 82" My C programming skill (or lack thereof is at beginner, so most of this stuff is over my head.) All I wanted was a simple little program to clear a console screen. – Agi Hammerthief Dec 25 '13 at 22:38
  • Lesson 1: If the compiler says you are wrong, you are wrong. – IInspectable Dec 25 '13 at 22:39
  • @IInspectable I'm not new to programming, having three years of PHP and Drupal behind me. I am, however, new to C, so I'll thank you not to be condescending (while admitting that, yes, in this case, I don't know what I'm doing.) I know that the compiler says the code's wrong. What I don't know is why and how I go about correcting it. Come to that, I don't even know what its output means other than on what lines and in what file the errors occur. – Agi Hammerthief Dec 25 '13 at 22:46
  • I was commenting on the "The listing compiles fine, but I get [...] errors" part of your comment. A more professional comment was provided by H2CO3: Please, do provide the exact error messages. – IInspectable Dec 25 '13 at 22:51
  • Fair enough. It's had me at the end of my leash for the better part of today, so I'm not in the best of moods. – Agi Hammerthief Dec 25 '13 at 23:00
  • Possible duplicate of [How can I clear console](https://stackoverflow.com/questions/6486289/how-can-i-clear-console) – Tanveer Badar Aug 04 '17 at 16:06
  • The GetConsoleHwnd() subroutine in the listing is appropriate for finding a handle to use with GDI graphic functions. – trindflo Sep 09 '21 at 18:20

1 Answers1

7

Get the console handle using GetStdHandle(STD_OUTPUT_HANDLE) if the standard output was not redirected. If it could be redirected but you want to clear the real console nevertheless, open the CONOUT$ pseudo-file using CreateFile function.

Anton Kovalenko
  • 20,999
  • 2
  • 37
  • 69
  • 1
    Thank you, sir that did it! : int main (void) { HWND cons = GetStdHandle(STD_OUTPUT_HANDLE); cls(cons); return 0; } – Agi Hammerthief Dec 25 '13 at 22:55
  • Every time I look at this answer (frequently), I wish it was possible to upvote it. – Agi Hammerthief May 04 '15 at 18:38
  • 2
    Note for completeness that it should be `HANDLE cons` not `HWND cons`. It may also be worth mentioning for the benefit of future readers that the reason the original code didn't work was that it was passing a window handle to a function expecting a console handle, and they're not at all the same thing. – Harry Johnston May 02 '17 at 21:26
  • 2
    Although the question specifically asks for a Windows solution (which the answer caters for), I'm adding this for completeness: The UNIX/POSIX solution is to simply write `"\033[2J\033[3J\n\0"` to `stdout` (file handle #1), as mentioned in [the man page for `clear(1)`](http://man7.org/linux/man-pages/man1/clear.1.html) – Agi Hammerthief Mar 06 '19 at 07:20