home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l430 / 1.ddi / CHAP5.ZIP / WALKSEGS.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-09  |  2.1 KB  |  68 lines

  1. /*
  2.     WALKSEGS.C -- Walks segments belonging to module
  3.  
  4.     From Chapter 5 of "Undocumented Windows" (Addison-Wesley 1992)
  5.     by Andrew Schulman, Dave Maxey and Matt Pietrek
  6.  
  7.     Build using: WINIOBC WALKSEGS (for Borland C++ v3.00)
  8.                  WINIOMS WALKSEGS (for Microsoft C/SDK)
  9. */
  10.  
  11. #include <windows.h>
  12. #include <dos.h>
  13. #include "winio.h"
  14.  
  15. // Ignore the 'parameter 1 different' error from MS - the following
  16. // declaration splits the original GetCodeHandle lpProc param into
  17. // seg/ofs params.
  18. DWORD (FAR PASCAL *GetCodeHandleD)(WORD hMod, WORD wSeg);
  19.  
  20. /* GetCodeInfo is documented, but not very well */
  21. typedef struct {
  22.     WORD wOfs, wLen, wFlags, wAlloc, wHandle, wShift, wReserved[2];
  23.     } CODEINFO;
  24.  
  25. main(int argc, char *argv[])
  26. {
  27.     char *modname;
  28.     HANDLE hMod;
  29.     WORD wSeg;
  30.     WORD cSeg;
  31.     DWORD dwCode;
  32.     
  33.     winio_about("WALKSEGS"
  34.         "\nWalks segments belonging to module"
  35.         "\nUsage: WALKSEGS [module name]"
  36.         "\n\nFrom Chapter 5 of"
  37.         "\n\"Undocumented Windows\" (Addison-Wesley, 1992)"
  38.         "\nby Andrew Schulman, David Maxey and Matt Pietrek"
  39.         );
  40.     
  41.     GetCodeHandleD = GetCodeHandle;
  42.     
  43.     modname = (argc < 2) ? "KERNEL" : argv[1];
  44.     if (! (hMod = GetModuleHandle(modname)))
  45.         fail("Cannot locate module");
  46.     
  47.     /* Get number of segments from offset 1Ch in Module Table */
  48.     cSeg = *((WORD far *) MK_FP(hMod, 0x1c));
  49.     
  50.     printf("%s %04x\n", modname, hMod);
  51.     for (wSeg=1; wSeg<=cSeg; wSeg++)
  52.         if ((dwCode = GetCodeHandleD(hMod, wSeg)) < 32)
  53.             printf("   Seg #%d\tError!!\n", wSeg);
  54.         else
  55.         {
  56.             CODEINFO codeinfo;
  57.             WORD wSel = HIWORD(dwCode); // code selector
  58.             /* this alternate form of GetCodeInfo is documented */
  59.             GetCodeInfo((FARPROC) MK_FP(hMod, wSeg), &codeinfo);
  60.             printf("   Seg #%d\t%04x\tLen=%04xh\t%s\n", 
  61.                 wSeg, wSel, 
  62.                 codeinfo.wLen, 
  63.                 (codeinfo.wFlags & 1) ? "DATA" : "CODE");
  64.         }
  65.     return 0;
  66. }
  67.  
  68.