Source code of Windows XP (NT5)
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.

209 lines
4.0 KiB

  1. /*++
  2. Copyright (c) 1995-1997 Microsoft Corporation
  3. Module Name:
  4. enummod.c
  5. Abstract:
  6. This module implements a remote module enumerator.
  7. Author:
  8. Keith Moore (keithmo) 16-Sep-1997
  9. Revision History:
  10. --*/
  11. #include <nt.h>
  12. #include <ntrtl.h>
  13. #include <nturtl.h>
  14. #undef IF_DEBUG
  15. #include <windows.h>
  16. //#include <ntsdexts.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <wchar.h>
  21. # include <stdlib.h>
  22. #include "inetdbgp.h"
  23. #define ReadMemory(a,b,c,d) ReadProcessMemory( ExtensionCurrentProcess, (LPCVOID)(a), (b), (c), (d) )
  24. #define WriteMemory(a,b,c,d) WriteProcessMemory( ExtensionCurrentProcess, (LPVOID)(a), (LPVOID)(b), (c), (d) )
  25. //
  26. // Globals
  27. //
  28. BOOLEAN
  29. EnumModules(
  30. IN HANDLE ExtensionCurrentProcess,
  31. IN PFN_ENUMMODULES EnumProc,
  32. IN PVOID Param
  33. )
  34. /*++
  35. Routine Description:
  36. Enumerates all loaded modules in the debugee.
  37. Arguments:
  38. EnumProc - An enumeration proc that will be invoked for each module.
  39. Param - An uninterpreted parameter passed to the enumeration proc.
  40. Return Value:
  41. BOOLEAN - TRUE if successful, FALSE otherwise.
  42. --*/
  43. {
  44. PROCESS_BASIC_INFORMATION basicInfo;
  45. NTSTATUS status;
  46. PPEB peb;
  47. PPEB_LDR_DATA ldr;
  48. PLIST_ENTRY ldrHead, ldrNext;
  49. PLDR_DATA_TABLE_ENTRY ldrEntry;
  50. LDR_DATA_TABLE_ENTRY ldrEntryData;
  51. WCHAR tmpName[MAX_PATH];
  52. MODULE_INFO moduleInfo;
  53. //
  54. // Get the process info.
  55. //
  56. status = NtQueryInformationProcess(
  57. ExtensionCurrentProcess,
  58. ProcessBasicInformation,
  59. &basicInfo,
  60. sizeof(basicInfo),
  61. NULL
  62. );
  63. if( !NT_SUCCESS(status) ) {
  64. return FALSE;
  65. }
  66. peb = basicInfo.PebBaseAddress;
  67. if( peb == NULL ) {
  68. return FALSE;
  69. }
  70. //
  71. // ldr = peb->Ldr
  72. //
  73. if( !ReadMemory(
  74. (ULONG_PTR)&peb->Ldr,
  75. &ldr,
  76. sizeof(ldr),
  77. NULL
  78. ) ) {
  79. return FALSE;
  80. }
  81. ldrHead = &ldr->InMemoryOrderModuleList;
  82. //
  83. // ldrNext = ldrHead->Flink;
  84. //
  85. if( !ReadMemory(
  86. (ULONG_PTR)&ldrHead->Flink,
  87. &ldrNext,
  88. sizeof(ldrNext),
  89. NULL
  90. ) ) {
  91. return FALSE;
  92. }
  93. while( ldrNext != ldrHead ) {
  94. #if 0
  95. if( CheckControlC() ) {
  96. break;
  97. }
  98. #endif
  99. //
  100. // Read the LDR_DATA_TABLE_ENTRY structure and the module name.
  101. //
  102. ldrEntry = CONTAINING_RECORD(
  103. ldrNext,
  104. LDR_DATA_TABLE_ENTRY,
  105. InMemoryOrderLinks
  106. );
  107. if( !ReadMemory(
  108. (ULONG_PTR)ldrEntry,
  109. &ldrEntryData,
  110. sizeof(ldrEntryData),
  111. NULL
  112. ) ) {
  113. return FALSE;
  114. }
  115. if( !ReadMemory(
  116. (ULONG_PTR)ldrEntryData.BaseDllName.Buffer,
  117. tmpName,
  118. ldrEntryData.BaseDllName.MaximumLength,
  119. NULL
  120. ) ) {
  121. return FALSE;
  122. }
  123. wsprintfA(
  124. moduleInfo.BaseName,
  125. "%ws",
  126. tmpName
  127. );
  128. if( !ReadMemory(
  129. (ULONG_PTR)ldrEntryData.FullDllName.Buffer,
  130. tmpName,
  131. ldrEntryData.FullDllName.MaximumLength,
  132. NULL
  133. ) ) {
  134. return FALSE;
  135. }
  136. wsprintfA(
  137. moduleInfo.FullName,
  138. "%ws",
  139. tmpName
  140. );
  141. moduleInfo.DllBase = (ULONG_PTR)ldrEntryData.DllBase;
  142. moduleInfo.EntryPoint = (ULONG_PTR)ldrEntryData.EntryPoint;
  143. moduleInfo.SizeOfImage = (ULONG)ldrEntryData.SizeOfImage;
  144. //
  145. // Invoke the callback.
  146. //
  147. if( !(EnumProc)(
  148. Param,
  149. &moduleInfo
  150. ) ) {
  151. break;
  152. }
  153. ldrNext = ldrEntryData.InMemoryOrderLinks.Flink;
  154. }
  155. return TRUE;
  156. } // EnumModules