Skip to content

Commit

Permalink
Improve console allocation.
Browse files Browse the repository at this point in the history
  • Loading branch information
dlbuhtig4096 committed Oct 24, 2024
1 parent 28f5c76 commit f1ca6dc
Showing 1 changed file with 20 additions and 11 deletions.
31 changes: 20 additions & 11 deletions tools/nds_firmware_tool/src/console.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
#include <io.h>

#define BUFFER_SIZE 100
HANDLE hConsole;
HANDLE hConsole = NULL;
void OpenConsole()
{
COORD csize;
Expand All @@ -41,27 +41,33 @@ void OpenConsole()
//dont do anything if we're already attached
if (hConsole) return;

#if 0
//attach to an existing console (if we can; this is circuitous because AttachConsole wasnt added until XP)
//remember to abstract this late bound function notion if we end up having to do this anywhere else
bool attached = false;
HMODULE lib = LoadLibrary("kernel32.dll");
if(lib)

// kernel32.dll is always loaded on windows.
HMODULE lib = GetModuleHandleA("kernel32.dll"); // LoadLibrary("kernel32.dll");
if (lib)
{
typedef BOOL (WINAPI *_TAttachConsole)(DWORD dwProcessId);
_TAttachConsole _AttachConsole = (_TAttachConsole)GetProcAddress(lib,"AttachConsole");
if(_AttachConsole)
if (_AttachConsole)
{
if(_AttachConsole(-1))
attached = true;
attached = (bool)_AttachConsole(-1);
}
FreeLibrary(lib);
// You cannot free kernel32.dll
// FreeLibrary(lib);
}

//if we failed to attach, then alloc a new console
if(!attached)
if (!attached)
{
AllocConsole();
}
#else
AttachConsole(-1);
#endif

hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

Expand Down Expand Up @@ -91,9 +97,12 @@ void OpenConsole()
srect.Right = srect.Left + 99;
srect.Bottom = srect.Top + 64;
SetConsoleWindowInfo(GetStdHandle(STD_OUTPUT_HANDLE), TRUE, &srect);
SetConsoleCP(GetACP());
SetConsoleOutputCP(GetACP());
if(attached) printf("\n");

// Use default code page.
// SetConsoleCP(GetACP());
// SetConsoleOutputCP(GetACP());

if (attached) printf("\n");
printf("%s\n",_TITLE);
printf("- compiled: %s %s\n\n",__DATE__,__TIME__);
}
Expand Down

0 comments on commit f1ca6dc

Please sign in to comment.