Create Console for Logging in Win32 Application

2/10/2017来源:ASP.NET技巧人气:1164

1 Create Console for Logging in Win32 application

Win32 Application is created without Console, which is quite useful for debuging.

1.1 AllocConsole && Redirect stdout

void InitConsole() { ASSERT_TRUE(AllocConsole()); auto out = GetStdHandle(STD_OUTPUT_HANDLE); auto message = "Console Allocated\n"; WriteConsole(out, message, (DWord)strlen(message), nullptr, nullptr); // redirect stream stdout mStdout = freopen("CONOUT$", "w", stdout); ASSERT_NOT_NULL(mStdout); PRintf("stdout redirected\n"); } void DestroyConsole() { SAFE_CLOSE(mStdout); ASSERT_TRUE(FreeConsole()); }

1.2 TODO: REPL

1) Support REPL in Console

See Also: AllocConsole, FreeConsole, GetStdHandle