0

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;
}

Edited by triumphost

2
Contributors
2
Replies
4
Views
5 Years
Discussion Span
Last Post by triumphost
0

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.

Votes + Comments
Helpful!
0

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.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.