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.

128 lines
2.4 KiB

  1. /*++
  2. Copyright (c) 1995 Microsoft Corporation
  3. Module Name:
  4. proc.c
  5. Abstract:
  6. Code for dumping information about processes using the NT API rather than
  7. the win32 API.
  8. Environment:
  9. User mode only
  10. Revision History:
  11. 03-26-96 : Created
  12. --*/
  13. //
  14. // this module may be compiled at warning level 4 with the following
  15. // warnings disabled:
  16. //
  17. #pragma warning(disable:4200) // array[0]
  18. #pragma warning(disable:4201) // nameless struct/unions
  19. #pragma warning(disable:4214) // bit fields other than int
  20. #include <nt.h>
  21. #include <ntrtl.h>
  22. #include <nturtl.h>
  23. #include <windows.h>
  24. #include <string.h>
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <assert.h>
  28. #define PROCESS_BUFFER_INCREMENT (16 * 4096)
  29. PSYSTEM_PROCESS_INFORMATION ProcessInfo = NULL;
  30. DWORD ProcessInfoLength;
  31. VOID
  32. GetAllProcessInfo(
  33. VOID
  34. )
  35. {
  36. PSYSTEM_PROCESS_INFORMATION buffer;
  37. DWORD bufferSize = 1 * PROCESS_BUFFER_INCREMENT;
  38. NTSTATUS status;
  39. assert(ProcessInfo == NULL);
  40. do {
  41. buffer = LocalAlloc(LMEM_FIXED, bufferSize);
  42. if(buffer == NULL) {
  43. return;
  44. }
  45. status = NtQuerySystemInformation(SystemProcessInformation,
  46. buffer,
  47. bufferSize,
  48. &ProcessInfoLength);
  49. if(status == STATUS_INFO_LENGTH_MISMATCH) {
  50. LocalFree(buffer);
  51. bufferSize += PROCESS_BUFFER_INCREMENT;
  52. continue;
  53. }
  54. } while(status == STATUS_INFO_LENGTH_MISMATCH);
  55. if(NT_SUCCESS(status)) {
  56. ProcessInfo = buffer;
  57. }
  58. }
  59. VOID
  60. PrintProcessInfo(
  61. DWORD_PTR ProcessId
  62. )
  63. {
  64. PSYSTEM_PROCESS_INFORMATION info;
  65. if(ProcessInfo == NULL) {
  66. return;
  67. }
  68. info = ProcessInfo;
  69. do {
  70. if(ProcessId == (DWORD_PTR) info->UniqueProcessId) {
  71. printf(": %.*S",
  72. (info->ImageName.Length / sizeof(WCHAR)),
  73. info->ImageName.Buffer);
  74. break;
  75. }
  76. info = (PSYSTEM_PROCESS_INFORMATION) (((ULONG_PTR) info) +
  77. info->NextEntryOffset);
  78. } while((ULONG_PTR) info <= (ULONG_PTR) ProcessInfo + ProcessInfoLength);
  79. }
  80. VOID
  81. FreeProcessInfo(
  82. VOID
  83. )
  84. {
  85. LocalFree(ProcessInfo);
  86. ProcessInfo = NULL;
  87. }