Click here to Skip to main content
15,861,172 members
Articles / DevOps / Testing
Tip/Trick

Console Output, General For All Executables

Rate me:
Please Sign up or sign in to vote.
5.00/5 (7 votes)
19 Jul 2016CPOL1 min read 12.1K   238   11   2
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)


Written By
Austria Austria
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionExplanation of the implementation Pin
Wendelius18-Jul-16 1:18
mentorWendelius18-Jul-16 1:18 
AnswerRe: Explanation of the implementation Pin
David A. Gray18-Jul-16 6:00
David A. Gray18-Jul-16 6:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.