StrokeSeg 1.0.0
Loading...
Searching...
No Matches
consoleAllocator.cpp
1#include "deps.h"
2
3int allocateConsole(wchar_t const * consoleTitle)
4{
5 int iRes = -1; // Default return value indicating failure
6
7 if (AttachConsole(ATTACH_PARENT_PROCESS))
8 {
9 iRes = 1; // Successfully attached to the parent console
10 }
11 else if (AllocConsole())
12 {
13 iRes = 0; // Successfully allocated a new console
14 SetConsoleTitle(consoleTitle);
15 }
16
17 if (iRes != -1)
18 {
19 FILE* pCout = nullptr;
20 FILE* pCin = nullptr;
21 FILE* pCerr = nullptr;
22
23 freopen_s(&pCout, "CONOUT$", "w", stdout);
24 freopen_s(&pCin, "CONIN$", "r", stdin);
25 freopen_s(&pCerr, "CONOUT$", "w", stderr);
26
27 // Synchronise C++ streams with C stdio
28 std::ios_base::sync_with_stdio(true);
29 std::cout.clear();
30 std::cin.clear();
31 std::cerr.clear();
32 }
33 else
34 {
35 MessageBox(nullptr, TEXT("Could not allocate or attach console!"), TEXT("Error"), MB_OK | MB_ICONERROR);
36 }
37
38 return iRes;
39}