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.

203 lines
6.9 KiB

  1. #include "pch.h"
  2. #pragma hdrstop
  3. #include <ndisguid.h>
  4. #include "afilexp.h"
  5. #include "edc.h"
  6. #include "lm.h"
  7. #include "nceh.h"
  8. #include "ncerror.h"
  9. #include "ncmisc.h"
  10. #include "ncnetcfg.h"
  11. #include "ncreg.h"
  12. #include "ncsvc.h"
  13. #include "ncsetup.h"
  14. #include "ncatlui.h"
  15. #include "netcfgn.h"
  16. #include "netsetup.h"
  17. #include "nslog.h"
  18. #include "nsres.h"
  19. #include "resource.h"
  20. #include "upgrade.h"
  21. #include "windns.h"
  22. #include "winstall.h"
  23. #include <hnetcfg.h>
  24. #include "icsupgrd.h"
  25. static const WCHAR szBefore[] = L"Before";
  26. static const WCHAR szWMI[] = L"WMI";
  27. static const WCHAR szAfter[] = L"After";
  28. void DumpDependencyList(
  29. const WCHAR * Name,
  30. PWCHAR List
  31. )
  32. {
  33. PWCHAR p;
  34. TraceTag(ttidWmi,
  35. "%ws Dependency List",
  36. Name);
  37. p = List;
  38. while (*p != 0)
  39. {
  40. TraceTag(ttidWmi,
  41. " %ws",
  42. p);
  43. while (*p++ != 0) ;
  44. }
  45. }
  46. void FixWmiServiceDependencies(
  47. const WCHAR *ServiceName
  48. )
  49. {
  50. SC_HANDLE ScmHandle, ServiceHandle;
  51. LPQUERY_SERVICE_CONFIG Config;
  52. PUCHAR Buffer;
  53. ULONG SizeNeeded = sizeof(QUERY_SERVICE_CONFIG) + 256*sizeof(TCHAR);
  54. PWCHAR p, pSrc, pDest;
  55. ULONG Bytes;
  56. BOOLEAN Working;
  57. TraceTag(ttidWmi,
  58. "Entering FixWmiServiceDependcies for %ws...",
  59. ServiceName);
  60. ScmHandle = OpenSCManager(NULL,
  61. NULL,
  62. SC_MANAGER_ALL_ACCESS);
  63. if (ScmHandle != NULL)
  64. {
  65. ServiceHandle = OpenService(ScmHandle,
  66. ServiceName,
  67. SERVICE_ALL_ACCESS);
  68. if (ServiceHandle != NULL)
  69. {
  70. Working = TRUE;
  71. do
  72. {
  73. Buffer = (PUCHAR)MemAlloc(SizeNeeded);
  74. if (Buffer != NULL)
  75. {
  76. Config = (LPQUERY_SERVICE_CONFIG)Buffer;
  77. if (QueryServiceConfig(ServiceHandle,
  78. Config,
  79. SizeNeeded,
  80. &SizeNeeded))
  81. {
  82. p = Config->lpDependencies;
  83. DumpDependencyList(szBefore, Config->lpDependencies);
  84. while (*p != 0)
  85. {
  86. if (_wcsicmp(p, szWMI) == 0)
  87. {
  88. //
  89. // We found the WMI dependency on the
  90. // list
  91. //
  92. TraceTag(ttidWmi,
  93. "FixWmiServiceDependcies: Found WMI Dependency at %p",
  94. p);
  95. pDest = p;
  96. //
  97. // Advance to next string
  98. //
  99. while (*p++ != 0) ;
  100. //
  101. // Figure out number of byes left at
  102. // end of string
  103. //
  104. pSrc = p;
  105. while ((*p != 0) || (*(p+1) != 0))
  106. {
  107. p++;
  108. }
  109. Bytes = ((p + 2) - pSrc) * sizeof(TCHAR);
  110. TraceTag(ttidWmi,
  111. "FixWmiServiceDependcies: Copy 0x%x bytes from %ws to %ws",
  112. Bytes,
  113. pSrc,
  114. pDest);
  115. memcpy(pDest, pSrc, Bytes);
  116. DumpDependencyList(szAfter, Config->lpDependencies);
  117. if (! ChangeServiceConfig(ServiceHandle,
  118. SERVICE_NO_CHANGE,
  119. SERVICE_NO_CHANGE,
  120. SERVICE_NO_CHANGE,
  121. NULL,
  122. NULL,
  123. NULL,
  124. Config->lpDependencies,
  125. NULL,
  126. NULL,
  127. NULL))
  128. {
  129. //
  130. // This is bad
  131. //
  132. TraceError(
  133. "FixWmiServiceDependcies: ChangeServiceConfig failed",
  134. GetLastError());
  135. }
  136. } else {
  137. //
  138. // Advance to next string in list
  139. //
  140. TraceTag(ttidWmi,
  141. "FixWmiServiceDependcies: skipping dependency %ws",
  142. p);
  143. while (*p++ != 0) ;
  144. }
  145. }
  146. Working = FALSE;
  147. } else {
  148. if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
  149. {
  150. //
  151. // This is bad, but what can we do ??
  152. //
  153. Working = FALSE;
  154. TraceError(
  155. "FixWmiServiceDependcies: QueryServiceConfig failed",
  156. GetLastError());
  157. }
  158. }
  159. MemFree(Buffer);
  160. } else {
  161. //
  162. // This is bad, but what can we do ??
  163. //
  164. TraceTag(ttidWmi,
  165. "FixWmiServiceDependcies: BufferAlloc for %d failed",
  166. SizeNeeded);
  167. Working = FALSE;
  168. }
  169. } while(Working);
  170. CloseServiceHandle(ServiceHandle);
  171. } else {
  172. TraceError(
  173. "FixWmiServiceDependcies: OpenService failed",
  174. GetLastError());
  175. }
  176. CloseServiceHandle(ScmHandle);
  177. } else {
  178. TraceError(
  179. "FixWmiServiceDependcies: OpenSCManager failed",
  180. GetLastError());
  181. }
  182. }