65.9K
CodeProject is changing. Read more.
Home

Console Output, General For All Executables

starIconstarIconstarIconstarIconstarIcon

5.00/5 (7 votes)

Jul 18, 2016

CPOL

1 min read

viewsIcon

12537

downloadIcon

241

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:

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

and use it like this:

    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:

        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.