the prototype function is:

BOOL WINAPI WriteConsoleOutputAttribute(
  _In_   HANDLE hConsoleOutput,
  _In_   const WORD *lpAttribute,
  _In_   DWORD nLength,
  _In_   COORD dwWriteCoord,
  _Out_  LPDWORD lpNumberOfAttrsWritten
);

i'm trying use it for the 1st time... what means:
"lpNumberOfAttrsWritten [out]

A pointer to a variable that receives the number of attributes actually written to the console screen buffer."

i'm trying use it but i don't understand what means:(
where i put the text?
can anyone explain to me?

You need to read the remarks for that function:

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 last parameter to that function is a pointer to a DWORD object that is created in your program.

so i just need to use:

&varname

right?

Yes

"Copies a number of character attributes to consecutive cells of a console screen buffer, beginning at a specified location."
so the WriteConsoleOutputAttribute() just change the rectangule attributes and then i use WriteConsoleOutput() on that rectangule and print the characters, right?
(i'm a little confuse)

Yes, you could do that, but that function is used to change the color of existing text. If you want to write new text in a specific color then call SetTextColor()

Moderator: Please change the thread title to: "how use WriteConsoleOutputAttribute() and WriteConsoleOutput()?"

can you give a example of WriteConsoleOutput()?

BOOL WINAPI WriteConsoleOutput(
  _In_     HANDLE hConsoleOutput,
  _In_     const CHAR_INFO *lpBuffer,
  _In_     COORD dwBufferSize,
  _In_     COORD dwBufferCoord,
  _Inout_  PSMALL_RECT lpWriteRegion
);




typedef struct _CHAR_INFO {
  union {
    WCHAR UnicodeChar;
    CHAR  AsciiChar;
  } Char;
  WORD  Attributes;
} CHAR_INFO, *PCHAR_INFO;

i don't understand where i put the string :(
can you explain better, please?

The function is used to write a 2 dimensional array of bytes to the screen, so the second parameter is a 2d array of structures.

Example program is given here. This example reads the text that's on the screen then changes the coordinates so that the text will be written in a different position on the screen. If you want to write all new text you don't have to read from the screen but instead create a new array of structures and fill them in with the text/attribute info

HANDLE hout=GetStdHandle(STD_OUTPUT_HANDLE);
    char text[]="hello world";
    SHORT stringlen=strlen("hello world");
    CHAR_INFO *consoletext= new CHAR_INFO[stringlen];
    COORD a={1,stringlen}, b={0,0};
    SMALL_RECT c={0,0,1,1};

    int i=0;
    for (i=0; i<stringlen;i++)
    {
        text[i].AsciiChar=(int) text[i];
        text[i].Attributes=FOREGROUND_BLUE|BACKGROUND_RED;
    }

    WriteConsoleOutput(hout,consoletext,a,b,&c);

but i get 2 errors:
"error: request for member 'AsciiChar' in 'text[i]', which is of non-class type 'char'"
and
"error: request for member 'Attributes' in 'text[i]', which is of non-class type 'char'"
can you advice me?

lines 11 and 12: text is an array of char, not an array of structures, it should be consoletext.

Did you correct lines 11 and 12?

sorry you have right:

HANDLE hout=GetStdHandle(STD_OUTPUT_HANDLE);
    char text[]={'h','e','l','l','o',' ','w','o','r','l','d'};
    SHORT stringlen=strlen("hello world");
    CHAR_INFO *consoletext= new CHAR_INFO[stringlen];
    COORD a={1,stringlen}, b={0,0};
    SMALL_RECT c={0,0,1,1};

    int i=0;
    for (i=0; i<stringlen;i++)
    {
        consoletext[i].Char.AsciiChar  =text[i];
        consoletext[i].Attributes=FOREGROUND_BLUE|BACKGROUND_RED;
    }

    WriteConsoleOutput(hout,consoletext,a,b,&c);

but i have more errors: the text only show me 2 letters on vertical.
so i belive that my SMALL_RECT and maybe the 2 COORD are wrong. can you explain to me, please?

now works.. thanks
i did a mistake between x,y:

HANDLE hout=GetStdHandle(STD_OUTPUT_HANDLE);
    char text[]={'h','e','l','l','o',' ','w','o','r','l','d'};
    SHORT stringlen=strlen("hello world");
    CHAR_INFO *consoletext= new CHAR_INFO[stringlen];
    COORD a={stringlen,1}, b={0,0};
    SMALL_RECT c={0,0,stringlen,1};

    int i=0;
    for (i=0; i<=stringlen;i++)
    {
        consoletext[i].Char.AsciiChar  =text[i];
        consoletext[i].Attributes=FOREGROUND_GREEN;

    }

    WriteConsoleOutput(hout,consoletext,a,b,&c);

thanks for all

now i'm add it to my code:

Blink *p = static_cast<Blink *>(params);
SHORT tlen = (SHORT)p->Text.length();
SHORT x=(SHORT)p->x, y=(SHORT)p->y;
HANDLE hout=GetStdHandle(STD_OUTPUT_HANDLE);
char *text=(char*)p->Text.c_str();
SHORT stringlen=strlen(text);
CHAR_INFO *ConsoleText= new CHAR_INFO[tlen];
CHAR_INFO *EmptyConsoleText= new CHAR_INFO[tlen];
COORD a={tlen,1}, b={0,0};
SMALL_RECT c={x,y,x+tlen,y+(SHORT)1};

int i=0;
for (i=0; i<=stringlen;i++)
{
    ConsoleText[i].Char.AsciiChar  =text[i];
    ConsoleText[i].Attributes=FOREGROUND_GREEN;
    ConsoleText[i].Char.AsciiChar  =' ';
}

output errors :(
on SMALL_RECT i get these warnings:

1 - "warning: narrowing conversion of '(((int)x) + ((int)tlen))' from 'int' to 'SHORT {aka short int}' inside { } [-Wnarrowing]"

2 - "warning: narrowing conversion of '(((int)y) + 1)' from 'int' to 'SHORT {aka short int}' inside { } [-Wnarrowing]"

why these warnings if the x and y are SHORT?

another problem: the output give me strange characters, can you tell me why?

This works

#include <Windows.h>
#include <iostream>
#pragma warning(disable: 4996)

int main()
{
    HANDLE hout=GetStdHandle(STD_OUTPUT_HANDLE);
    char text[]="Hello World";
    CHAR_INFO *consoletext= new CHAR_INFO[sizeof(text)-1];
    COORD a={sizeof(text)-1,1}, b={0,0};
    SMALL_RECT c={0,0,80,24};

    int i=0;
    for (i=0; i<sizeof(text)-1;i++)
    {
        consoletext[i].Char.AsciiChar  =text[i];
        consoletext[i].Attributes=FOREGROUND_RED|BACKGROUND_RED;
    }

    int success = WriteConsoleOutput(hout,consoletext,a,b,&c);
    if( !success )
    {
        std::cout << "WriteConsoleOutput failed\n";
    }
    delete[] consoletext;
    std::cin.get();
}

yes again i did a mistake:

Blink *p = static_cast<Blink *>(params);
    SHORT tlen = (SHORT)p->Text.length();
    SHORT x=p->x, y=p->y;
    HANDLE hout=GetStdHandle(STD_OUTPUT_HANDLE);
    const char *text=(char*)p->Text.c_str();
    SHORT stringlen=strlen(text);
    CHAR_INFO *ConsoleText= new CHAR_INFO[tlen];
    CHAR_INFO *EmptyConsoleText= new CHAR_INFO[tlen];
    COORD a={tlen,1}, b={0,0};
    SMALL_RECT c={x,y,x+tlen,y+(SHORT)1};

    int i=0;
    for (i=0; i<stringlen;i++)
    {
        ConsoleText[i].Char.AsciiChar  =text[i];
        ConsoleText[i].Attributes=FOREGROUND_GREEN;
        EmptyConsoleText[i].Char.AsciiChar  =' ';
    }

i have 1 question: when the EmptyConsoleText is printed, the 3st characters are showed with a backcolor, why?

finally i resolve it:

Blink *p = static_cast<Blink *>(params);
    SHORT tlen = (SHORT)p->Text.length();
    SHORT x=p->x, y=p->y;
    HANDLE hout=GetStdHandle(STD_OUTPUT_HANDLE);
    const char *text=(char*)p->Text.c_str();
    SHORT stringlen=strlen(text);
    CHAR_INFO *ConsoleText= new CHAR_INFO[tlen];
    CHAR_INFO *EmptyConsoleText= new CHAR_INFO[tlen];
    COORD a={tlen,1}, b={0,0};
    SMALL_RECT c={x,y,x+tlen,y+(SHORT)1};

    int i=0;
    for (i=0; i<stringlen;i++)
    {
        ConsoleText[i].Char.AsciiChar  =text[i];
        ConsoleText[i].Attributes=FOREGROUND_GREEN;
        EmptyConsoleText[i].Char.AsciiChar  =' ';
        EmptyConsoleText[i].Attributes=FOREGROUND_GREEN;
    }

    while (true)
    {
        WriteConsoleOutput(hout,ConsoleText,a,b,&c);
        Sleep(500);
        WriteConsoleOutput(hout,EmptyConsoleText,a,b,&c);
        Sleep(500);
    }

but i get 2 warnings on:

SMALL_RECT c={x,y,x+tlen,y+(SHORT)1};

1 - "warning: narrowing conversion of '(((int)x) + ((int)tlen))' from 'int' to 'SHORT {aka short int}' inside { } [-Wnarrowing]";

2 - "warning: narrowing conversion of '(((int)y) + 1)' from 'int' to 'SHORT {aka short int}' inside { } [-Wnarrowing]".

but the 'p' pointer structure are in SHORT's, so why the warnings?

remove the typecast -- it's not needed.

Since Text is std::string, why are you calling strlen() instead of using Text.Length()??? Line 5 is completely unnessary, just extra usless baggaged. Line 6 can be rewritten SHORT stringlen=Text.Length();, and you really don't need that either. Just use Text.Length() wherever you have stringlen.

the c++ is case sensitive, so we must use:

Text.length();

sorry i continue with warnings on that line

resolved:

SMALL_RECT c={x, y,SHORT(x+tlen),SHORT(y+1)};

seems the '+' operator put them to 'int', that's why i must use SHORT like a function(the casting don't works)
thanks for all

Moderator: Please change the thread title to: "how use WriteConsoleOutputAttribute() and WriteConsoleOutput()?"

Next time use "Flag Bad Post" to request such things.

thanks for correct me... sorry if i fail in some things of the forum.. isn't by bad, but i want learn

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.