home *** CD-ROM | disk | FTP | other *** search
- 2/*
- HANDLES.H -- KERNEL-related handles
-
- Macros and functions (see HANDLES.C) for:
- Task Database handles (HTASK)
- Task Queue handles
- Module Database handles (HMODULE)
- Instance (DGROUP) handles
- Window handles (HWND)
- PSP/PDB
- Selector validation
-
- from "Undocumented Windows" by Schulman et al. (Addison-Wesley, 1992)
- Chapter 5: KERNEL
- Copyright (c) Andrew Schulman and Matt Pietrek 1992
- */
-
- #ifdef __cplusplus
- extern "C" {
- #endif
-
- #ifndef MK_FP
- #define MK_FP(s,o) ((void far *) (((DWORD) (s) << 16) | (o)))
- #endif
-
- /* Convert a handle to a selector */
- WORD HandleToSel(HANDLE h);
-
- /* Turn hTask into hModule: use WORD at offset 1Eh in the Task
- Database (TDB) */
- #define HMODULE_FROM_HTASK(hTask) \
- *((WORD far *) MK_FP(hTask, 0x1E))
-
- /* Get module handle for current task */
- #define GetCurrentModule() \
- HMODULE_FROM_HTASK(GetCurrentTask())
-
- /* Turn hTask into hInstance: use WORD at offset 1Ch in the TDB */
- #define HINSTANCE_FROM_HTASK(hTask) \
- *((WORD far *) MK_FP(hTask, 0x1C))
-
- /* Turn hTask into hWnd: a task can have more than one window;
- use the documented EnumTaskWindows() function */
-
- /* Turn hTask into PSP: use WORD at offset 60h in the TDB */
- #define PSP_FROM_HTASK(hTask) \
- *((WORD far *) MK_FP(hTask, 0x60))
-
- /* Turn hTask into hTaskQ: use the undocumented GetTaskQueue()
- function */
-
- /* Turn hInstance into hModule: use the GetInstanceModule()
- macro provided with the Windows 3.1 SDK (WINDOWSX.H), or use the
- undocumented GetExePtr() function. Remember that hInstance is
- just a task's default data segment (DGROUP). */
- #define HMODULE_FROM_HINSTANCE(hInstance) \
- GetModuleHandle(MK_FP(0, hInstance))
-
- /* Turn hInstance into hTask */
- WORD hTask_from_hInstance(WORD hInstance);
-
- /* Turn hInstance into hWnd: use the "Turn hInstance into an
- hTask" technique, then the "hTask into hWnd" technique */
-
- /* Turn hInstance into PSP: get hTask from hInstance, then PSP
- from hTask */
- #define PSP_FROM_HINSTANCE(hInstance) \
- PSP_FROM_HTASK(hTask_from_hInstance(hInstance))
-
- /* Turn hModule into hInstance: though note that this
- is a true function only for DLLs (see IsModuleDLL()) */
- WORD hInstance_from_hModule(WORD hModule);
-
- /* a useful synonym, though it might be more useful
- if it did HandleToSel also */
- #define GetModuleDgroup(hModule) \
- hInstance_from_hModule(hModule)
-
- /* Turn hModule into hTask: not always possible, as multiple tasks
- can share the same module table (see hModule into hInstance, above),
- and because DLL modules do not have an hTask. If the load count at
- offset 2 in the module table is 1, i.e.:
- *((WORD far *) MK_FP(hModule, 2)) == 1
- then the hTask can be obtained by first getting the hInstance for the
- module (see "hModule into hInstance," above), then getting the hTask
- from the hInstance (see "hInstance into hTask," above) */
-
- /* Turn hModule to hWnd: To obtain a list of windows associated with
- an hModule, first use the "hModule into hTask" technique above, then
- use the documented EnumTaskWindows() function to list the top-level
- windows */
-
- /* Turn hWnd into hTask: use the documented GetWindowTask() function */
-
- /* Turn hWnd into hModule: get hTask from hWnd, then hModule from hTask */
- #define HMODULE_FROM_HWND(hWnd) \
- HMODULE_FROM_HTASK(GetWindowTask(hWnd))
-
- /* Turn hWnd into hInstance: use the documented GetWindowWord(hwnd,
- GWW_HINSTANCE) call */
-
- /* Turn PDB (PSP) into hTask */
- HANDLE hTask_from_PSP(WORD wPSP);
-
- /* Turn PDB into hModule, hInstance, or hWnd: follow "PDB into hTask"
- instructions above, then convert hTask */
- #define HMODULE_FROM_PSP(wPSP) \
- HMODULE_FROM_HTASK(hTask_from_PSP(wPSP))
-
- #define HINSTANCE_FROM_PSP(wPSP) \
- HINSTANCE_FROM_HTASK(hTask_from_PSP(wPSP))
-
- /* Turn hTaskQ into hTask */
- #define HTASK_FROM_HTASKQ(hTaskQ) \
- *((WORD far *) MK_FP(hTaskQ, 2))
-
- /* Don't use hInstance field in Task Queue structure:
- moves from 3.0 to 3.1 */
- #define HINSTANCE_FROM_HTASKQ(hTaskQ) \
- HINSTANCE_FROM_HTASK(HTASK_FROM_HTASKQ(hTaskQ))
-
- /* Useful for getting name of sender from InSendMessage() */
- #define HMODULE_FROM_HTASKQ(hTaskQ) \
- HMODULE_FROM_HINSTANCE(HINSTANCE_FROM_HTASKQ(hTaskQ))
-
- /* Is handle for a DLL rather than a task? */
- BOOL IsModuleDLL(HANDLE hModule);
-
- /* C interface to protected-mode instructions; must compile
- with 286 instructions (Borland -2, Microsoft -G2) */
- BOOL verr(WORD wSel); // verify for reading
- BOOL verw(WORD wSel); // verify for writing
- WORD lsl(WORD wSel); // load segment limit
- WORD lar(WORD wSel); // load access rights
-
- /* for use with LAR */
- #define CODEDATA_MASK 8
- #define CODE 8
- #define DATA 0
-
- /* Are we using the 16-bit or 32-bit KERNEL?
- Returns 16 for KRNL286, 32 for KRNL386, 0 for real mode */
- int Kernel1632(void);
-
- /* Does the far pointer point to a valid local heap info struct? */
- BOOL IsValidLocalHeap(BYTE far *fp);
-
- /* Is this handle for a Module Database? */
- BOOL IsValidModuleHandle(HANDLE h);
-
- /* Is this handle for a DOS Program Segment Prefix (PSP; alias PDB)? */
- BOOL IsValidPSP(HANDLE h);
-
- /* Is this handle for a Task Database? 3.1 has documented
- IsTask(), but it's not in 3.0, so do our own */
- BOOL IsValidTask(HANDLE h);
-
- /* Not perfect, but a reasonable test: a Task Queue
- contains a valid task handle at offset 2 */
- #define IsTaskQueue(h) \
- IsValidTask(HTASK_FROM_HTASKQ(h))
-
- #ifdef __cplusplus
- }
- #endif
-
-