win32: attach to existing console (from commandprompt) if it exists instead of always creating a new one. still needs a bit more work
This commit is contained in:
parent
29ac31a5a4
commit
7b4804b878
|
@ -31,7 +31,7 @@
|
||||||
///////////////////////////////////////////////////////////////// Console
|
///////////////////////////////////////////////////////////////// Console
|
||||||
#if !defined(PUBLIC_RELEASE) || defined(DEVELOPER)
|
#if !defined(PUBLIC_RELEASE) || defined(DEVELOPER)
|
||||||
#define BUFFER_SIZE 100
|
#define BUFFER_SIZE 100
|
||||||
HANDLE hConsole;
|
HANDLE hConsole = NULL;
|
||||||
void printlog(const char *fmt, ...);
|
void printlog(const char *fmt, ...);
|
||||||
|
|
||||||
void OpenConsole()
|
void OpenConsole()
|
||||||
|
@ -41,8 +41,41 @@ void OpenConsole()
|
||||||
SMALL_RECT srect;
|
SMALL_RECT srect;
|
||||||
char buf[256];
|
char buf[256];
|
||||||
|
|
||||||
|
//dont do anything if we're already attached
|
||||||
if (hConsole) return;
|
if (hConsole) return;
|
||||||
|
|
||||||
|
//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)
|
||||||
|
{
|
||||||
|
typedef BOOL (WINAPI *_TAttachConsole)(DWORD dwProcessId);
|
||||||
|
_TAttachConsole _AttachConsole = (_TAttachConsole)GetProcAddress(lib,"AttachConsole");
|
||||||
|
if(_AttachConsole)
|
||||||
|
{
|
||||||
|
if(_AttachConsole(-1))
|
||||||
|
attached = true;
|
||||||
|
}
|
||||||
|
FreeLibrary(lib);
|
||||||
|
}
|
||||||
|
|
||||||
|
//if we failed to attach, then alloc a new console
|
||||||
|
if(!attached)
|
||||||
|
{
|
||||||
AllocConsole();
|
AllocConsole();
|
||||||
|
}
|
||||||
|
|
||||||
|
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
|
||||||
|
|
||||||
|
//redirect stdio
|
||||||
|
long lStdHandle = (long)hConsole;
|
||||||
|
int hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
|
||||||
|
FILE *fp = _fdopen( hConHandle, "w" );
|
||||||
|
*stdout = *fp;
|
||||||
|
//and stderr
|
||||||
|
*stderr = *fp;
|
||||||
|
|
||||||
memset(buf,0,256);
|
memset(buf,0,256);
|
||||||
sprintf(buf,"%s OUTPUT", DESMUME_NAME_AND_VERSION);
|
sprintf(buf,"%s OUTPUT", DESMUME_NAME_AND_VERSION);
|
||||||
SetConsoleTitle(TEXT(buf));
|
SetConsoleTitle(TEXT(buf));
|
||||||
|
@ -54,19 +87,13 @@ void OpenConsole()
|
||||||
srect.Right = srect.Left + 99;
|
srect.Right = srect.Left + 99;
|
||||||
srect.Bottom = srect.Top + 64;
|
srect.Bottom = srect.Top + 64;
|
||||||
SetConsoleWindowInfo(GetStdHandle(STD_OUTPUT_HANDLE), TRUE, &srect);
|
SetConsoleWindowInfo(GetStdHandle(STD_OUTPUT_HANDLE), TRUE, &srect);
|
||||||
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
|
|
||||||
SetConsoleCP(GetACP());
|
SetConsoleCP(GetACP());
|
||||||
SetConsoleOutputCP(GetACP());
|
SetConsoleOutputCP(GetACP());
|
||||||
|
if(attached) printlog("\n");
|
||||||
printlog("%s\n",DESMUME_NAME_AND_VERSION);
|
printlog("%s\n",DESMUME_NAME_AND_VERSION);
|
||||||
printlog("- compiled: %s %s\n\n",__DATE__,__TIME__);
|
printlog("- compiled: %s %s\n\n",__DATE__,__TIME__);
|
||||||
|
|
||||||
//redirect stdio
|
|
||||||
long lStdHandle = (long)GetStdHandle(STD_OUTPUT_HANDLE);
|
|
||||||
int hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
|
|
||||||
FILE *fp = _fdopen( hConHandle, "w" );
|
|
||||||
*stdout = *fp;
|
|
||||||
//and stderr
|
|
||||||
*stderr = *fp;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void CloseConsole() {
|
void CloseConsole() {
|
||||||
|
|
Loading…
Reference in New Issue