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.

187 lines
4.9 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows 2000 (c) 1999
  4. //
  5. // File: install.cpp
  6. //
  7. // Contents: Net config code that installs the packet scheduler based
  8. // the domain policy
  9. //
  10. // Author: Shreedhar Madhavapeddi (ShreeM)
  11. //
  12. // Reworked by Sergei Antonov (sergeia) -- removed ugly notation and
  13. // adapted to iis needs
  14. //
  15. // Usage Notes:
  16. //
  17. //----------------------------------------------------------------------------
  18. #include "stdafx.h"
  19. #include <netcfgx.h>
  20. #include <devguid.h>
  21. static const GUID * c_aguidClass[] =
  22. {
  23. &GUID_DEVCLASS_NET,
  24. &GUID_DEVCLASS_NETTRANS,
  25. &GUID_DEVCLASS_NETSERVICE,
  26. &GUID_DEVCLASS_NETCLIENT
  27. };
  28. HRESULT CallINetCfg(BOOL Install);
  29. HRESULT
  30. AddRemovePSCHED(INetCfg * pINetCfg, BOOL Install)
  31. {
  32. HRESULT hr;
  33. INetCfgClassSetup * pSetup;
  34. INetCfgComponent * pIComp;
  35. OBO_TOKEN OboToken;
  36. pSetup = NULL;
  37. pIComp = NULL;
  38. if (!pINetCfg)
  39. {
  40. return E_POINTER;
  41. }
  42. hr = pINetCfg->QueryNetCfgClass (&GUID_DEVCLASS_NETSERVICE,
  43. IID_INetCfgClassSetup, (VOID**)&pSetup);
  44. if (S_OK == hr)
  45. {
  46. ZeroMemory (&OboToken, sizeof(OboToken));
  47. OboToken.Type = OBO_USER;
  48. if (Install)
  49. {
  50. hr = pSetup->Install (
  51. L"ms_psched",
  52. &OboToken,
  53. 0, 0, NULL, NULL,
  54. &pIComp);
  55. if (NETCFG_S_REBOOT == hr)
  56. {
  57. hr = S_OK;
  58. }
  59. if (HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND) == hr)
  60. {
  61. hr = S_OK;
  62. }
  63. }
  64. else
  65. {
  66. // first find the component.
  67. hr = pINetCfg->FindComponent (L"ms_psched", &pIComp);
  68. if (S_OK == hr)
  69. {
  70. hr = pSetup->DeInstall (
  71. pIComp,
  72. &OboToken,
  73. NULL);
  74. if (NETCFG_S_REBOOT == hr)
  75. {
  76. hr = S_OK;
  77. }
  78. if (NETCFG_S_STILL_REFERENCED == hr)
  79. {
  80. hr = S_OK;
  81. }
  82. }
  83. }
  84. if (pIComp && SUCCEEDED(hr))
  85. {
  86. pIComp->Release();
  87. }
  88. pSetup->Release();
  89. }
  90. HRESULT hrT = pINetCfg->Uninitialize ();
  91. return hr;
  92. }
  93. HRESULT
  94. CallINetCfg(BOOL Install)
  95. {
  96. HRESULT hr = S_OK;
  97. // initialize COM
  98. hr = CoInitializeEx(NULL, COINIT_DISABLE_OLE1DDE | COINIT_APARTMENTTHREADED );
  99. if (SUCCEEDED(hr))
  100. {
  101. // Create the object implementing INetCfg.
  102. //
  103. INetCfg* pnc;
  104. hr = CoCreateInstance(CLSID_CNetCfg, NULL, CLSCTX_INPROC_SERVER,
  105. IID_INetCfg, (void**)&pnc);
  106. if (SUCCEEDED(hr))
  107. {
  108. INetCfgLock * pncLock = NULL;
  109. // Get the locking interface
  110. hr = pnc->QueryInterface(IID_INetCfgLock,
  111. (LPVOID *)&pncLock);
  112. if (SUCCEEDED(hr))
  113. {
  114. // Attempt to lock the INetCfg for read/write
  115. static const ULONG c_cmsTimeout = 15000;
  116. static const WCHAR c_szSampleNetcfgApp[] =
  117. L"Internet Information Services MMC Snapin";
  118. PWSTR szLockedBy;
  119. hr = pncLock->AcquireWriteLock(c_cmsTimeout,
  120. c_szSampleNetcfgApp,
  121. &szLockedBy);
  122. if (S_FALSE == hr)
  123. {
  124. hr = NETCFG_E_NO_WRITE_LOCK;
  125. // _tprintf(L"Could not lock INetcfg, it is already locked by '%s'", szLockedBy);
  126. pncLock->Release();
  127. pnc->Release();
  128. CoUninitialize();
  129. }
  130. if (SUCCEEDED(hr))
  131. {
  132. // Initialize the INetCfg object.
  133. //
  134. hr = pnc->Initialize(NULL);
  135. if (SUCCEEDED(hr))
  136. {
  137. pnc->AddRef();
  138. AddRemovePSCHED(pnc, Install);
  139. }
  140. else
  141. {
  142. // initialize failed, if obtained lock, release it
  143. pncLock->ReleaseWriteLock();
  144. }
  145. }
  146. pncLock->Release();
  147. pnc->Release();
  148. }
  149. else
  150. {
  151. pnc->Release();
  152. }
  153. }
  154. if (FAILED(hr))
  155. {
  156. CoUninitialize();
  157. }
  158. }
  159. return hr;
  160. }