home *** CD-ROM | disk | FTP | other *** search
/ Windows Graphics Programming / Feng_Yuan_Win32_GDI_DirectX.iso / Samples / Chapt_03 / Fosterer / Extension.cpp next >
Encoding:
C/C++ Source or Header  |  2000-05-11  |  3.8 KB  |  154 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   : extension.cpp                                                         //
  10. //  Description: Interface with debugger extension                                   //
  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 <imagehlp.h> 
  19. #include <richedit.h>
  20. #include <wdbgexts.h>
  21.  
  22. #include "ImageModule.h"
  23. #include "Extension.h"
  24. #include "Host.h"
  25.  
  26. bool KExtensionRoutine::Attach(HINSTANCE hModule, const char * name)
  27. {
  28.     assert(hModule);
  29.  
  30.     m_funcname = name;
  31.     m_funcaddr = (ExtensionRoutine) GetProcAddress(hModule, name);
  32.  
  33.     return m_funcaddr != NULL;
  34. }
  35.  
  36.  
  37. bool KExtensionRoutine::Help(void)
  38. {
  39.     if ( m_funcaddr )
  40.     {
  41.         m_funcaddr(0, 0, 0, 1, "-?");
  42.         return true;
  43.     }
  44.     else
  45.         return false;
  46. }
  47.  
  48.  
  49. bool KExtensionRoutine::Call(const char * args, HANDLE proc, HANDLE thread, ULONG pc, ULONG processor)
  50. {
  51.     if ( m_funcaddr )
  52.     {
  53.         // NULL is not be handled well by the extension DLL
  54.         if ( args==NULL )
  55.             args = "";
  56.  
  57.         try
  58.         {
  59.             m_funcaddr(proc, thread, pc, processor, args);
  60.         }
  61.         catch (...)
  62.         {
  63.             MessageBox(NULL, m_funcname, "Exception in", MB_OK);
  64.         }
  65.  
  66.         return true;
  67.     }
  68.     else
  69.     {
  70.         assert(false);
  71.  
  72.         return false;
  73.     }
  74. }
  75.  
  76.  
  77. bool KGDIExtension::Load(const void * ExtensionAPI, const char * filename)
  78. {
  79.     // check OS version
  80.     char temp[MAX_PATH];
  81.     
  82.     OSVERSIONINFO osvi;
  83.     osvi.dwOSVersionInfoSize = sizeof(osvi);
  84.     GetVersionEx(& osvi);
  85.  
  86.     theHost.ExtOutput("Windows OS v%d.%d, build %d %s\n", 
  87.         osvi.dwMajorVersion, osvi.dwMinorVersion,
  88.         LOWORD(osvi.dwBuildNumber), osvi.szCSDVersion);
  89.     
  90.     hGDI = LoadLibrary(filename);
  91.  
  92.     if ( hGDI==NULL )
  93.         return false;
  94.     
  95.     temp[0] = '"';
  96.     GetModuleFileName(hGDI, temp+1, sizeof(temp));
  97.     strcat(temp, "\" loaded.\n");
  98.     theHost.ExtOutput(temp);
  99.  
  100.     PWINDBG_EXTENSION_DLL_INIT pInit = (PWINDBG_EXTENSION_DLL_INIT) 
  101.         GetProcAddress(hGDI, "WinDbgExtensionDllInit");
  102.  
  103.     if ( pInit )
  104.         pInit((WINDBG_EXTENSION_APIS *)ExtensionAPI, MajorVersion, LOWORD(osvi.dwBuildNumber));
  105.     else
  106.         return false;
  107.  
  108.     PWINDBG_CHECK_VERSION pCheck = (PWINDBG_CHECK_VERSION) GetProcAddress(hGDI, "CheckVersion");
  109.  
  110.     if ( pCheck )
  111.         pCheck();
  112.     else
  113.         return false;
  114.  
  115.     return true;
  116. }
  117.  
  118.  
  119. bool KGDIExtension::Do(const char *command)
  120. {
  121.     if ( command && strlen(command) )
  122.     {
  123.         // skip to non space character
  124.         while ( isspace(* command) )
  125.             command ++;
  126.  
  127.         char cmd[MAX_PATH];
  128.         strcpy(cmd, command);
  129.  
  130.         // find space to seperate command and parameters
  131.         char * para = strchr(cmd, ' ');  
  132.  
  133.           theHost.ExtOutput("%s\n", command);
  134.  
  135.         if ( para )
  136.             * para++ = NULL;  // 0 terminate command
  137.         else
  138.             para = "";
  139.  
  140.         KExtensionRoutine routine;
  141.  
  142.         if ( routine.Attach(hGDI, cmd) )
  143.         {
  144.             theHost.hProcess = m_hProcess;
  145.             routine.Call(para);
  146.             return true;
  147.         }
  148.         else
  149.             theHost.ExtOutput("Unknown command: %s\n", cmd);
  150.     }
  151.  
  152.     return false;
  153. }
  154.