Results 1 to 9 of 9

Thread: [C++] ConsoleEx - simultaneous in/out

  1. #1
    Senior Member rafi's Avatar
    Join Date
    Sep 2009
    Location
    munich
    Posts
    200

    [C++] ConsoleEx - simultaneous in/out V2

    hey,

    this is my console class for simultaneous input and output designed for windows. originally written for a chat-like program it is extremely useful for every project you could think of!
    some infos:
    - define own callback that gets inputted string
    - usage is easy like printf
    - very fast!

    your callback may access stuff at the same time as your mainthread does.
    the class can only handle its internal thread saveness and is not responsible for you messing up yours!

    example:
    Code:
    #include "ConsoleEx.h"
    
    ConsoleEx *g_pConEx;
    bool g_bRunning;
    
    void cbInput(std::string input)
    {
    	g_pConEx->printf("> %s\n", input.c_str());
    	if (input == "exit")
    		g_bRunning = false;
    }
    
    int WINAPI WinMain( __in HINSTANCE hInstance, __in_opt HINSTANCE hPrevInstance, __in LPSTR lpCmdLine, __in int nShowCmd )
    {
    	g_pConEx = new ConsoleEx(hInstance);
    	g_pConEx->registerInputCallback(cbInput);
    
    	CONSOLEEX_PARAMETERS params = ConsoleEx::GetDefaultParameters();
    	params.cellsX = 60;
    	params.closemenu = true;
    
    	g_bRunning = g_pConEx->create("Test", &params);
    	while (g_bRunning) {
    		Sleep(500);
    		g_pConEx->printf("just a message\n");
    	}
    
    	delete g_pConEx;
    
    	return 0;
    }
    Attached Files Attached Files
    Last edited by rafi; 02-27-2014 at 06:51 PM.

  2. #2
    Super Moderator ACB's Avatar
    Join Date
    Aug 2009
    Posts
    583

    Re: [C++] ConsoleEx - simultaneous in/out

    the result is quite nicely though i must say the code isnt that nice :P

    there is little to no error checking and not really good commented. usually you have 1 comment/5 codelines. also you should use c++ style casting.

    personally i wouldnt make something like this header only there are no advantages in this case plus it slows down compilation time and makes it kinda hard to read.
    also i think you shouldnt use the windows console but implement an own console like window/control which would allow much more customization though ofc a little bit more tedious to write

    btw you dont need to implement printf yourself you can just redirect stdout into a buffer.

    well was much critizism but all in all the result is really nice!

  3. #3
    Senior Member rafi's Avatar
    Join Date
    Sep 2009
    Location
    munich
    Posts
    200
    Version 2!
    completly rewritten with custom console -> no more performance problems with crappy windows console.
    handling of the simultanous input and output also became a lot cleaner because of that

  4. #4
    It is quite nice and I am guessing this is the same console in the gw2 packet logger?

  5. #5
    Senior Member rafi's Avatar
    Join Date
    Sep 2009
    Location
    munich
    Posts
    200
    the packet logger used the old version that used the windows console. new version doesnt have different text colors though

  6. #6
    Junior Member
    Join Date
    Jan 2014
    Posts
    7
    Thanks for the release, i was looking for something like this

  7. #7
    Still rollin' GoldCoin's Avatar
    Join Date
    Jul 2009
    Location
    Neverland
    Posts
    490
    Very useful piece of code, thank you very much for this.
    They say it can't be done? We ain't never not done it yet, have we?

  8. #8
    Thank you for the console, it has been very useful!

    I wanted to upload my modified version which has a static function to generate a logger with one function, of course you wont have as much control this way but I find it nice to quickly make a generic log.

    Also implemented SetWindowPos to set the console to have the topmost flag, can also set its X and Y coordinates on screen.

    Example:

    Code:
    ConsoleEx* console[2];
    
    void init(HMODULE hModule){
    
    	console[0] = ConsoleEx::GenerateLogger(hModule, "[StoC] Gameserver", 40, 20);
    	console[1] = ConsoleEx::GenerateLogger(hModule, "[StoC] Loginserver", 40, 20);
    
    	console[0]->SetPos(HWND_TOPMOST, 0, 0);
    	console[1]->SetPos(HWND_TOPMOST, 350, 0);
    
           // More stuff here...
    }
    
    BOOL WINAPI DllMain(_In_ HMODULE _HDllHandle, _In_ DWORD _Reason, _In_opt_ LPVOID _Reserved){
    	if (_Reason == DLL_PROCESS_ATTACH){
    		DisableThreadLibraryCalls(_HDllHandle);
    		CreateThread(0, 0, (LPTHREAD_START_ROUTINE)init, _HDllHandle, 0, 0);
    	}
    	return TRUE;
    }
    Result:

    Attached Files Attached Files
    Last edited by 4D 1; 05-09-2015 at 09:54 PM.

  9. #9
    Senior Member rafi's Avatar
    Join Date
    Sep 2009
    Location
    munich
    Posts
    200
    Exposing HWND is a good idea, so the user can do more stuff with the console window.

    Note that the code from this thread may be outdated. The ConsoleEx class is now part of hacklib (http://www.gamerevision.com/showthread.php?7568-Hacklib). It does not depend on anything, so you can still just take the source files without linking all of hacklib.

    Your complete example would look like this:

    Code:
    #include "hacklib/Main.h"
    #include "hacklib/ConsoleEx.h"
    
    class MyMain : public hl::Main
    {
    public:
        MyMain() : m_cons{ hl::GetCurrentModule(), hl::GetCurrentModule() }
        {
        }
        bool init() override
        {
            auto params = hl::ConsoleEx::GetDefaultParameters();
            params.cellsX = 40;
            params.cellsYVisible = 20;
            if (!m_cons[0].create("[StoC] Gameserver", &params))
                return false;
            if (!m_cons[1].create("[StoC] Loginserver", &params))
                return false;
            if (!SetWindowPos(m_cons[0].getWindowHandle(), HWND_TOPMOST, 0, 0, NULL, NULL, SWP_NOSIZE))
                return false;
            if (!SetWindowPos(m_cons[1].getWindowHandle(), HWND_TOPMOST, 350, 0, NULL, NULL, SWP_NOSIZE))
                return false;
            // More stuff here...
            return true;
        }
    private:
        hl::ConsoleEx m_cons[2];
    };
    
    hl::StaticInit<MyMain> g_main;

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •