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

  1. /*
  2.     NULLSEG.C -- Reports on task's NULL segment
  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 NULLSEG HANDLES (for Borland C++ v3.00)
  8.                  WINIOMS NULLSEG HANDLES (for Microsoft C/SDK)
  9.                      
  10.     Changed from version in book:  for 3.0, have to convert hInstance
  11.     to a valid selector
  12. */
  13.  
  14. #include <windows.h>
  15. #include <stdlib.h>
  16. #include <dos.h>
  17. #include "winio.h"
  18. #include "handles.h"
  19.  
  20. typedef struct {
  21.     WORD wMustBeZero;
  22.     DWORD dwOldSSSP;
  23.     WORD pLocalHeap;
  24.     WORD pAtomTable;
  25.     WORD pStackTop;
  26.     WORD pStackMin;
  27.     WORD pStackBottom;
  28.     } INSTDATA;
  29.  
  30. extern DWORD FAR PASCAL GetSelectorLimit(WORD sel);
  31.  
  32. BOOL IsValidNULLSegment(HANDLE h);
  33. BOOL IsValidLocalHeap(BYTE far *fp);
  34. WORD HandleToSel(HANDLE h);
  35. int Kernel1632(void);
  36. int axtoi(char *s);
  37.  
  38. main(int argc, char *argv[])
  39. {
  40.     INSTDATA far *lpInstData;
  41.     BYTE far *lpLocalHeap;
  42.     HANDLE hModule, hDGroup;
  43.     
  44.     winio_about("NULLSEG"
  45.         "\nReports on task or DLL's NULL segment"
  46.         "\nUsage: NULLSEG 0x[taskid] -or- NULLSEG [modname]"
  47.         "\n\nFrom Chapter 5 of"
  48.         "\n\"Undocumented Windows\" (Addison-Wesley, 1992)"
  49.         "\nby Andrew Schulman, David Maxey and Matt Pietrek"
  50.         );
  51.     
  52.     if (argv[1][0] == '0' && argv[1][1] == 'x')
  53.     {
  54.         // assume that hex number on command line is a task
  55.         hDGroup = HINSTANCE_FROM_HTASK(HandleToSel(axtoi(argv[1])));
  56.         hModule = 0;  // GetModuleDgroup() not appropriate for tasks
  57.                       // because a task can have more than one instance
  58.                       // and therefore more than one corresponding DGROUP
  59.                       // (for tasks GetModuleDgroup() isn't a true function)
  60.     }
  61.     else if (argc < 2)
  62.     {
  63.         hDGroup = __hInst;
  64.         hModule = GetCurrentModule();
  65.     }
  66.     else
  67.     {
  68.         char *modname = argv[1] ;
  69.         if (! (hModule = GetModuleHandle(modname)))
  70.             fail("Can't locate %s", modname);
  71.         if (! (hDGroup = GetModuleDgroup(hModule)))
  72.             fail("Can't locate %s's DGROUP", modname);
  73.     }
  74.     
  75.     // oops, forgot to do this in version in book!
  76.     hDGroup = HandleToSel(hDGroup);
  77.     
  78.     if (! IsValidNULLSegment(hDGroup))
  79.         fail("Doesn't have a NULL segment");
  80.     
  81.     lpInstData = MK_FP(hDGroup, 0);
  82.     
  83.     // fields starts off holding _rsrvptrs (5) from compiler
  84.     if (lpInstData->dwOldSSSP != 5)
  85.         printf("dwOldSSSP = %Fp\n", lpInstData->dwOldSSSP);
  86.     
  87.     if (lpInstData->pLocalHeap)
  88.     {
  89.         lpLocalHeap = MK_FP(hDGroup, lpInstData->pLocalHeap);
  90.         printf("pLocalHeap = %Fp\n", lpLocalHeap);
  91.         if (! IsValidLocalHeap(lpLocalHeap))
  92.             printf("Not a valid local heap!\n");
  93.     }
  94.     else
  95.         printf("No local heap\n");
  96.     
  97.     if (lpInstData->pAtomTable)
  98.     {
  99.         // for Atom Table structure, see ATOMWALK.C, WINEXP.H
  100.         WORD far *lpAtomTable = MK_FP(hDGroup, lpInstData->pAtomTable);
  101.         printf("pAtomTable = %Fp (%u atoms)\n", lpAtomTable, *lpAtomTable);
  102.     }
  103.     else
  104.         printf("No atom table\n");
  105.     
  106.     printf("pStackTop  =   %04x\n", lpInstData->pStackTop);
  107.     printf("pStackMin  =   %04x\n", lpInstData->pStackMin);
  108.     printf("pStackBottom = %04x\n", lpInstData->pStackBottom);
  109.     if (hModule && IsModuleDLL(hModule))
  110.     {
  111.         if (lpInstData->pStackTop || lpInstData->pStackBottom ||
  112.             lpInstData->pStackMin)
  113.             printf("Error - DLLs don't have stacks!\n");
  114.         else
  115.             printf("DLL - no stack\n");
  116.     }
  117.     else
  118.     {
  119.         printf("Stack used =   %04x\n", 
  120.             lpInstData->pStackBottom - lpInstData->pStackMin);
  121.         printf("Stack size =   %04x\n",
  122.             lpInstData->pStackBottom - lpInstData->pStackTop);
  123.     }
  124.     
  125.     return 0;
  126. }
  127.  
  128. BOOL IsValidNULLSegment(HANDLE h)
  129. {
  130.     INSTDATA far *lpInstData;
  131.  
  132.     if (! verr(h))
  133.         return FALSE;
  134.     if (GetSelectorLimit(h) < 16)
  135.         return FALSE;
  136.     lpInstData = MK_FP(h, 0);
  137.     if (lpInstData->wMustBeZero != 0)
  138.         return FALSE;
  139.     if (lpInstData->pLocalHeap == 0)
  140.         return TRUE;    // it's ok to not have a local heap!
  141.     return IsValidLocalHeap(MK_FP(h, lpInstData->pLocalHeap));
  142. }
  143.  
  144. int axtoi(char *s)
  145. {
  146.     if (s[0]=='0' && s[1]=='x')
  147.     {
  148.         int ret;
  149.         sscanf(s+2, "%x", &ret);
  150.         return ret;
  151.     }
  152.     else
  153.         return atoi(s);
  154. }
  155.  
  156.