Porting Borland console programs to Win32 using MS Visual C++

The Borland version


#include <stdio.h>
#include <conio.h>

void main()
{
	clrscr();
	gotoxy(10, 10);
	printf("This line will be deleted");
	getch();

	gotoxy(10, 10);
	clreol();
	printf("Press any key to quit");
	getch();
}

Now for the Win32 version in Visual C++


#include<windows.h>
#include<wincon.h>
#include<stdio.h>
#include<conio.h>

HANDLE g_hConsole = 0;
DWORD  g_dwScreenChars = 0;

int getch( void );
void clrscr(void);
void clreol(void );
void gotoxy(SHORT nX, SHORT nY);

int main(void)
{
    
    // Get a handle to the console
    g_hConsole = GetStdHandle( STD_OUTPUT_HANDLE );

    // Get the number of characters in the console
    CONSOLE_SCREEN_BUFFER_INFO sInfo={0};
    GetConsoleScreenBufferInfo( g_hConsole , &sInfo);
    g_dwScreenChars = sInfo.dwMaximumWindowSize.X * 
                      sInfo.dwMaximumWindowSize.Y ;



    // Now for some good old fashioned work
    clrscr();
	gotoxy(10, 10);
	printf("This line will be deleted");
	getch();

	gotoxy(10, 10);
	clreol();
	printf("Press any key to quit");
	getch();


 
    return 0;
}



void clrscr(void)
{
    COORD sCoord = {0,0};
    DWORD dwNumberOfCharsWritten=0;
    FillConsoleOutputCharacter( g_hConsole,
                                ' ', 
                                g_dwScreenChars, 
                                sCoord, 
                                &dwNumberOfCharsWritten); 
 
}; 
 
void clreol(void )
{
    // Get the current cursor position
    CONSOLE_SCREEN_BUFFER_INFO sInfo={0};
    GetConsoleScreenBufferInfo( g_hConsole , &sInfo);

    // Get the distance to the end of the line
    SHORT nDistance = sInfo.dwSize.X - 
                      sInfo.dwCursorPosition.X ;

    // Now fill from here with that many spaces
    DWORD dwNumberOfCharsWritten=0;
    FillConsoleOutputCharacter( g_hConsole,
                                ' ', 
                                nDistance, 
                                sInfo.dwCursorPosition, 
                                &dwNumberOfCharsWritten); 

    // Move the cursor back
    SetConsoleCursorPosition( g_hConsole,sInfo.dwCursorPosition);
}

void gotoxy(SHORT nX, SHORT nY)
{
    COORD sCoord = {nX,nY};
    SetConsoleCursorPosition( g_hConsole,sCoord);
}

int getch( void )
{
    return _getch();
}

Download source

HowTo