|< Windows Process & Threads: C Run-Time 4 | Main | Windows Process & Threads: C Run-Time 6 >| Site Index | Download |


 

 

 

 

MODULE S1

PROCESSES AND THREADS: C RUN-TIME

Part 5: Program Examples

 

 

 

 

 

What do we have in this Module?

  1. Functions & Misc.

  2. WriteConsoleOutputCharacter()

  3. WriteConsoleOutputAttribute()

  4. Writing Characters or Colors to Consecutive Cells

  5. FillConsoleOutputCharacter()

  6. FillConsoleOutputAttribute()

 

 

 

 

 

 

 

 

 

 

 

 

My Training Period: xx hours. Before you begin, read some instruction here.

 

The expected abilities:

  • Able to understand and use functions from Windows C Run-Time Library in your process and thread programs.

  • Able to use Visual C++ / .Net compiler to build and run your process and thread programs.

  • Able to understand and use various functions related to process and thread in your programs.

WriteConsoleOutputCharacter()

 

Information

Description

The function

WriteConsoleOutputCharacter().

The use

Copies a number of characters to consecutive cells of a console screen buffer, beginning at a specified location.

The prototype

BOOL WriteConsoleOutputCharacter(

  HANDLE hConsoleOutput,

  LPCTSTR lpCharacter,

  DWORD nLength,

  COORD dwWriteCoord,

  LPDWORD lpNumberOfCharsWritten);

Example

-

The parameters

hConsoleOutput - [in] Handle to the console screen buffer. The handle must have the GENERIC_READ access right.

lpCharacter - [in] Pointer to a buffer that contains the characters to write to the console screen buffer. The total size must be less than 64K.

nLength - [in] Number of TCHARs to be written.

dwWriteCoord - [in] A COORD structure that specifies the coordinates of the first cell in the console screen buffer to which characters will be written.

lpNumberOfCharsWritten - [out] Pointer to a variable that receives the number of TCHARs actually written.

The return value

If the function succeeds, the return value is nonzero. If the function fails, the return value is zero. To get extended error information, call GetLastError().

The header file

<windows.h>

Remarks

Implemented as Unicode and ANSI versions. Note that Unicode support on Windows Me/98/95 requires Microsoft Layer for Unicode.

If the number of characters to be written to extends beyond the end of the specified row in the console screen buffer, characters are written to the next row. If the number of characters to be written to extends beyond the end of the console screen buffer, characters are written up to the end of the console screen buffer. The attribute values at the positions written to are not changed. This function uses either Unicode characters or 8-bit characters from the console's current code page. The console's code page defaults initially to the system's OEM code page. To change the console's code page, use the SetConsoleCP() or SetConsoleOutputCP() functions, or use the chcp or mode con cp select= commands.

For Windows Me/98/95:  WriteConsoleOutputCharacterW() is supported by the Microsoft Layer for Unicode. To use this, you must add certain files to your application, as outlined in Microsoft Layer for Unicode on Windows Me/98/95 Systems.

 

Table 5

 

WriteConsoleOutputAttribute()

 

Information

Description

The function

WriteConsoleOutputAttribute().

The use

Copies a number of character attributes to consecutive cells of a console screen buffer, beginning at a specified location.

The prototype

BOOL WriteConsoleOutputAttribute(

  HANDLE hConsoleOutput,

  const WORD* lpAttribute,

  DWORD nLength,

  COORD dwWriteCoord,

  LPDWORD lpNumberOfAttrsWritten);

Example

-

The parameters

hConsoleOutput - [in] Handle to the console screen buffer. The handle must have the GENERIC_READ access right.

lpAttribute - [in] Pointer to a buffer that contains the attributes to use when writing to the console screen buffer.

nLength - [in] Number of screen buffer character cells to which the attributes will be copied. The total must be less than 64K.

dwWriteCoord - [in] A COORD structure that specifies the coordinates of the first cell in the console screen buffer to which the attributes will be written.

lpNumberOfAttrsWritten - [out] Pointer to a variable that receives the number of attributes actually written to the console screen buffer.

The return value

If the function succeeds, the return value is nonzero. If the function fails, the return value is zero. To get extended error information, call GetLastError().

The header file

<windows.h>

Remarks

If the number of attributes to be written to extends beyond the end of the specified row in the console screen buffer, attributes are written to the next row. If the number of attributes to be written to extends beyond the end of the console screen buffer, the attributes are written up to the end of the console screen buffer. The character values at the positions written to are not changed.

 

Table 6

 

A Program example

 

