home *** CD-ROM | disk | FTP | other *** search
/ Windows Graphics Programming / Feng_Yuan_Win32_GDI_DirectX.iso / Samples / include / MemDump.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-11  |  3.9 KB  |  162 lines

  1. //-----------------------------------------------------------------------------------//
  2. //              Windows Graphics Programming: Win32 GDI and DirectDraw               //
  3. //                             ISBN  0-13-086985-6                                   //
  4. //                                                                                   //
  5. //  Written            by  Yuan, Feng                             www.fengyuan.com   //
  6. //  Copyright (c) 2000 by  Hewlett-Packard Company                www.hp.com         //
  7. //  Published          by  Prentice Hall PTR, Prentice-Hall, Inc. www.phptr.com      //
  8. //                                                                                   //
  9. //  FileName   : memdump.cpp                                                         //
  10. //  Description: Memory dumper, Chapter 3                                            //
  11. //  Version    : 1.00.000, May 31, 2000                                              //
  12. //-----------------------------------------------------------------------------------//
  13.  
  14. #define STRICT
  15. #include <windows.h>
  16. #include <assert.h>
  17. #include <tchar.h>
  18. #include <fstream.h>
  19. #include <stdio.h>
  20.  
  21. #include "MemDump.h"
  22.  
  23. void KMemDump::DumpLine(unsigned char * p, unsigned offset, int unitsize)
  24. {
  25.     if ( unitsize==1 )
  26.     {
  27.         wsprintf(m_line, "%08lx: "
  28.                          "%02lx %02lx %02lx %02lx %02lx %02lx %02lx %02lx "
  29.                          "%02lx %02lx %02lx %02lx %02lx %02lx %02lx %02lx",
  30.                         p + offset, 
  31.                         p[0], p[1],  p[2],  p[3],  p[4],  p[5],  p[6],  p[7],
  32.                         p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
  33.  
  34.     }
  35.     else if ( unitsize==2 )
  36.     {
  37.         unsigned short * q = (unsigned short *) p;
  38.  
  39.         wsprintf(m_line, "%08lx: "
  40.                         "%04lx %04lx %04lx %04lx %04lx %04lx %04lx %04lx",
  41.                         p + offset, 
  42.                         q[0], q[1], q[2], q[3], q[4], q[5], q[6], q[7]);
  43.     }
  44.     else
  45.     {
  46.         unsigned long * r = (unsigned long *) p;
  47.  
  48.         wsprintf(m_line, "%08lx: "
  49.                         "%08lx %08lx %08lx %08lx",
  50.                         p + offset, 
  51.                         r[0], r[1], r[2], r[3]);
  52.     }
  53.  
  54.     char s[2] = { 0 };
  55.  
  56.     strcat(m_line, "  ");
  57.     for (int i=0; i<16; i++)
  58.     {
  59.         if ( isprint(p[i]) )
  60.             s[0] = p[i];
  61.         else
  62.             s[0] = '.';
  63.  
  64.         strcat(m_line, s);
  65.     }
  66.         
  67.     strcat(m_line, "\r\n");
  68. }
  69.  
  70.  
  71. void KMemDump::OpenDump(void)
  72. {
  73.        TCHAR TempPath[MAX_PATH];
  74.  
  75.     GetTempPath(sizeof(TempPath), TempPath);
  76.     
  77.     GetTempFileName(TempPath, "Mem", 0000, m_filename);
  78.  
  79.     assert(m_stream==NULL);
  80.  
  81.     m_stream = new ofstream;
  82.     assert(m_stream);
  83.  
  84.     m_stream->open(m_filename);
  85. }
  86.  
  87.  
  88. void KMemDump::Newline(void)
  89. {
  90.     assert(m_stream);
  91.  
  92.     * m_stream << "\n";
  93. }
  94.  
  95.  
  96. void KMemDump::Writeln(const TCHAR * text)
  97. {
  98.     assert(m_stream);
  99.  
  100.     * m_stream << text;
  101.     * m_stream << "\n";
  102. }
  103.  
  104.  
  105. void KMemDump::CloseDump(void)
  106. {
  107.        TCHAR TempPath[MAX_PATH];
  108.  
  109.     GetTempPath(sizeof(TempPath), TempPath);
  110.  
  111.     assert(m_stream);
  112.  
  113.     m_stream->close();
  114.  
  115.     wsprintf(TempPath, "notepad %s", m_filename);
  116.     WinExec(TempPath, SW_NORMAL);
  117.  
  118.     m_stream = NULL;
  119. }
  120.  
  121.  
  122. void KMemDump::Dump(unsigned char * start, unsigned offset, int size, int unitsize)
  123. {
  124.     if ( offset==0 )
  125.     {
  126.         HANDLE hHeaps[10];
  127.         int no = GetProcessHeaps(10, hHeaps);
  128.     
  129.         // walk the heap if it is a heap
  130.         for (int i=0; i<no; i++)
  131.             if ( start == hHeaps[i] )
  132.             {
  133.                 PROCESS_HEAP_ENTRY entry;
  134.  
  135.                 entry.lpData = NULL;
  136.  
  137.                 while ( HeapWalk(start, & entry) )
  138.                 {
  139.                     wsprintf(m_line, "%x %d+%d bytes %x\r\n", 
  140.                         entry.lpData, entry.cbData,
  141.                         entry.cbOverhead, entry.iRegionIndex);
  142.                 
  143.                     * m_stream << m_line;
  144.                 }
  145.                 * m_stream << "\r\n";
  146.             
  147.                 break;
  148.             }            
  149.     }
  150.  
  151.     * m_stream << size;
  152.     * m_stream << " bytes\n";
  153.     while (size>0)
  154.     {
  155.         DumpLine(start, offset, unitsize);
  156.         start += 16;
  157.         size  -= 16;
  158.  
  159.         * m_stream << m_line;
  160.     }
  161. }
  162.