home *** CD-ROM | disk | FTP | other *** search
/ Windows Graphics Programming / Feng_Yuan_Win32_GDI_DirectX.iso / Samples / include / pefile.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-11  |  3.2 KB  |  112 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   : pefile.cpp                                                             //
  10. //  Description: Portable Exectuable file class                                      //
  11. //  Version    : 1.00.000, May 31, 2000                                              //
  12. //-----------------------------------------------------------------------------------//
  13.  
  14. #define STRICT
  15. #define WIN32_AND_MEAN
  16.  
  17. #include <windows.h>
  18. #include <assert.h>
  19. #include "pefile.h"
  20.  
  21.  
  22. bool KPEFile::Load(const TCHAR * filename)
  23. {
  24.     m_pBaseAddress = (char *) LoadLibrary(filename);
  25. //    m_pBaseAddress = (char *) LoadLibraryEx(filename, NULL, LOAD_LIBRARY_AS_DATAFILE);
  26.  
  27. //    if ((unsigned) m_pBaseAddress & 1 )
  28. //        m_pBaseAddress --;
  29.  
  30.     if (m_pBaseAddress==NULL)
  31.         return false;
  32.  
  33.     m_pDOSHeader    = (PIMAGE_DOS_HEADER) m_pBaseAddress;
  34.     if ( IsBadReadPtr(m_pBaseAddress, sizeof(IMAGE_DOS_HEADER)) )
  35.         return false;
  36.     if ( m_pDOSHeader->e_magic != IMAGE_DOS_SIGNATURE )
  37.         return false;
  38.  
  39.     m_pNTHeader = (IMAGE_NT_HEADERS *) RealAddress(m_pDOSHeader->e_lfanew);
  40.     return  m_pNTHeader->Signature == IMAGE_NT_SIGNATURE;
  41. }
  42.  
  43.  
  44. void KPEFile::Unload(void)
  45. {
  46.     if (m_pBaseAddress)
  47.     {
  48.         FreeLibrary((HINSTANCE) m_pBaseAddress);
  49.         m_pBaseAddress = NULL;
  50.     }
  51. }
  52.  
  53.  
  54. int KPEFile::EnumExported(void)
  55. {
  56.     IMAGE_EXPORT_DIRECTORY * pExportDir;
  57.     
  58.     pExportDir = (IMAGE_EXPORT_DIRECTORY *) RealAddress(
  59.                     m_pNTHeader->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress);
  60.  
  61.     if (pExportDir == NULL)
  62.         return 0;
  63.     
  64.     DWORD * namerva = (DWORD *) RealAddress((unsigned) pExportDir->AddressOfNames);
  65.     if (namerva == NULL)
  66.         return 0;
  67.  
  68.     int count = 0;
  69.     
  70.     for (unsigned i=0; i<pExportDir->NumberOfNames; i++)
  71.     {
  72.         const char * pName = (const char *) RealAddress(namerva[i]);
  73.  
  74.         if (pName)
  75.         {
  76.             if ( !FuncEnumCallBack(pName) )
  77.                 break;
  78.  
  79.             count ++;
  80.         }
  81.     }
  82.  
  83.     return count;
  84. }
  85.  
  86.  
  87. int KPEFile::EnumSection(void)
  88. {
  89.     int sectionno                    = m_pNTHeader->FileHeader.NumberOfSections;
  90.     IMAGE_SECTION_HEADER * pSections = IMAGE_FIRST_SECTION(m_pNTHeader);
  91.  
  92.     for (int i=0; i<sectionno; i++)
  93.     {
  94.         if ( !SectionEnumCallBack(pSections + i) )
  95.             break;
  96.     }
  97.         
  98.     return i;
  99. }
  100.  
  101.  
  102. const char * KPEFile::GetSectionName(unsigned offset)
  103. {
  104.     int sectionno                    = m_pNTHeader->FileHeader.NumberOfSections;
  105.     IMAGE_SECTION_HEADER * pSections = IMAGE_FIRST_SECTION(m_pNTHeader);
  106.  
  107.     for (int i=0; i<sectionno; i++)
  108.         if ( pSections[i].VirtualAddress == offset )
  109.             return (const char *) pSections[i].Name;
  110.  
  111.     return NULL;
  112. }