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()
{
    if(!ConsoleActive)
    {
        ConsoleActive = AllocConsole();
        CinBuffer = std::cin.rdbuf();
        CoutBuffer = std::cout.rdbuf();
        CerrBuffer = std::cerr.rdbuf();
        ConsoleInput.open("CONIN$", std::ios::in);
        ConsoleOutput.open("CONOUT$", std::ios::out);
        ConsoleError.open("CONOUT$", std::ios::out);
        std::cin.rdbuf(ConsoleInput.rdbuf());
        std::cout.rdbuf(ConsoleOutput.rdbuf());
        std::cerr.rdbuf(ConsoleError.rdbuf());
    }

    return ConsoleActive;
}

Ref: http://msdn.microsoft.com/en-us/library/windows/desktop/ms681944(v=vs.85).aspx
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.

commented: Helpful! +5

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.

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.