CDumpContext Class


For the latest documentation on Visual Studio 2017 RC, see Visual Studio 2017 RC Documentation.

Supports stream-oriented diagnostic output in the form of human-readable text.

class CDumpContext  

Public Constructors

CDumpContext::CDumpContextConstructs a CDumpContext object.

Public Methods

CDumpContext::DumpAsHexDumps the indicated item in hexadecimal format.
CDumpContext::FlushFlushes any data in the dump context buffer.
CDumpContext::GetDepthGets an integer corresponding to the depth of the dump.
CDumpContext::HexDumpDumps bytes contained in an array in hexadecimal format.
CDumpContext::SetDepthSets the depth of the dump.

Public Operators

CDumpContext::operator <<Inserts variables and objects into the dump context.

CDumpContext does not have a base class.

You can use afxDump, a predeclared CDumpContext object, for most of your dumping. The afxDump object is available only in the Debug version of the Microsoft Foundation Class Library.

Several of the memory diagnostic services use afxDump for their output.

Under the Windows environment, the output from the predefined afxDump object, conceptually similar to the cerr stream, is routed to the debugger via the Windows function OutputDebugString.

The CDumpContext class has an overloaded insertion ( <<) operator for CObject pointers that dumps the object's data. If you need a custom dump format for a derived object, override CObject::Dump. Most Microsoft Foundation classes implement an overridden Dump member function.

Classes that are not derived from CObject, such as CString, CTime, and CTimeSpan, have their own overloaded CDumpContext insertion operators, as do often-used structures such as CFileStatus, CPoint, and CRect.

If you use the IMPLEMENT_DYNAMIC or IMPLEMENT_SERIAL macro in the implementation of your class, then CObject::Dump will print the name of your CObject-derived class. Otherwise, it will print CObject.

The CDumpContext class is available with both the Debug and Release versions of the library, but the Dump member function is defined only in the Debug version. Use #ifdef _DEBUG / #endif statements to bracket your diagnostic code, including your custom Dump member functions.

Before you create your own CDumpContext object, you must create a CFile object that serves as the dump destination.

For more information on CDumpContext, see Debugging MFC Applications.

#define _DEBUG


Header: afx.h

Constructs an object of class CDumpContext.

CDumpContext(CFile* pFile = NULL);


A pointer to the CFile object that is the dump destination.


The afxDump object is constructed automatically.

Do not write to the underlying CFile while the dump context is active; otherwise, you will interfere with the dump. Under the Windows environment, the output is routed to the debugger via the Windows function OutputDebugString.


         CFile f;
         if (!f.Open(_T("dump.txt"), CFile::modeCreate | CFile::modeWrite)) 
            AFXDUMP(_T("Unable to open file\n"));
         CDumpContext dc(&f);      

Dumps the specified type formatted as hexadecimal numbers.

CDumpContext& DumpAsHex(BYTE b);  
CDumpContext& DumpAsHex(DWORD dw);  
CDumpContext& DumpAsHex(int n);  
CDumpContext& DumpAsHex(LONG l);  
CDumpContext& DumpAsHex(LONGLONG n);  
CDumpContext& DumpAsHex(UINT u);  
CDumpContext& DumpAsHex(ULONGLONG n);  
CDumpContext& DumpAsHex(WORD w);

Return Value

A reference to a CDumpContext object.


Call this member function to dump the item of the specified type as a hexadecimal number. To dump an array, call CDumpContext::HexDump.


      #if _DEBUG

Forces any data remaining in buffers to be written to the file attached to the dump context.

void Flush();


      #if _DEBUG

Determines whether a deep or shallow dump is in process.

int GetDepth() const;  

Return Value

The depth of the dump as set by SetDepth.


See the example for SetDepth.

Dumps an array of bytes formatted as hexadecimal numbers.

void HexDump(
    LPCTSTR lpszLine,  
    BYTE* pby,  
    int nBytes,  
    int nWidth);


A string to output at the start of a new line.

A pointer to a buffer containing the bytes to dump.

The number of bytes to dump.

Maximum number of bytes dumped per line (not the width of the output line).


To dump a single, specific item type as a hexadecimal number, call CDumpContext::DumpAsHex.


         #if _DEBUG
            TCHAR test[] = _T("This is a test of CDumpContext::HexDump\n");
            afxDump.HexDump(_T("."), (BYTE*)test, sizeof(test), 20);

Outputs the specified data to the dump context.

CDumpContext& operator<<(const CObject* pOb);


CDumpContext& operator<<(const CObject& ob);


CDumpContext& operator<<(LPCTSTR lpsz);


CDumpContext& operator<<(const void* lp);


CDumpContext& operator<<(BYTE by);


CDumpContext& operator<<(WORD w);


CDumpContext& operator<<(DWORD dw);


CDumpContext& operator<<(int n);


CDumpContext& operator<<(double d);


CDumpContext& operator<<(float f);


CDumpContext& operator<<(LONG l);


CDumpContext& operator<<(UINT u);


CDumpContext& operator<<(LPCWSTR lpsz);


CDumpContext& operator<<(LPCSTR lpsz);


CDumpContext& operator<<(LONGLONG n);

CDumpContext& operator<<(ULONGLONG n);

CDumpContext& operator<<(HWND h);

CDumpContext& operator<<(HDC h);

CDumpContext& operator<<(HMENU h);

CDumpContext& operator<<(HACCEL h);

CDumpContext& operator<<(HFONT h);

Return Value

A CDumpContext reference. Using the return value, you can write multiple insertions on a single line of source code.


The insertion operator is overloaded for CObject pointers as well as for most primitive types. A pointer to character results in a dump of string contents; a pointer to void results in a hexadecimal dump of the address only. A LONGLONG results in a dump of a 64-bit signed integer; A ULONGLONG results in a dump of a 64-bit unsigned integer.

If you use the IMPLEMENT_DYNAMIC or IMPLEMENT_SERIAL macro in the implementation of your class, then the insertion operator, through CObject::Dump, will print the name of your CObject-derived class. Otherwise, it will print CObject. If you override the Dump function of the class, then you can provide a more meaningful output of the object's contents instead of a hexadecimal dump.


         #if _DEBUG
            CStringList li;
            li.AddHead(_T("item 0"));
            li.AddHead(_T("item 1"));
            CString s = _T("test");
            int i = 7;
            long lo = 1000000000L;
            LONGLONG lolo = 12345678901234i64;
            afxDump << _T("list=") << &li << _T("string=")
               << s << _T("int=") << i << _T("long=") << lo 
               << _T("LONGLONG=") << lolo << _T("\n");

Sets the depth for the dump.

void SetDepth(int nNewDepth);


The new depth value.


If you are dumping a primitive type or simple CObject that contains no pointers to other objects, then a value of 0 is sufficient. A value greater than 0 specifies a deep dump where all objects are dumped recursively. For example, a deep dump of a collection will dump all elements of the collection. You may use other specific depth values in your derived classes.

System_CAPS_ICON_note.jpg Note

Circular references are not detected in deep dumps and can result in infinite loops.


      #if _DEBUG
         afxDump.SetDepth(1);  // Specifies deep dump
         ASSERT(afxDump.GetDepth() == 1);      

Hierarchy Chart
CFile Class
CObject Class