CreateFile() and CONOUT$

This is a discussion on CreateFile() and CONOUT$ within the Windows Programming forums, part of the Platform Specific Boards category; I could've sworn I've done this before, but I can't get it to work for the life of me this ...

  1. #1
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709

    CreateFile() and CONOUT$

    I could've sworn I've done this before, but I can't get it to work for the life of me this time round.

    Code:
    bool ConWnd::Write( const char* mesg )
    {
        if ( !consoleReady )
        {
            ELogging::S().LogAction( "Console not ready, allocating..." );
            if ( !AllocConsole() )
            {
                ELogging::S().ActionFailed( "AllocConsole() failed" );
                EReportBox( "ConWnd::Write: AllocConsole() failed" );
                return false;
            }
    
            consoleHandle = CreateFile( "CONOUT$", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0 );
            if ( consoleHandle == INVALID_HANDLE_VALUE )
            {
                ELogging::S().ActionFailed( "CreateFile() failed" );
                EReportBox( "ConWnd::Write: CreateFile() failed" );
                return false;
            }
    
            ELogging::S().ActionDone();
            consoleReady = true;
        }
    
        //if ( !WriteFile(consoleHandle, mesg, static_cast<DWORD> (strlen(mesg)), 0, 0) )
        //{
        //    DWORD   eCode = GetLastError();
    
        //    ELogging::S().Log( Va("Writing to console failed (%d)", eCode) );
        //    EReportBox( "ConWnd::Write: Writing failed" );
        //    return false;
        //}
        if ( !SetStdHandle(STD_OUTPUT_HANDLE, consoleHandle) )
        {
            DWORD   eCode = GetLastError();
    
            ELogging::S().Log( Va("Error setting STD_OUTPUT_HANDLE to consoleHandle (%d)", eCode) );
            EReportBox( "ConWnd::Write: Error setting output handle" );
            return false;
        }
    
        std::cout << mesg;
        return true;
    }
    I've gone through all the MSDN docs related to this and I'm *sure* I've done everything right.

    Any takers?
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  2. #2
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Actually nevermind. I really should get working on the in-game console anyway.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21