July 21, 2001 tools

TraceHook

July 21, 2001

TraceHook is a member function call tracing delegator hook based on Keith Brown’s most excellent universal delegator. It allows you to write:

    // Get a COM object to wrap
    CComPtr<ICalc>  spCalc;
    spCalc.CoCreateInstance(CLSID_Calc);

    // Wrap it (when _DEBUG is defined only)
    TRACEHOOK(OLESTR("MyCalc"), &spCalc.p);

    // Use it
    spCalc->put_Sum(0);
    spCalc->Add(2);
    spCalc->Add(2);
    long nSum;
    spCalc->get_Sum(&nSum);

and see the following in the debug output (with no changes to the object’s source) :

    MyCalc, ICalc::put_Sum(0)
    MyCalc, ICalc::Add(2)
    MyCalc, ICalc::Add(2)
    MyCalc, ICalc::get_Sum()

Notice that the call stack is now dumped (for duals, either directly or via IDispatch, or any method with automation-compliant data types, thanks to features added by the most excellent Simon Fell! He also added the ability to hook objects created in scripting environments (or VB or Java, too), e.g.

    ' WSH Test script for CalcSvr / TraceHook

    ' Create the implementation of ITraceHookFactory
    set th = CreateObject("UDTraceHookSvr.TraceHook")

    ' Create an object (an <object> tag in HTML would be fine, too)
    set calc = CreateObject("CalcSvr.Calc")

    ' Trace all further calls on calc
    th.ComTrace "MyCalc", calc

    calc.sum = 0
    calc.add 2
    calc.add 2
    msgbox "2+2= " & calc.sum

would yield the following debug output:

    MyCalc, IDispatch::(Invoke) sum(0)
    MyCalc, IDispatch::(Invoke) add(2)
    MyCalc, IDispatch::(Invoke) add(2)
    MyCalc, IDispatch::(Invoke) sum() returned 4

The source, pre-built binaries and client-side headers are available here. The pre-built binaries include Keith’s delegator as well.