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

  1. /*
  2.     SETSBASE.C -- Illustrates use of SetSelectorBase/Limit
  3.  
  4.     From Chapter 5 of "Undocumented Windows" (Addison-Wesley 1992)
  5.     by Andrew Schulman, Dave Maxey and Matt Pietrek
  6.     
  7.     Note: If using Borland, precompile SETSBASE to use -B switch to
  8.     remove error message generated by 'sgdt fword ptr es:[bx]' line
  9.     below:
  10.     bcc -2 -K -d -O -v -B -WS -w-par -ms -c -P-.C SETSBASE
  11.     Then complete build using WINIOBC SETSBASE.
  12.     
  13.     Build using: WINIOMS SETSBASE (for Microsoft C/SDK Only)
  14. */
  15.  
  16. #include <windows.h>
  17. #include "winio.h"
  18.  
  19. /* undocumented functions */
  20. extern void FAR PASCAL SetSelectorBase(WORD sel, DWORD base);
  21. extern void FAR PASCAL SetSelectorLimit(WORD sel, DWORD limit);
  22.  
  23. typedef char DESCRIPTOR[8];  /* just for purposes of this sample */
  24.  
  25. typedef struct { WORD limit; DWORD base; } GDTR;
  26.     
  27. /* C wrapper for the Intel SGDT instruction; must compile with
  28.    286 instructions (-G2 in Microsoft C; -2 in Borland C++). 
  29.    Places the Global Descriptor Table (GDT) base and limit into
  30.    the six-byte (FWORD PTR) structure pointed to by pgdtr */
  31. void sgdt(GDTR far *pgdtr)
  32. {
  33.     _asm les bx, pgdtr
  34.     _asm sgdt fword ptr es:[bx]
  35. }
  36.  
  37. main()
  38. {
  39.     DESCRIPTOR far *gdt;
  40.     GDTR gdtr;
  41.     WORD sel;
  42.  
  43.     winio_about("SETSBASE"
  44.         "\nIllustrates use of SetSelectorBase/Limit"
  45.         "\n\nFrom Chapter 5 of"
  46.         "\n\"Undocumented Windows\" (Addison-Wesley, 1992)"
  47.         "\nby Andrew Schulman, David Maxey and Matt Pietrek"
  48.         );
  49.     
  50.     /* get the linear base address and size (limit) of the Global
  51.        Descriptor Table (GDT), using the Intel SGDT instruction */
  52.     sgdt(&gdtr);
  53.  
  54.     /* allocate a selector similar to our current DS 
  55.        (i.e., a data selector) */
  56.     _asm mov sel, ds
  57.     if ((sel = AllocSelector(sel)) == 0)
  58.         fail("Cannot allocate a selector!");
  59.     
  60.     /* set the base and limit of the new selector */
  61.     SetSelectorBase(sel, gdtr.base);
  62.     SetSelectorLimit(sel, gdtr.limit);
  63.     
  64.     /* we now have a selector that maps the GDT into our address
  65.        space; create a far pointer from this selector */
  66.     gdt = MK_FP(sel, 0);
  67.     
  68.     /* the program now has a far pointer to the GDT, and could 
  69.        manipulate it just like any other data. Here, we'll just
  70.        print out some values */
  71.     printf("GDT base=%08lx limit=%04x\n", gdtr.base, gdtr.limit);
  72.     printf("GDT mapped as %Fp\n", gdt);
  73.         
  74.     /* when done, free the selector! */
  75.     FreeSelector(sel);
  76.     return 0;
  77. }
  78.