Categories: Windows

Add a debug console to your windows gui application

Call AllocConsole() at the beginning of your application. Only one console can be allocated for a process.

Next, call WriteConsole to show a text.

void CMyApp::LogDebug(LPCSTR p)
{
 if(m_bAppDebug)
 {
  DWORD d;
  WriteConsoleA(
   GetStdHandle(STD_OUTPUT_HANDLE),
   (CONST VOID *)p,
   strlen(p),
   &d,
   NULL
   );
  WriteConsoleA(
   GetStdHandle(STD_OUTPUT_HANDLE),
   (CONST VOID *)"\r\n",
   2,
   &d,
   NULL
   );

 }
}

I don’t know why but WriteConsole has Anscii and Unicode definition. Is there any differences?
I didn’t do a research for that now.
And at the end of your application, call FreeConsole().

You can also call WriteFile instead of WriteConsole, in this case the function can be used for both the console and a file.

In both cases, there is a chance standard handle is not as what you expected because the calling process can inherit or put a security on it. For example, if the application using above code was run from cygwin, the GetStdHandle() would fail.

admin

Share
Published by
admin

Recent Posts

Obtain global IP in bash

In current Internet environment, every PC is assigned a private IP. If global IP is…

4 years ago

Create log file in the directory of the script in Python

__file__ is the script file itself. From python3.9 above, it is an absolute path, otherwise…

4 years ago

RichTextBox’s font changes automatically in C#

You need to clear these two flags IMF_DUALFONT and IMF_AUTOFONT to keep font unchanged.

4 years ago

Test post to check image URL

This is an image inserted by wordpress 'Add Media'

4 years ago

msbuild says ‘target does not exist’

I don't know what is going wrong but... How to fix it Open the solution…

4 years ago

Creating a break point that hits on calling win32 api

Create a break point that hits when CreateProcess was called Enter a Function Breakpoint Enter…

5 years ago