Scanf()/printf() and Juce

#1

I want my Juce application to be able to start in console mode or GUI mode. For this I want to start a simple REPL if the user passes a command line flag like “-console”. This is because the application supports Lua scripting and I want the user to have access to only the Lua console when in pure console mode(the main window is not created)

I am able to pass the command line flag but unable to implement the REPL with std::cin and std::cout as they dont work. How do I do this?

I love Juce for everything else. Thanks!

0 Likes

#2

Are you on Windows? Because on Unix systems this should work out of the box, however on Windows there is no console when starting a GUI application (even if you are not creating any window). You can however create a console at runtime. I’m not working on Windows that often and it’s been a while since I did it, however I think it was done somehow like that:

#include <windows.h>

if (AllocConsole())
{
    freopen("CONOUT$", "w", stdout);
    freopen("CONOUT$", "w", stderr);
}
1 Like

#3

Thanks, it works(yes I am on Windows):

HWND ptr = GetForegroundWindow();
DWORD  u;
GetWindowThreadProcessId(ptr, &u);

if (AllocConsole())
{
    AttachConsole(u);
    freopen("CONOUT$", "w", stdout);
    freopen("CONOUT$", "w", stderr);
    freopen("CONIN$",  "r", stdin);
}

This lets me take I/O from the command line.
Now, I am trying the AttachConsole() part to see if I make the console appear in the same console cmd window when I start my application from the command line. Right now it opens a new console window.

0 Likes