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

  1. /*
  2.     SELLIMIT.C -- Demonstration of GetSelectorLimit
  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 SELLIMIT (for Borland C++ v3.00)
  8.                  WINIOMS SELLIMIT (for Microsoft C/SDK)
  9. */
  10.  
  11. #include <windows.h>
  12. #include <stdlib.h>
  13. #include <dos.h>
  14. #include "winio.h"
  15.  
  16. extern DWORD FAR PASCAL GetSelectorLimit(WORD wSel);
  17. extern DWORD FAR PASCAL GetSelectorBase(WORD wSel);
  18. WORD (FAR PASCAL *GetTaskQueue)(WORD hTask);
  19. extern WORD FAR PASCAL SelectorAccessRights(WORD wSel, 
  20.     WORD wFlag, WORD wParam);
  21.  
  22. WORD lar(WORD wSel) // load access rights
  23. {
  24.     _asm lar ax, wSel
  25.     _asm jnz error
  26.     _asm shr ax, 8
  27.     _asm jmp short done; // value in AX
  28. error:
  29.     return 0;
  30. done:;
  31. }
  32.  
  33. DWORD MyGetSelectorSize(WORD wSel)
  34. {
  35.     DWORD dwSize;
  36.     WORD wRights;
  37.  
  38.     /* The Windows Get/SetSelectorBase/Limit functions can't
  39.        handle selectors in the GDT */
  40.     if ((wSel & 4) == 0)
  41.         return 0L;              // not an LDT selector
  42.  
  43.     /* Unfortunately, SelectorAccessRights() does not check the Zero
  44.        flag after doing a LAR, so it can't be used to check if a
  45.        selector is valid -- we'll use LAR outselves to check */
  46.     if ((wRights = lar(wSel)) == 0)
  47.         return 0L;              // invalid selector
  48.     
  49.     /* Add one to limit to get size */
  50.     dwSize = GetSelectorLimit(wSel) + 1;
  51.  
  52.     /* Now we can use SelectorAccessRights to see if this (valid)
  53.        selector has Page granularity */
  54.     wRights = SelectorAccessRights(wSel, 0, 0);
  55.     if (wRights & (1 << 15))    // page granularity bit set
  56.         dwSize *= 4096;         // size was pages; turn into bytes
  57.             
  58.     return dwSize;
  59. }
  60.  
  61. void show_size(char *msg, HANDLE h)
  62. {
  63.     DWORD dwSize;
  64.     if ((dwSize = MyGetSelectorSize(h)) == 0)
  65.         printf("%s INVALID OR NOT IN LDT\n", msg);
  66.     else
  67.     {
  68.         DWORD dwBase = GetSelectorBase(h);
  69.         printf("%s size=%lu bytes @ %lxh\n", msg, dwSize, dwBase);
  70.     }
  71. }
  72.  
  73. main(int argc, char *argv[])
  74. {
  75.     extern WORD __hInst; 
  76.  
  77.     HANDLE h = GlobalAlloc(GMEM_MOVEABLE, 1);
  78.     char far *fp = GlobalLock(h);
  79.  
  80.     winio_about("SELLIMIT"
  81.         "\nDemonstration of GetSelectorLimit"
  82.         "\n\nFrom Chapter 5 of"
  83.         "\n\"Undocumented Windows\" (Addison-Wesley, 1992)"
  84.         "\nby Andrew Schulman, David Maxey and Matt Pietrek"
  85.         );
  86.     
  87.     show_size("GlobalAlloc=1 byte; actually", FP_SEG(fp));
  88.     GlobalUnlock(h);
  89.     GlobalFree(h);
  90.  
  91.     show_size("Task Database", GetCurrentTask());
  92.  
  93.     show_size("PSP (PDB)", GetCurrentPDB());
  94.  
  95.     GetTaskQueue = GetProcAddress(GetModuleHandle("KERNEL"), "GETTASKQUEUE");
  96.     show_size("Task Queue", GetTaskQueue(0));   // q for current task
  97.  
  98.     show_size("Module Table", 
  99.         GetModuleHandle(MK_FP(0, __hInst))); // hModule from hInstance
  100.             
  101.     return 0;
  102. }
  103.  
  104.