Writing Characters or Colors to Consecutive Cells

 

Characters or color attributes can be written to specified character cells in a screen buffer. The following example uses the WriteConsoleOutputCharacter() function to write a string of characters beginning at the upper left corner of a screen buffer. Then the example uses the WriteConsoleOutputAttribute() function to write a string of color attributes to the first 51 cells of the same row. The coord parameter for both functions specifies the character cell in the console screen buffer at which writing begins. The location in the console window where these characters or colors appear depends on the current window rectangle of the console screen buffer. Note that MyErrorExit() is a placeholder for an application-defined function to display and handle error conditions.

 

#include <windows.h>

#include <stdio.h>

 

CONSOLE_SCREEN_BUFFER_INFO csbi;

 

int main()

{

HANDLE hStdOut;

    LPTSTR lpszString = "Testing a character StRiNg";

    DWORD cWritten;

    BOOL fSuccess;

    COORD coord;

    WORD wColors[3];

hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);

if (hStdOut == INVALID_HANDLE_VALUE)

    printf("GetStdHandle() failed, error: %d.\n", GetLastError());

else

    printf("GetStdHandle() is OK.\n");

/* Get display screen's text row and column information. */

if (GetConsoleScreenBufferInfo(hStdOut, &csbi) == 0)

    printf("GetConsoleScreenBufferInfo() failed, error: %d.\n", GetLastError());

else

    printf("GetConsoleScreenBufferInfo() is OK.\n");

    /* Write a string of characters to a screen buffer. */

    coord.X = 10;            // start at tenth cell

    coord.Y = 10;            //   of tenth row     

    fSuccess = WriteConsoleOutputCharacter(

        hStdOut,              // screen buffer handle

        lpszString,           // pointer to source string

        lstrlen(lpszString),  // length of string

        coord,                // first cell to write to

        &cWritten);           // actual number written

    if (! fSuccess)

        printf("WriteConsoleOutputCharacter() failed.\n");

    else

        printf("WriteConsoleOutputCharacter() is OK.\n");

    /* Write a string of colors to a screen buffer. */

    wColors[0] = BACKGROUND_RED;

    wColors[1] =  BACKGROUND_GREEN;

    /* for white - BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE; */

    wColors[2] = BACKGROUND_BLUE;

    for (;fSuccess && coord.X < 50; coord.X += 3)

    {

        fSuccess = WriteConsoleOutputAttribute(

            hStdOut,          // screen buffer handle

            wColors,          // pointer to source string

            3,                // length of string

            coord,            // first cell to write to

            &cWritten);       // actual number written

    }

    if (! fSuccess)

        printf("WriteConsoleOutputAttribute() failed.\n");

    else

       printf("WriteConsoleOutputAttribute() is OK.\n");

      

       return 0;

}

 

A sample output:

 

The same character or color attribute can be written to a specified number of consecutive screen buffer cells beginning at a specified location. The following example uses the FillConsoleOutputCharacter() function to clear a 30-by-20-character screen buffer, and then it uses the FillConsoleOutputAttribute() function to set the color attributes of the same cells.

 

#include <windows.h>

#include <stdio.h>

 

CONSOLE_SCREEN_BUFFER_INFO csbi;

 

int main()

{

       HANDLE hStdOut;

    DWORD cWritten;

    BOOL fSuccess;

    COORD coord;

    WORD wColor;

    CHAR chFillChar;

hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);

if (hStdOut == INVALID_HANDLE_VALUE)

       printf("GetStdHandle() failed, error: %d.\n", GetLastError());

else

       printf("GetStdHandle() is OK.\n");

/* Get display screen's text row and column information. */

if (GetConsoleScreenBufferInfo(hStdOut, &csbi) == 0)

       printf("GetConsoleScreenBufferInfo() failed, error: %d.\n", GetLastError());

else

       printf("GetConsoleScreenBufferInfo() is OK.\n");

    // Fill a 30-by-20-character screen buffer with the space character.

    coord.X = 0;            // start at 0th cell

    coord.Y = 6;            //   of sixth row

    chFillChar = ' ';

    fSuccess = FillConsoleOutputCharacter(

        hStdOut,          // screen buffer handle

        chFillChar,       // fill with spaces

        30*20,            // number of cells to fill

        coord,            // first cell to write to

        &cWritten);       // actual number written

       if (! fSuccess)

           printf("WriteConsoleOutputCharacter() failed.\n");

       else

          printf("WriteConsoleOutputCharacter() is OK.\n");

    // Set 30-by-20-character screen buffer colors to white text on red.

    wColor = BACKGROUND_RED | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;

    fSuccess = FillConsoleOutputAttribute(

        hStdOut,          // screen buffer handle

        wColor,           // color to fill with

        30*20,            // number of cells to fill

        coord,            // first cell to write to

        &cWritten);       // actual number written

       if (!fSuccess)

          printf("WriteConsoleOutputAttribute() failed.\n");

       else

          printf("WriteConsoleOutputAttribute() is OK.\n");

       return 0;

}

 

