Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The following class will give you an idea of what you will need to do. On construction, a CConsole attempts to use AllocConsole to create a new console for the process.</p> <p>If AllocConsole fails, nothing is changed -- a console already exists and the class assumes that C-Runtime handles have already been set up somewhere else.</p> <p>If AllocConsole succeeds, the objects currently associated with stdout and stdin are saved, then these are replaced with ones created for the new console. Now C-Runtime output functions (like printf) will output to the new console. cout and cin will also use the new console.</p> <p>The RemoveMenu call will prevent a user from closing the console window, terminating the process. It is not necessary beyond this function.</p> <p>When a CConsole is destroyed (and AllocConsole was successful), stdout and stdin are restored, then the console is closed with a call to FreeConsole.</p> <p>I find this class to be inconvenient when you want your console to persist beyond the function which created it -- you need to get a new CConsole and keep track of its pointer until you close it with a delete. But its implementation lays out the steps you will need to take in your own project. I have never tried this with a dll, but I don't see any reason for this to pose a problem.</p> <p>Console.h:</p> <pre><code>#pragma once #include &lt;stdio.h&gt; class CConsole { FILE m_OldStdin, m_OldStdout; bool m_OwnConsole; public: CConsole(); ~CConsole(); }; </code></pre> <p>Console.cpp:</p> <pre><code>#include &lt;windows.h&gt; #include &lt;conio.h&gt; #include &lt;FCNTL.H&gt; #include &lt;io.h&gt; #include "Console.h" static BOOL WINAPI MyConsoleCtrlHandler(DWORD dwCtrlEvent) { return dwCtrlEvent == CTRL_C_EVENT;} CConsole::CConsole() : m_OwnConsole(false) { if(!AllocConsole()) return; SetConsoleCtrlHandler(MyConsoleCtrlHandler, TRUE); RemoveMenu(GetSystemMenu(GetConsoleWindow(), FALSE), SC_CLOSE, MF_BYCOMMAND); const int in = _open_osfhandle(INT_PTR(GetStdHandle(STD_INPUT_HANDLE)), _O_TEXT); const int out = _open_osfhandle(INT_PTR(GetStdHandle(STD_OUTPUT_HANDLE)), _O_TEXT); m_OldStdin = *stdin; m_OldStdout = *stdout; *stdin = *_fdopen(in, "r"); *stdout = *_fdopen(out, "w"); m_OwnConsole = true; } CConsole::~CConsole() { if(m_OwnConsole) { fclose(stdout); fclose(stdin); *stdout = m_OldStdout; *stdin = m_OldStdin; SetConsoleCtrlHandler(MyConsoleCtrlHandler, FALSE); FreeConsole(); } } </code></pre> <p>Enjoy!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload