Process returned -1073741819 (0xC0000005) after writing color information

-2
#include <windows.h>
#include <stdint.h>

int main() {
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    COORD pos = {3, 6};
    SetConsoleCursorPosition(hConsole, pos);
    WriteConsole(hConsole, "01234", 5, NULL, NULL);
    pos = {3, 24};
    SetConsoleCursorPosition(hConsole, pos);
    WriteConsole(hConsole, "test", 4, NULL, NULL);
    const WORD a[2] = {0x49, 0xA6};
    WriteConsoleOutputAttribute(hConsole, a, 2, {3, 6}, NULL);
    WriteConsoleOutputAttribute(hConsole, a, 2, {5, 6}, NULL);
    return 0;
}

This kind of code will return

enter image description here

The first color write at location {3, 6} will execute, but then somehow takes time to then return an error code, preventing the commands afterwards from executing, therefore it doesn't get written at {5, 6}.

c++
winapi
asked on Stack Overflow Dec 21, 2019 by Piotr Grochowski • edited Dec 22, 2019 by E_net4 the disappointed Ferris

1 Answer

0

Credits to churill for resolving this.

"Now that's the same as passing NULL as last parameter, becuase b is 0. An LPWORD is a WORD *, so you need WORD b; and pass &b."

Except it would be DWORD, not WORD.

This is experimental code that outputs various shades of colors with the dithered characters. The window width should be 80 characters.

#include <windows.h>
#include <stdint.h>

int main() {
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    DWORD b = 0;
    char shades[1280];
    for (int i=0; i<1280; i+=80){
        for (int k=0; k<16; k++){
            shades[i+k]=32;
        }
        for (int k=16; k<32; k++){
            shades[i+k]=176;
        }
        for (int k=32; k<48; k++){
            shades[i+k]=177;
        }
        for (int k=48; k<64; k++){
            shades[i+k]=178;
        }
        for (int k=64; k<80; k++){
            shades[i+k]=219;
        }
    }
    WriteConsole(hConsole, shades, 1280, NULL, NULL);
    WORD colors[1280];
    for (int i=0; i<1280; i++){
        colors[i] = (i%16)+((i/80)*16);
    }
    WriteConsoleOutputAttribute(hConsole, (const WORD*)(colors), 1280, {0, 0}, &b);
    return 0;
}

enter image description here

answered on Stack Overflow Dec 21, 2019 by Piotr Grochowski

User contributions licensed under CC BY-SA 3.0