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.

120 lines
2.4 KiB

  1. /*++
  2. Copyright (c) 1994 Microsoft Corporation
  3. Module Name:
  4. handle.cxx
  5. Abstract:
  6. Contains function to return number of open handles owned by this process
  7. Contents:
  8. InternetHandleCount
  9. Author:
  10. Richard L Firth (rfirth) 02-May-1995
  11. Environment:
  12. Win32 user-mode DLL
  13. Revision History:
  14. 02-May-1995 rfirth
  15. Created
  16. --*/
  17. #include <wininetp.h>
  18. #include "rprintf.h"
  19. #if INET_DEBUG
  20. //
  21. // private types
  22. //
  23. typedef (*NT_QUERY_SYSTEM_INFORMATION)(ULONG, PVOID, ULONG, PULONG);
  24. //
  25. // functions
  26. //
  27. DWORD
  28. InternetHandleCount(
  29. VOID
  30. )
  31. /*++
  32. Routine Description:
  33. Gets the number of system handles owned by this process. We LoadLibrary()
  34. NTDLL.DLL so that the debug version of this DLL still works on Win95
  35. Arguments:
  36. None.
  37. Return Value:
  38. DWORD
  39. --*/
  40. {
  41. static HINSTANCE hNtdll = NULL;
  42. static NT_QUERY_SYSTEM_INFORMATION _NtQuerySystemInformation;
  43. if (IsPlatformWin95()) {
  44. return 0;
  45. }
  46. if (hNtdll == NULL) {
  47. hNtdll = LoadLibrary("ntdll");
  48. if (hNtdll == NULL) {
  49. return 0;
  50. }
  51. _NtQuerySystemInformation = (NT_QUERY_SYSTEM_INFORMATION)GetProcAddress(hNtdll, "NtQuerySystemInformation");
  52. if (_NtQuerySystemInformation == 0) {
  53. FreeLibrary(hNtdll);
  54. hNtdll = NULL;
  55. }
  56. }
  57. if (_NtQuerySystemInformation) {
  58. DWORD idProcess;
  59. NTSTATUS status;
  60. ULONG outputLength;
  61. BYTE buffer[32768];
  62. PSYSTEM_PROCESS_INFORMATION info;
  63. status = _NtQuerySystemInformation(SystemProcessInformation,
  64. (PVOID)buffer,
  65. sizeof(buffer),
  66. &outputLength
  67. );
  68. if (!NT_SUCCESS(status)) {
  69. return 0;
  70. }
  71. info = (PSYSTEM_PROCESS_INFORMATION)buffer;
  72. idProcess = GetCurrentProcessId();
  73. while (TRUE) {
  74. if ((DWORD_PTR)info->UniqueProcessId == idProcess) {
  75. return info->HandleCount;
  76. }
  77. if (info->NextEntryOffset == 0) {
  78. return 0;
  79. }
  80. info = (PSYSTEM_PROCESS_INFORMATION)((PCHAR)info + info->NextEntryOffset);
  81. }
  82. }
  83. return 0;
  84. }
  85. #endif // INET_DEBUG