The Windows threads operation and console 1 C program example

 

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows Xp Pro SP2

Target platform: none, just for learning and fun

Header file: Standard and Windows

Additional library: Windows Platform SDK

Additional project setting: Set project to be compiled as C

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C Code (/TC)

Other info: non-CLR or unmanaged.

To do: The Windows threads operation and console 1

To show: More on Windows threads and processes operation

 

 

 

 

#include <windows.h>

#include <stdio.h>

 

CONSOLE_SCREEN_BUFFER_INFO csbi;

 

int main(void)

{

HANDLE hStdOut;

LPTSTR lpszString = L"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;

}

 

Output example:

 

GetStdHandle() is OK.

GetConsoleScreenBufferInfo() is OK.

WriteConsoleOutputCharacter() is OK.

WriteConsoleOutputAttribute() is OK.

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index