home *** CD-ROM | disk | FTP | other *** search
/ Windows Graphics Programming / Feng_Yuan_Win32_GDI_DirectX.iso / Samples / Chapt_04 / Diver / Module.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-11  |  4.6 KB  |  161 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   : module.cpp                                                             //
  10. //  Description: KModuleTable class                                                  //
  11. //  Version    : 1.00.000, May 31, 2000                                              //
  12. //-----------------------------------------------------------------------------------//
  13.  
  14. #define NOCRYPT
  15.  
  16. #include <windows.h>
  17. #include <memory.h>
  18. #include <string.h>
  19. #include <assert.h>
  20.  
  21. #include "Module.h"
  22.  
  23.  
  24. BOOL KInterface::Hack(int n, FARPROC newfunc)
  25. {
  26.     DWORD cBytesWritten;
  27.     
  28.     WriteProcessMemory(GetCurrentProcess(),
  29.                        & pvtbl[n],
  30.                        & newfunc,
  31.                        sizeof(newfunc),
  32.                        &cBytesWritten);
  33.  
  34.     return cBytesWritten == sizeof(newfunc);
  35. }
  36.  
  37.  
  38. BOOL HackMethod(unsigned vtable, int n, FARPROC newfunc)
  39. {
  40.     DWORD cBytesWritten;
  41.     
  42.     WriteProcessMemory(GetCurrentProcess(),
  43.                        (LPVOID) (vtable + n * 4),
  44.                        & newfunc,
  45.                        sizeof(newfunc),
  46.                        &cBytesWritten);
  47.  
  48.     return cBytesWritten == sizeof(newfunc);
  49. }
  50.  
  51.  
  52. int KModuleTable::LookupModule(const char *caller, const char *callee)
  53. {
  54.     for (int i=0; i<m_moduleno; i++)
  55.         if ( (stricmp(m_modules[i].m_caller, caller)==0) &&
  56.              (stricmp(m_modules[i].m_callee, callee)==0) )
  57.             return i;
  58.  
  59.     return -1;
  60. }
  61.                               
  62.  
  63. int KModuleTable::LoadModule(const char *caller, const char *callee, 
  64.                              const char * intrfc,
  65.                              unsigned vtable, unsigned queryinterface, int methodno)
  66. {   
  67.     // check if already loaded    
  68.     m_lastmodule = LookupModule(caller, callee);
  69.     if (m_lastmodule != -1)
  70.         return m_lastmodule;
  71.     
  72.     // too many modules
  73.     if (m_moduleno >= MAX_MODULE)
  74.     {
  75.         m_lastmodule = -1;
  76.         return m_lastmodule;
  77.     }        
  78.  
  79.     // add to module table    
  80.     memset(& m_modules[m_moduleno], 0, sizeof(m_modules[m_moduleno]));
  81.  
  82.     m_modules[m_moduleno].m_handle      = LoadLibrary(callee); 
  83.  
  84.     Copy(m_modules[m_moduleno].m_caller, caller);
  85.     Copy(m_modules[m_moduleno].m_callee, callee);
  86.     Copy(m_modules[m_moduleno].m_intrfc, intrfc);
  87.  
  88.     m_modules[m_moduleno].m_vtable           = vtable;
  89.     m_modules[m_moduleno].m_queryinterface = queryinterface;
  90.     m_modules[m_moduleno].m_methodno       = methodno;
  91.  
  92. //  char temp[64];
  93.     
  94. //  wsprintf(temp, "Loadlibrary(%s)=%x", callee, m_modules[m_moduleno].m_handle);
  95. //  MessageBox(NULL, temp, "LoadModule", MB_OK);
  96.  
  97.     if (m_modules[m_moduleno].m_handle == NULL)
  98.     {
  99.         assert(FALSE);
  100.         m_lastmodule = -1;
  101.     }
  102.     else        
  103.         m_lastmodule = m_moduleno++;
  104.     
  105.     return m_lastmodule;
  106. }
  107.  
  108.  
  109. /*
  110. int CModuleTable::LoadInterface(REFCLSID rclsid, REFIID riid)
  111. {
  112.     // check if already loaded
  113.     for (int i=0; i<m_moduleno; i++)
  114.     {
  115.         if (m_modules[i].m_isinterface)
  116.         if (m_modules[i].m_clsid == rclsid)
  117.         if (m_modules[i].m_iid   == riid)
  118.         {
  119.             m_lastmodule = i;
  120.             return m_lastmodule;
  121.         }
  122.     }
  123.  
  124.     // too many modules
  125.     if (m_moduleno >= MAX_MODULE)
  126.     {
  127.         m_lastmodule = -1;
  128.         return m_lastmodule;
  129.     }
  130.  
  131.     // add to module table
  132.     m_modules[m_moduleno].m_isinterface = TRUE;
  133.     m_modules[m_moduleno].m_clsid       = rclsid;
  134.     m_modules[m_moduleno].m_iid         = riid;
  135.     m_modules[m_moduleno].m_iunknown    = NULL;
  136.  
  137.     HRESULT hr = CoCreateInstance(rclsid, NULL, CLSCTX_INPROC_SERVER, riid, 
  138.                                   (void **) & m_modules[m_moduleno].m_iunknown );
  139.  
  140.     if (SUCCEEDED(hr))
  141.         m_lastmodule = m_moduleno ++;
  142.     else
  143.         m_lastmodule = -1;
  144.  
  145.     return m_lastmodule;
  146. }
  147. */
  148.  
  149. void KModuleTable::FreeModules(void)
  150. {
  151.     for (int i=0; i<m_moduleno; i++)
  152.      /* if (m_modules[i].m_isinterface)
  153.         {
  154.             ((IUnknown *) m_modules[i].m_iunknown)->Release();
  155.         }
  156.         else */
  157.         {
  158.             FreeLibrary(m_modules[i].m_handle);
  159.         }
  160. }
  161.