home *** CD-ROM | disk | FTP | other *** search
/ Windows Graphics Programming / Feng_Yuan_Win32_GDI_DirectX.iso / Samples / Chapt_03 / Handles / MemView.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-11  |  4.7 KB  |  195 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   : memview.cpp                                                         //
  10. //  Description: Memory dumper dialog box                                            //
  11. //  Version    : 1.00.000, May 31, 2000                                              //
  12. //-----------------------------------------------------------------------------------//
  13.  
  14. #define STRICT
  15. #define WIN32_LEAN_AND_MEAN
  16.  
  17. #include <windows.h>
  18. #include <assert.h>
  19. #include <tchar.h>
  20. #include <fstream.h>
  21. #include <stdio.h>
  22.  
  23. #include "resource.h"
  24. #include "dialog.h"
  25.  
  26. #include "MemView.h"
  27.  
  28. // CMemViewer class implementation
  29.  
  30. BOOL KMemViewer::OnInitDialog(HWND hWnd, WPARAM wParam, LPARAM lParam)
  31. {
  32.     m_hWnd = hWnd;
  33.  
  34.     TCHAR temp[32];
  35.  
  36.     wsprintf(temp, "0x%08lx", m_start); SetDlgItemText(m_hWnd, IDC_REGIONSTART, temp);
  37.     wsprintf(temp, "0x%lx", m_size);    SetDlgItemText(m_hWnd, IDC_REGIONSIZE,  temp);
  38.         
  39.     m_current  = m_start;
  40.     m_pagesize = 128;
  41.  
  42.     m_hFont = CreateFont(16, 0, 0, 0, 0, 0, 0, 0, ANSI_CHARSET,
  43.         OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS,
  44.         DEFAULT_QUALITY, FIXED_PITCH | FF_DONTCARE, "Courier New");
  45.         
  46.     SendDlgItemMessage(m_hWnd, IDC_DUMP, WM_SETFONT, (WPARAM) m_hFont, 0);
  47.     
  48.     assert( m_size % m_pagesize == 0);
  49.  
  50.     SendDlgItemMessage(m_hWnd, IDC_BYTE, BM_SETCHECK, BST_CHECKED, 0);
  51.     DumpPage();
  52.  
  53.     return TRUE;
  54. }
  55.     
  56.  
  57. BOOL KMemViewer::OnCommand(HWND hWnd, WPARAM wParam, LPARAM lParam)
  58. {
  59.     switch ( LOWORD(wParam) )
  60.     {
  61.         case IDC_BYTE:
  62.         case IDC_WORD:
  63.         case IDC_DWORD:
  64.             DumpPage();
  65.             return TRUE;
  66.  
  67.         case IDC_DOWN:
  68.             if ( m_current + m_pagesize >= m_start + m_size )
  69.                 return FALSE;
  70.             m_current += m_pagesize;
  71.             DumpPage();
  72.             return TRUE;
  73.  
  74.         case IDC_UP:
  75.             if ( m_current == m_start )
  76.                 return FALSE;
  77.             m_current -= m_pagesize;
  78.             DumpPage();
  79.             return TRUE;
  80.  
  81.         case IDC_MEMORYDUMP:
  82.             OpenDump();
  83.             Dump(m_start, 0, m_size, UnitSize());
  84.             CloseDump();
  85.             return TRUE;
  86.  
  87.         case IDC_SEARCH:
  88.             Search();
  89.             return TRUE;
  90.  
  91.         case IDOK:
  92.             EndDialog(m_hWnd, TRUE);
  93.             return TRUE;
  94.     }
  95.  
  96.     return FALSE;
  97. }
  98.  
  99.  
  100. int KMemViewer::UnitSize(void) const
  101. {
  102.     if ( SendDlgItemMessage(m_hWnd, IDC_BYTE, BM_GETCHECK, 0, 0) == BST_CHECKED)
  103.         return sizeof(unsigned char);
  104.     else if ( SendDlgItemMessage(m_hWnd, IDC_WORD, BM_GETCHECK, 0, 0) == BST_CHECKED)
  105.         return sizeof(unsigned short);
  106.     else
  107.         return sizeof(unsigned long);
  108. }
  109.  
  110.  
  111. void KMemViewer::Search(void)
  112. {
  113.     unsigned char value[4];
  114.  
  115.     {
  116.         TCHAR Target[32];
  117.     
  118.         GetDlgItemText(m_hWnd, IDC_TARGET, Target, sizeof(Target));
  119.  
  120.         sscanf(Target, "%x", value);
  121.     }
  122.     
  123.     TCHAR TempPath[MAX_PATH], FileName[MAX_PATH];
  124.  
  125.     GetTempPath(sizeof(TempPath), TempPath);
  126.     
  127.     GetTempFileName(TempPath, "Mem", 0000, FileName);
  128.  
  129.     ofstream outfile;
  130.  
  131.     outfile.open(FileName);
  132.  
  133.     outfile << "Search for 0x";
  134.  
  135.     int size;
  136.  
  137.     if ( SendDlgItemMessage(m_hWnd, IDC_BYTE, BM_GETCHECK, 0, 0) == BST_CHECKED)
  138.     {
  139.         wsprintf(TempPath, "%02x", value[0]);
  140.         size = 1;
  141.     }
  142.     else if ( SendDlgItemMessage(m_hWnd, IDC_WORD, BM_GETCHECK, 0, 0) == BST_CHECKED)
  143.     {
  144.         wsprintf(TempPath, "%04x", ((unsigned short *)value)[0]);
  145.         size = 2;
  146.     }
  147.     else
  148.     {
  149.         wsprintf(TempPath, "%08x", ((unsigned long *)value)[0]);
  150.         size = 4;
  151.     }
  152.  
  153.     outfile << TempPath;
  154.  
  155.     wsprintf(TempPath, " in region start at %08lx, size %x bytes\n", m_start, m_size);
  156.  
  157.     outfile << TempPath;
  158.  
  159.     for (unsigned char * addr = m_start; (addr + size - 1) < (m_start + m_size); addr++)
  160.     {
  161.         for (int s=0; s<size; s++)
  162.             if ( addr[s] != value[s] )
  163.                 goto miss;
  164.  
  165.         wsprintf(TempPath, "%08lx\r\n", addr);
  166.         outfile << TempPath;
  167.  
  168.     miss: ;
  169.  
  170.     }        
  171.  
  172.     outfile.close();
  173.  
  174.     wsprintf(TempPath, "notepad %s", FileName);
  175.     WinExec(TempPath, SW_NORMAL);
  176.  
  177. }
  178.  
  179.  
  180. void KMemViewer::DumpPage(void)
  181. {
  182.     TCHAR buffer[1024];
  183.     
  184.     buffer[0] = 0;
  185.     unsigned char * p = m_current;
  186.     
  187.     for (unsigned row=0; row<m_pagesize/16; row++, p+=16)
  188.     {
  189.         DumpLine(p, 0, UnitSize());
  190.         strcat(buffer, m_line);
  191.     }
  192.     
  193.     SetDlgItemText(m_hWnd, IDC_DUMP, buffer);
  194. }
  195.