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.

154 lines
4.1 KiB

  1. /*++
  2. Copyright (c) Microsoft Corporation. All rights reserved.
  3. Module Name:
  4. request.c
  5. Abstract:
  6. Implements WMI requests to different data providers
  7. Author:
  8. 16-Jan-1997 AlanWar
  9. Revision History:
  10. --*/
  11. #include "wmiump.h"
  12. #include "request.h"
  13. ULONG EtwpSendWmiKMRequest(
  14. HANDLE DeviceHandle,
  15. ULONG Ioctl,
  16. PVOID InBuffer,
  17. ULONG InBufferSize,
  18. PVOID OutBuffer,
  19. ULONG MaxBufferSize,
  20. ULONG *ReturnSize,
  21. LPOVERLAPPED Overlapped
  22. )
  23. /*+++
  24. Routine Description:
  25. This routine does the work of sending WMI requests to the WMI kernel
  26. mode device. Any retry errors returned by the WMI device are handled
  27. in this routine.
  28. Arguments:
  29. Ioctl is the IOCTL code to send to the WMI device
  30. Buffer is the input buffer for the call to the WMI device
  31. InBufferSize is the size of the buffer passed to the device
  32. OutBuffer is the output buffer for the call to the WMI device
  33. MaxBufferSize is the maximum number of bytes that can be written
  34. into the buffer
  35. *ReturnSize on return has the actual number of bytes written in buffer
  36. Overlapped is an option OVERLAPPED struct that is used to make the
  37. call async
  38. Return Value:
  39. ERROR_SUCCESS or an error code
  40. ---*/
  41. {
  42. OVERLAPPED StaticOverlapped;
  43. ULONG Status;
  44. BOOL IoctlSuccess;
  45. EtwpEnterPMCritSection();
  46. if (EtwpKMHandle == NULL)
  47. {
  48. //
  49. // If device is not open for then open it now. The
  50. // handle is closed in the process detach dll callout (DlllMain)
  51. EtwpKMHandle = CreateFile(WMIDataDeviceName,
  52. GENERIC_READ | GENERIC_WRITE,
  53. 0,
  54. NULL,
  55. OPEN_EXISTING,
  56. FILE_ATTRIBUTE_NORMAL |
  57. FILE_FLAG_OVERLAPPED,
  58. NULL);
  59. if (EtwpKMHandle == (HANDLE)-1)
  60. {
  61. EtwpKMHandle = NULL;
  62. EtwpLeavePMCritSection();
  63. return(GetLastError());
  64. }
  65. }
  66. EtwpLeavePMCritSection();
  67. if (Overlapped == NULL)
  68. {
  69. //
  70. // if caller didn't pass an overlapped structure then supply
  71. // our own and make the call synchronous
  72. //
  73. Overlapped = &StaticOverlapped;
  74. Overlapped->hEvent = EtwpAllocEvent();
  75. if (Overlapped->hEvent == NULL)
  76. {
  77. return(ERROR_NOT_ENOUGH_MEMORY);
  78. }
  79. }
  80. if (DeviceHandle == NULL)
  81. {
  82. DeviceHandle = EtwpKMHandle;
  83. }
  84. do
  85. {
  86. IoctlSuccess = DeviceIoControl(DeviceHandle,
  87. Ioctl,
  88. InBuffer,
  89. InBufferSize,
  90. OutBuffer,
  91. MaxBufferSize,
  92. ReturnSize,
  93. Overlapped);
  94. if (!IoctlSuccess)
  95. {
  96. if (Overlapped == &StaticOverlapped)
  97. {
  98. //
  99. // if the call was successful and we are synchronous then
  100. // block until the call completes
  101. //
  102. if (GetLastError() == ERROR_IO_PENDING)
  103. {
  104. IoctlSuccess = GetOverlappedResult(DeviceHandle,
  105. Overlapped,
  106. ReturnSize,
  107. TRUE);
  108. }
  109. if (! IoctlSuccess)
  110. {
  111. Status = GetLastError();
  112. } else {
  113. Status = ERROR_SUCCESS;
  114. }
  115. } else {
  116. Status = GetLastError();
  117. }
  118. } else {
  119. Status = ERROR_SUCCESS;
  120. }
  121. } while (Status == ERROR_WMI_TRY_AGAIN);
  122. if (Overlapped == &StaticOverlapped)
  123. {
  124. EtwpFreeEvent(Overlapped->hEvent);
  125. }
  126. return(Status);
  127. }