home *** CD-ROM | disk | FTP | other *** search
- /*
- SETSBASE.C -- Illustrates use of SetSelectorBase/Limit
-
- From Chapter 5 of "Undocumented Windows" (Addison-Wesley 1992)
- by Andrew Schulman, Dave Maxey and Matt Pietrek
-
- Note: If using Borland, precompile SETSBASE to use -B switch to
- remove error message generated by 'sgdt fword ptr es:[bx]' line
- below:
- bcc -2 -K -d -O -v -B -WS -w-par -ms -c -P-.C SETSBASE
- Then complete build using WINIOBC SETSBASE.
-
- Build using: WINIOMS SETSBASE (for Microsoft C/SDK Only)
- */
-
- #include <windows.h>
- #include "winio.h"
-
- /* undocumented functions */
- extern void FAR PASCAL SetSelectorBase(WORD sel, DWORD base);
- extern void FAR PASCAL SetSelectorLimit(WORD sel, DWORD limit);
-
- typedef char DESCRIPTOR[8]; /* just for purposes of this sample */
-
- typedef struct { WORD limit; DWORD base; } GDTR;
-
- /* C wrapper for the Intel SGDT instruction; must compile with
- 286 instructions (-G2 in Microsoft C; -2 in Borland C++).
- Places the Global Descriptor Table (GDT) base and limit into
- the six-byte (FWORD PTR) structure pointed to by pgdtr */
- void sgdt(GDTR far *pgdtr)
- {
- _asm les bx, pgdtr
- _asm sgdt fword ptr es:[bx]
- }
-
- main()
- {
- DESCRIPTOR far *gdt;
- GDTR gdtr;
- WORD sel;
-
- winio_about("SETSBASE"
- "\nIllustrates use of SetSelectorBase/Limit"
- "\n\nFrom Chapter 5 of"
- "\n\"Undocumented Windows\" (Addison-Wesley, 1992)"
- "\nby Andrew Schulman, David Maxey and Matt Pietrek"
- );
-
- /* get the linear base address and size (limit) of the Global
- Descriptor Table (GDT), using the Intel SGDT instruction */
- sgdt(&gdtr);
-
- /* allocate a selector similar to our current DS
- (i.e., a data selector) */
- _asm mov sel, ds
- if ((sel = AllocSelector(sel)) == 0)
- fail("Cannot allocate a selector!");
-
- /* set the base and limit of the new selector */
- SetSelectorBase(sel, gdtr.base);
- SetSelectorLimit(sel, gdtr.limit);
-
- /* we now have a selector that maps the GDT into our address
- space; create a far pointer from this selector */
- gdt = MK_FP(sel, 0);
-
- /* the program now has a far pointer to the GDT, and could
- manipulate it just like any other data. Here, we'll just
- print out some values */
- printf("GDT base=%08lx limit=%04x\n", gdtr.base, gdtr.limit);
- printf("GDT mapped as %Fp\n", gdt);
-
- /* when done, free the selector! */
- FreeSelector(sel);
- return 0;
- }
-