Click here to Skip to main content
14,425,800 members

Console Output, General For All Executables

Rate this:
5.00 (7 votes)
Please Sign up or sign in to vote.
5.00 (7 votes)
19 Jul 2016CPOL
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.

License

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

Share

About the Author

No Biography provided

Comments and Discussions

 
QuestionExplanation of the implementation Pin
Wendelius18-Jul-16 2:18
mveWendelius18-Jul-16 2:18 
AnswerRe: Explanation of the implementation Pin
David A. Gray18-Jul-16 7:00
MemberDavid A. Gray18-Jul-16 7: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.

Tip/Trick
Posted 18 Jul 2016

Stats

9.2K views
227 downloads
11 bookmarked