Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

156 lines
4.5 KiB

  1. /*************************************************************************
  2. * MODULE.C
  3. *
  4. * Routines to enumerate the various module headers on the module
  5. * chain.
  6. *
  7. *************************************************************************/
  8. #include "toolpriv.h"
  9. #include <newexe.h>
  10. #include <string.h>
  11. /* ----- Function prototypes ----- */
  12. NOEXPORT BOOL PASCAL ModuleGetInfo(
  13. WORD wModule,
  14. MODULEENTRY FAR *lpModule);
  15. /* ModuleFirst
  16. * Finds the first module in the module list and returns information
  17. * about this module.
  18. */
  19. BOOL TOOLHELPAPI ModuleFirst(
  20. MODULEENTRY FAR *lpModule)
  21. {
  22. WORD FAR *lpwExeHead;
  23. /* Check the version number and verify proper installation */
  24. if (!wLibInstalled || !lpModule ||
  25. lpModule->dwSize != sizeof (MODULEENTRY))
  26. return FALSE;
  27. /* Get a pointer to the module head */
  28. lpwExeHead = MAKEFARPTR(segKernel, npwExeHead);
  29. /* Use this pointer to get information about this module */
  30. return ModuleGetInfo(*lpwExeHead, lpModule);
  31. }
  32. /* ModuleNext
  33. * Finds the next module in the module list.
  34. */
  35. BOOL TOOLHELPAPI ModuleNext(
  36. MODULEENTRY FAR *lpModule)
  37. {
  38. /* Check the version number and verify proper installation */
  39. if (!wLibInstalled || !lpModule ||
  40. lpModule->dwSize != sizeof (MODULEENTRY))
  41. return FALSE;
  42. /* Use the next handle to get information about this module */
  43. return ModuleGetInfo(lpModule->wNext, lpModule);
  44. }
  45. /* ModuleFindName
  46. * Finds a module with the given module name and returns information
  47. * about it.
  48. */
  49. HANDLE TOOLHELPAPI ModuleFindName(
  50. MODULEENTRY FAR *lpModule,
  51. LPCSTR lpstrName)
  52. {
  53. /* Check the version number and verify proper installation */
  54. if (!wLibInstalled || !lpModule || !lpstrName ||
  55. lpModule->dwSize != sizeof (MODULEENTRY))
  56. return NULL;
  57. /* Loop through module chain until we find the name (or maybe we don't) */
  58. if (ModuleFirst(lpModule))
  59. do
  60. {
  61. /* Is this the name? If so, we have the info, so return */
  62. if (!lstrcmp(lpstrName, lpModule->szModule))
  63. return lpModule->hModule;
  64. }
  65. while (ModuleNext(lpModule));
  66. /* If we get here, we didn't find it or there was an error */
  67. return NULL;
  68. }
  69. /* ModuleFindHandle
  70. * Returns information about a module with the given handle.
  71. */
  72. HANDLE TOOLHELPAPI ModuleFindHandle(
  73. MODULEENTRY FAR *lpModule,
  74. HANDLE hModule)
  75. {
  76. /* Check the version number and verify proper installation */
  77. if (!wLibInstalled || !lpModule || !hModule ||
  78. lpModule->dwSize != sizeof (MODULEENTRY))
  79. return NULL;
  80. /* Use the helper function to find out about this module */
  81. if (!ModuleGetInfo(hModule, lpModule))
  82. return NULL;
  83. return lpModule->hModule;
  84. }
  85. /* ----- Helper functions ----- */
  86. NOEXPORT BOOL PASCAL ModuleGetInfo(
  87. WORD wModule,
  88. MODULEENTRY FAR *lpModule)
  89. {
  90. struct new_exe FAR *lpNewExe;
  91. BYTE FAR *lpb;
  92. /* Verify the segment so we don't GP fault */
  93. if (!HelperVerifySeg(wModule, 2))
  94. return FALSE;
  95. /* Get a pointer to the module database */
  96. lpNewExe = MAKEFARPTR(wModule, 0);
  97. /* Make sure this is a module database */
  98. if (lpNewExe->ne_magic != NEMAGIC)
  99. return FALSE;
  100. /* Get the module name (it's the first name in the resident names
  101. * table
  102. */
  103. lpb = ((BYTE FAR *)lpNewExe) + lpNewExe->ne_restab;
  104. _fstrncpy(lpModule->szModule, lpb + 1, *lpb);
  105. lpModule->szModule[*lpb] = '\0';
  106. /* Get the EXE file path. A pointer is stored in the same place as
  107. * the high word of the CRC was in the EXE file. (6th word in new_exe)
  108. * This pointer points to the length of a PASCAL string whose first
  109. * eight characters are meaningless to us.
  110. */
  111. lpb = MAKEFARPTR(wModule, *(((WORD FAR *)lpNewExe) + 5));
  112. _fstrncpy(lpModule->szExePath, lpb + 8, *lpb - 8);
  113. lpModule->szExePath[*lpb - 8] = '\0';
  114. /* Get other information from the EXE Header
  115. * The usage count is stored in the second word of the EXE header
  116. * The handle of the next module in the chain is stored in the
  117. * ne_cbenttab structure member.
  118. */
  119. lpModule->hModule = wModule;
  120. lpModule->wcUsage = *(((WORD FAR *)lpNewExe) + 1);
  121. lpModule->wNext = lpNewExe->ne_cbenttab;
  122. return TRUE;
  123. }