Click here to Skip to main content
15,867,835 members
Articles / Programming Languages / C++
Article

IPCTrace

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
6 Nov 20012 min read 66.6K   842   22   5
IPCTrace - An inter-process tracing utility

Sample Image - IPCTrace.gif

Introduction

In the last few months I coded a lot of global hooks and out-of-process COM server related stuff. Global hooks are hard to debug because they're normally called in the context of the debugger process too. You can't use message boxes, etc. to report some action because this may cause your system to hang permanently under certain circumstances. There are tricks to resolve these problems (MSDN: Q179905), but I figured out problems under Windows 9x/Me. So I wrote my own inter-process tracer.

How it works

I tried to keep this tool as simple and easy to use as possible. I use the WM_COPYDATA for the IPC because this seemed to be the simplest (and fastest) way to fit my needs.

How to integrate

In order to use this IPC-tracer simply build the IPCTrace project (there are english and german resources included) and launch the application.

You should make a release build because you probably won't change any code of that project. To trace messages out of your application to the IPCTrace window I provided a macro called IPCTRACE. The definition of this macro can be found in the file IPCTraceMacros.h

#if (defined _DEBUG || defined _IPCRELEASETRACE)
#if (defined _DEBUG || defined _IPCRELEASETRACE)
#define IPCTRACE(message) \
{ \
    HWND hReceiver_CC976675_9794_4F03_B41C_FFB011BB8B8E = ::FindWindow(NULL, _T("IPCTrace")); \
    if (hReceiver_CC976675_9794_4F03_B41C_FFB011BB8B8E) \
    { \
        TCHAR szMessage[4096] = {'\0'}; \
        _stprintf(szMessage, _T("{P:%04d T:%04d} | "), ::GetCurrentProcessId(), ::GetCurrentThreadId()); \
        _tcscat(szMessage, message); \
        COPYDATASTRUCT cds; \
        ZeroMemory(&cds, sizeof(COPYDATASTRUCT)); \
        cds.dwData = 0x00007a69; \
        cds.cbData = _tcslen(szMessage) + sizeof(TCHAR); \
        cds.lpData = szMessage; \
        ::SendMessage(hReceiver_CC976675_9794_4F03_B41C_FFB011BB8B8E, WM_COPYDATA, NULL, (LPARAM) &cds); \
    } \
}
#else // defined _DEBUG || defined _IPCRELEASETRACE
#define IPCTRACE (void(0))
#endif // !_DEBUG

As you can see it is very simple. To use this macro include the IPCTraceMacros.h header and call the macro like this:

void CAnyClass::AnyFunc(...)
{
  IPCTRACE(_T("Trace this text..."))
}

An additional feature is that you can enable tracing in your release builds too! As you see in the code sample above you can define _IPCRELEASETRACE at the preprocessor definitions of your release build settings in order to make the macro work.

This updated version traces the corresponding process and thread ID to the output window.

In order to test this I supplied a simple client application called IPCTraceClient which is included in the package too. Try it and you'll see how it works...

If you need help with this or have suggestions how to improve it or state bugs feel free to drop me an email. Updated versions may be found at http://www.nitrobit.com/ or http://www.codecommunity.com/.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


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

Comments and Discussions

 
Questionnice, but what about windows service ? Pin
MGutmann16-Aug-05 22:41
MGutmann16-Aug-05 22:41 
QuestionTRACE? Pin
Thomas Freudenberg7-Nov-01 22:26
Thomas Freudenberg7-Nov-01 22:26 
AnswerRe: TRACE? Pin
Rick York10-Nov-01 11:08
mveRick York10-Nov-01 11:08 
AnswerRe: TRACE? Pin
#realJSOP31-Jul-02 2:51
mve#realJSOP31-Jul-02 2:51 
Because you may need to debug two or more programs at once. I wrote a similar program that provides a separate tab for each program that communicates with it. I can watch up to 50 programs at once (if they are coded to be "aware" of my monitor app).

In my case, I had three apps running, one of which I wanted to debug, but could only run if both of the other apps were running. These other apps were responsible for performing certain actions upon which the third app would react. In order to verify that the other two apps were indeed doing the things that needed to be done, I sent messages from them to my monitor app as I debugged the 3rd app.

------- signature starts

"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

Please review the Legal Disclaimer in my bio.

------- signature ends
GeneralRe: TRACE? Pin
Thomas Freudenberg31-Jul-02 2:57
Thomas Freudenberg31-Jul-02 2:57 

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.