Listing 4: The OnCopyData member function of the class QMonitorDlg, the Dialog that is used to create the debug window in the stand-alone application QMonitor.exe

BOOL CQMonitorDlg::OnCopyData(CWnd* pWnd,
                  COPYDATASTRUCT* pCopyDataStruct)
{
    if (pCopyDataStruct->cbData > 0)
    {
        // Format line
        DWORD n = pCopyDataStruct->cbData;
        char* buf = new char[n + 16 + 22];
        if (buf == NULL)
            return FALSE;
        SYSTEMTIME dt;
        GetLocalTime(&dt);
        sprintf(buf, 
            "%02d/%02d/%0004d %02d:%02d:%02d PID=%d : %s",
            dt.wMonth,
            dt.wDay,
            dt.wYear,
            dt.wHour,
            dt.wMinute,
            dt.wSecond,
            pCopyDataStruct->dwData,
            pCopyDataStruct->lpData);

        // Add line, keeping total line count less or equal 
        // MAX_LINES to limit consumed memory
        int lineCount = m_TextCtrl.GetCount();
        if (lineCount == MAX_LINES)
            m_TextCtrl.DeleteString(0);
        else
            lineCount++;
        // Make added line visible if stream mode
        if (lineCount >= m_LinesPerPage && !m_BrowseOn)
            m_TextCtrl.SetTopIndex(lineCount-m_LinesPerPage+1);
        m_TextCtrl.AddString(buf);
        delete buf;

        // Adjust horizontal scroll bar
        static int dx = 0;
        CDC*  pDC = m_TextCtrl.GetDC();
        CSize sz  = pDC -> GetTextExtent(buf);
        m_TextCtrl.ReleaseDC(pDC);
        if (sz.cx > dx)
        {
            dx = sz.cx;
            m_TextCtrl.SetHorizontalExtent(dx);
        }
        return TRUE;
    }
    return FALSE;
}