A sample output:

 

 

 

FillConsoleOutputCharacter()

 

Information

Description

The function

FillConsoleOutputCharacter().

The use

Writes a character to the console screen buffer a specified number of times, beginning at the specified coordinates.

The prototype

BOOL FillConsoleOutputCharacter(

  HANDLE hConsoleOutput,

  TCHAR cCharacter,

  DWORD nLength,

  COORD dwWriteCoord,

  LPDWORD lpNumberOfCharsWritten);

Example

-

The parameters

hConsoleOutput - [in] Handle to a console screen buffer. The handle must have the GENERIC_WRITE access right.

cCharacter - [in] Character to write to the console screen buffer.

nLength - [in] Number of character cells to which the character should be written.

dwWriteCoord - [in] A COORD structure that specifies the console screen buffer coordinates of the first cell to which the character is to be written.

lpNumberOfCharsWritten - [out] Pointer to a variable that receives the number of characters actually written to the console screen buffer.

The return value

If the function succeeds, the return value is nonzero. If the function fails, the return value is zero. To get extended error information, call GetLastError().

The header file

<windows.h>

Remarks

Implemented as Unicode and ANSI versions. Note that Unicode support on Windows Me/98/95 requires Microsoft Layer for Unicode. If the number of characters to write to extends beyond the end of the specified row in the console screen buffer, characters are written to the next row. If the number of characters to write to extends beyond the end of the console screen buffer, the characters are written up to the end of the console screen buffer. The attribute values at the positions written are not changed. This function uses either Unicode characters or 8-bit characters from the console's current code page. The console's code page defaults initially to the system's OEM code page. To change the console's code page, use the SetConsoleCP() or SetConsoleOutputCP() functions, or use the chcp or mode con cp select= commands. For Windows Me/98/95:   FillConsoleOutputCharacterW() is supported by the Microsoft Layer for Unicode. To use this, you must add certain files to your application, as outlined in Microsoft Layer for Unicode on Windows Me/98/95 Systems.

 

Table 7

 

FillConsoleOutputAttribute()

 

Information

Description

The function

FillConsoleOutputAttribute().

The use

Sets the character attributes for a specified number of character cells, beginning at the specified coordinates in a screen buffer.

The prototype

BOOL FillConsoleOutputAttribute(

  HANDLE hConsoleOutput,

  WORD wAttribute,

  DWORD nLength,

  COORD dwWriteCoord,

  LPDWORD lpNumberOfAttrsWritten);

Example

-

The parameters

hConsoleOutput - [in] Handle to a console screen buffer. The handle must have the GENERIC_WRITE access right.

wAttribute - [in] Attributes to use when writing to the console screen buffer.

nLength - [in] Number of character cells to be set to the specified color attributes.

dwWriteCoord - [in] A COORD structure that specifies the console screen buffer coordinates of the first cell whose attributes are to be set.

lpNumberOfAttrsWritten - [out] Pointer to a variable that receives the number of character cells whose attributes were actually set.

The return value

If the function succeeds, the return value is nonzero. If the function fails, the return value is zero. To get extended error information, call GetLastError().

The header file

<windows.h>

Remarks

If the number of character cells whose attributes are to be set extends beyond the end of the specified row in the console screen buffer, the cells of the next row are set. If the number of cells to write to extends beyond the end of the console screen buffer, the cells are written up to the end of the console screen buffer.

The character values at the positions written to are not changed.

 

Table 8

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Further reading and digging:

 

  1. Microsoft Visual C++, online MSDN.
  2. For Multibytes, Unicode characters and Localization please refer to Locale, wide characters & Unicode (Story) and Windows users & groups programming tutorials (Implementation).
  3. Structure, enum, union and typedef story can be found C/C++ struct, enum, union & typedef.
  4. Check the best selling C / C++ and Windows books at Amazon.com.

 

 

 

 

 

 

 

 

|< Windows Process & Threads: C Run-Time 4 | Main | Windows Process & Threads: C Run-Time 6 >| Site Index | Download |