home *** CD-ROM | disk | FTP | other *** search
/ Windows Graphics Programming / Feng_Yuan_Win32_GDI_DirectX.iso / Samples / Chapt_03 / Fosterer / ImageModule.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-11  |  3.0 KB  |  102 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   : imagemodule.cpp                                                     //
  10. //  Description: Query debug symbols using ImageHlp API                              //
  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 <imagehlp.h>
  19. #include <assert.h>
  20. #include <wdbgexts.h>
  21.  
  22. #include "ImageModule.h"
  23. #include "Host.h"
  24.  
  25.  
  26. KImageModule::KImageModule()
  27. {
  28.     static  int nLastProcess = 1961;
  29.  
  30.     m_pidi     = NULL;
  31.     m_hProcess = (HANDLE) (nLastProcess ++);
  32. }
  33.  
  34.  
  35. KImageModule::~KImageModule()
  36. {
  37.     Unload();
  38. }
  39.  
  40.  
  41. bool KImageModule::Load(char * filename, char *sympath)
  42. {
  43.     m_pidi = MapDebugInformation(NULL, filename, sympath, 0); 
  44.  
  45.     if ( m_pidi==NULL ) 
  46.         return false;
  47.  
  48.     if ( !SymInitialize(m_hProcess, sympath, FALSE) ) 
  49.         return false;
  50.     
  51.     if ( !SymLoadModule(m_hProcess, NULL, filename, 0, (DWORD)m_pidi->ReservedMappedBase, 0 ) ) 
  52.         return false;
  53.  
  54.     IMAGEHLP_MODULE im;
  55.     im.SizeOfStruct = sizeof(im);
  56.  
  57.     SymGetModuleInfo( m_hProcess, (DWORD)m_pidi->ReservedMappedBase, &im );
  58.     
  59.     theHost.ExtOutput("""%s"" loaded.\n", im.LoadedImageName);
  60.     return true;
  61. }
  62.  
  63.  
  64. void KImageModule::Unload(void)
  65. {
  66.     if ( m_pidi )
  67.     {
  68.         SymUnloadModule(m_hProcess, (DWORD)m_pidi->ReservedMappedBase);
  69.         SymCleanup(m_hProcess);
  70.         UnmapDebugInformation(m_pidi);
  71.  
  72.         m_pidi = NULL;
  73.     }
  74.  
  75.  
  76. const IMAGEHLP_SYMBOL * KImageModule::ImageGetSymbol(const char * name)
  77. {
  78.     char localname[MAX_PATH];
  79.  
  80.     memset(m_is, 0, sizeof(m_is));
  81.     m_is[0].SizeOfStruct  = sizeof(IMAGEHLP_SYMBOL);
  82.     m_is[0].MaxNameLength = sizeof(m_is) - sizeof(m_is[0]);
  83.     
  84.     // The silly implementation in imagehlp.dll will try to change the '!' in name
  85.     // to 0, which generates an access violation, because name would came from read-only
  86.     // constant data. Make a local copy to solve the problem
  87.     strcpy(localname, name);
  88.     
  89.     if ( SymGetSymFromName(m_hProcess, localname, m_is) )
  90.     {
  91.         m_is[0].Address += (unsigned) m_pidi->ImageBase - (unsigned) m_pidi->ReservedMappedBase;
  92.         
  93.         return & m_is[0];
  94.     }
  95.     else
  96.     {
  97.         DWORD err = GetLastError();
  98.         return NULL;
  99.     }
  100. }
  101.