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

  1. 2/* 
  2. HANDLES.H -- KERNEL-related handles
  3.  
  4. Macros and functions (see HANDLES.C) for:
  5.     Task Database handles (HTASK)
  6.     Task Queue handles
  7.     Module Database handles (HMODULE)
  8.     Instance (DGROUP) handles
  9.     Window handles (HWND)
  10.     PSP/PDB
  11.     Selector validation 
  12.         
  13. from "Undocumented Windows" by Schulman et al. (Addison-Wesley, 1992)
  14. Chapter 5: KERNEL
  15. Copyright (c) Andrew Schulman and Matt Pietrek 1992
  16. */
  17.  
  18. #ifdef __cplusplus
  19. extern "C" {
  20. #endif
  21.  
  22. #ifndef MK_FP
  23. #define MK_FP(s,o) ((void far *) (((DWORD) (s) << 16) | (o)))
  24. #endif
  25.  
  26. /* Convert a handle to a selector */
  27. WORD HandleToSel(HANDLE h);
  28.  
  29. /* Turn hTask into hModule:  use WORD at offset 1Eh in the Task
  30. Database (TDB) */
  31. #define HMODULE_FROM_HTASK(hTask) \
  32.     *((WORD far *) MK_FP(hTask, 0x1E))
  33.         
  34. /* Get module handle for current task */
  35. #define GetCurrentModule() \
  36.     HMODULE_FROM_HTASK(GetCurrentTask())
  37.  
  38. /* Turn hTask into hInstance:  use WORD at offset 1Ch in the TDB */
  39. #define HINSTANCE_FROM_HTASK(hTask) \
  40.     *((WORD far *) MK_FP(hTask, 0x1C))
  41.  
  42. /* Turn hTask into hWnd:  a task can have more than one window;
  43. use the documented EnumTaskWindows() function */
  44.  
  45. /* Turn hTask into PSP:  use WORD at offset 60h in the TDB */
  46. #define PSP_FROM_HTASK(hTask) \
  47.     *((WORD far *) MK_FP(hTask, 0x60))
  48.         
  49. /* Turn hTask into hTaskQ:  use the undocumented GetTaskQueue()
  50. function */
  51.  
  52. /* Turn hInstance into hModule:  use the GetInstanceModule()
  53. macro provided with the Windows 3.1 SDK (WINDOWSX.H), or use the 
  54. undocumented GetExePtr() function.  Remember that hInstance is 
  55. just a task's default data segment (DGROUP). */
  56. #define HMODULE_FROM_HINSTANCE(hInstance) \
  57.     GetModuleHandle(MK_FP(0, hInstance))
  58.         
  59. /* Turn hInstance into hTask */
  60. WORD hTask_from_hInstance(WORD hInstance);
  61.  
  62. /* Turn hInstance into hWnd:  use the "Turn hInstance into an
  63. hTask" technique, then the "hTask into hWnd" technique */
  64.         
  65. /* Turn hInstance into PSP:  get hTask from hInstance, then PSP
  66. from hTask */
  67. #define PSP_FROM_HINSTANCE(hInstance) \
  68.     PSP_FROM_HTASK(hTask_from_hInstance(hInstance))
  69.  
  70. /* Turn hModule into hInstance: though note that this
  71.    is a true function only for DLLs (see IsModuleDLL()) */
  72. WORD hInstance_from_hModule(WORD hModule);
  73.  
  74. /* a useful synonym, though it might be more useful
  75.    if it did HandleToSel also */
  76. #define GetModuleDgroup(hModule) \
  77.     hInstance_from_hModule(hModule)
  78.  
  79. /* Turn hModule into hTask: not always possible, as multiple tasks
  80. can share the same module table (see hModule into hInstance, above),
  81. and because DLL modules do not have an hTask.  If the load count at
  82. offset 2 in the module table is 1, i.e.:
  83.     *((WORD far *) MK_FP(hModule, 2)) == 1
  84. then the hTask can be obtained by first getting the hInstance for the
  85. module (see "hModule into hInstance," above), then getting the hTask
  86. from the hInstance (see "hInstance into hTask," above) */
  87.  
  88. /* Turn hModule to hWnd:  To obtain a list of windows associated with
  89. an hModule, first use the "hModule into hTask" technique above, then
  90. use the documented EnumTaskWindows() function to list the top-level
  91. windows */
  92.  
  93. /* Turn hWnd into hTask:  use the documented GetWindowTask() function */
  94.  
  95. /* Turn hWnd into hModule:  get hTask from hWnd, then hModule from hTask */
  96. #define HMODULE_FROM_HWND(hWnd) \
  97.     HMODULE_FROM_HTASK(GetWindowTask(hWnd))
  98.  
  99. /* Turn hWnd into hInstance:  use the documented GetWindowWord(hwnd,
  100. GWW_HINSTANCE) call */
  101.  
  102. /* Turn PDB (PSP) into hTask */
  103. HANDLE hTask_from_PSP(WORD wPSP);
  104.  
  105. /* Turn PDB into hModule, hInstance, or hWnd:  follow "PDB into hTask"
  106. instructions above, then convert hTask */
  107. #define HMODULE_FROM_PSP(wPSP) \
  108.     HMODULE_FROM_HTASK(hTask_from_PSP(wPSP))
  109.  
  110. #define HINSTANCE_FROM_PSP(wPSP) \
  111.     HINSTANCE_FROM_HTASK(hTask_from_PSP(wPSP))
  112.         
  113. /* Turn hTaskQ into hTask */
  114. #define HTASK_FROM_HTASKQ(hTaskQ) \
  115.     *((WORD far *) MK_FP(hTaskQ, 2))
  116.         
  117. /* Don't use hInstance field in Task Queue structure:
  118.    moves from 3.0 to 3.1 */
  119. #define HINSTANCE_FROM_HTASKQ(hTaskQ) \
  120.     HINSTANCE_FROM_HTASK(HTASK_FROM_HTASKQ(hTaskQ))
  121.         
  122. /* Useful for getting name of sender from InSendMessage() */
  123. #define HMODULE_FROM_HTASKQ(hTaskQ) \
  124.     HMODULE_FROM_HINSTANCE(HINSTANCE_FROM_HTASKQ(hTaskQ))
  125.         
  126. /* Is handle for a DLL rather than a task? */
  127. BOOL IsModuleDLL(HANDLE hModule);
  128.  
  129. /* C interface to protected-mode instructions; must compile 
  130.    with 286 instructions (Borland -2, Microsoft -G2) */
  131. BOOL verr(WORD wSel);   // verify for reading
  132. BOOL verw(WORD wSel);   // verify for writing
  133. WORD lsl(WORD wSel);    // load segment limit
  134. WORD lar(WORD wSel);    // load access rights
  135.  
  136. /* for use with LAR */
  137. #define CODEDATA_MASK   8
  138. #define CODE            8
  139. #define DATA            0
  140.  
  141. /* Are we using the 16-bit or 32-bit KERNEL?  
  142.    Returns 16 for KRNL286, 32 for KRNL386, 0 for real mode */
  143. int Kernel1632(void);
  144.  
  145. /* Does the far pointer point to a valid local heap info struct? */
  146. BOOL IsValidLocalHeap(BYTE far *fp);
  147.  
  148. /* Is this handle for a Module Database? */
  149. BOOL IsValidModuleHandle(HANDLE h);
  150.  
  151. /* Is this handle for a DOS Program Segment Prefix (PSP; alias PDB)? */
  152. BOOL IsValidPSP(HANDLE h);
  153.  
  154. /* Is this handle for a Task Database?  3.1 has documented 
  155.    IsTask(), but it's not in 3.0, so do our own */
  156. BOOL IsValidTask(HANDLE h);
  157.  
  158. /* Not perfect, but a reasonable test:  a Task Queue
  159.    contains a valid task handle at offset 2 */
  160. #define IsTaskQueue(h) \
  161.     IsValidTask(HTASK_FROM_HTASKQ(h))
  162.  
  163. #ifdef __cplusplus
  164. }
  165. #endif
  166.  
  167.