Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / DevOps / testing

Console Output, General For All Executables

5.00/5 (7 votes)
19 Jul 2016CPOL1 min read 12.5K   240  
Console output, general applicable to all executables on a Windows system

Introduction

In developing a solution, I repeatedly had the problem how to get a debug-output to a console. Finally, I found this general way, consisting of a Console-server and two .DLL's. One .DLL for native C++ standard and another for managed /clr:pure.

Background

Console-server provides a Named-Pipe in read-only mode. The Native-DLL connects to that Named-Pipe and writes messages to it. The managed /clr:pure DLL wraps the Native-DLL to the MS-managed world.

The DLLs can be used in any executable. I used it in a WIN32 application, in WindowsForms .NET application, in other DLLs, in an application with multiple threads.

Using the Code

To use the Native-DLL, write the following declaration:

C++
extern "C" {
int __declspec(dllimport) PipeWrite(const char msg[]);
}

and use it like this:

C++
case WM_CREATE:
    PipeWrite("A_Dialog connect");
    ...

case WM_DESTROY:
    PipeWrite("A_Dialog disconnect");
    ...

For managed code, there is no declaration needed, if your project knows the Managed-DLL as a resource.

You use it like this in the constructor and destructor of a component and anywhere else:

C++
DebugControl(void)
{
    InitializeComponent();

    Pipe::write("I'm connected");
}

~DebugControl()
{
    if (components)
    {
        delete components;
    }
    Pipe::write("bye, I'm going q:-D");
}

Source Code

The source consists of three files:

  • PipeSrvRead.cpp
  • PipeClntNativeDll.cpp
  • PipeClntClrpureDll.cpp

Important notice: The choosen pipe-type PIPE_TYPE_MESSAGE works in ANSI-mode only. Don't compile the Server and the Native-Client with Unicode!

Feedback

If you have any questions or a hint, feel free to write a comment below.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)