How can I get SetConsoleTextAttribute to work with my Console? I tried everything. First I redirected the cout and cin buffers so that my console can have input and output. But now it doesn't have Colour :S

I used:

HANDLE OutputHandle = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(OutputHandle, 10);

I don't think GetStdHandle is pointing to my console. Is there a way to redirect that? Or redirect all input/output to my console?

Currently I'm using this for redirecting and it works really well! Except for the colour part.

std::streambuf *CinBuffer, *CoutBuffer, *CerrBuffer;  //Backup the older buffers.
std::fstream ConsoleInput, ConsoleOutput, ConsoleError;  //My Buffers.

bool Debug::DebugConsole()
        ConsoleActive = AllocConsole();
        CinBuffer = std::cin.rdbuf();
        CoutBuffer = std::cout.rdbuf();
        CerrBuffer = std::cerr.rdbuf();"CONIN$", std::ios::in);"CONOUT$", std::ios::out);"CONOUT$", std::ios::out);

    return ConsoleActive;

Are you running in a seperate process?
Are you familiar with multiple processes?
Are you familiar with Interprocess communication? (It's really really fun!)

A Windows process can only be tied to one console at a time. So... create a new process. From that process, call FreeConsole. The console WON'T be destroyed. Then call AllocConsole.

I haven't researched too deeply, but if you want some backup I'll be happy to help.


You're right. I didn't do AttachConsole(GetCurrentProcessId()).

When I do that it works but then it'll make my program unusable until the console returns. I'll look into the interprocess communications.

This question has already been answered. Start a new discussion instead.