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.

260 lines
6.5 KiB

  1. #include "precomp.h"
  2. #pragma hdrstop
  3. #include <netcfgx.h>
  4. #include <devguid.h>
  5. HRESULT
  6. HrCreateINetCfg (
  7. IN BOOL fAcquireWriteLock,
  8. OUT INetCfg** ppINetCfg)
  9. {
  10. HRESULT hr;
  11. INetCfg* pINetCfg;
  12. // Get the INetCfg interface.
  13. //
  14. hr = CoCreateInstance(
  15. CLSID_CNetCfg,
  16. NULL,
  17. CLSCTX_INPROC_SERVER | CLSCTX_NO_CODE_DOWNLOAD,
  18. IID_INetCfg,
  19. reinterpret_cast<void**>(&pINetCfg));
  20. if (S_OK == hr) {
  21. INetCfgLock * pnclock = NULL;
  22. if (fAcquireWriteLock) {
  23. // Get the locking interface
  24. hr = pINetCfg->QueryInterface(IID_INetCfgLock,
  25. reinterpret_cast<LPVOID *>(&pnclock));
  26. if (SUCCEEDED(hr)) {
  27. LPWSTR pwszLockHolder;
  28. // Attempt to lock the INetCfg for read/write
  29. hr = pnclock->AcquireWriteLock(100, L"InstallIPv6",
  30. &pwszLockHolder);
  31. if (S_FALSE == hr) {
  32. // Couldn't acquire the lock
  33. hr = NETCFG_E_NO_WRITE_LOCK;
  34. DisplayMessage(g_hModule, EMSG_NO_WRITE_LOCK,
  35. pwszLockHolder);
  36. }
  37. if (pwszLockHolder) {
  38. CoTaskMemFree(pwszLockHolder);
  39. }
  40. }
  41. }
  42. if (S_OK == hr) {
  43. hr = pINetCfg->Initialize (NULL);
  44. if (S_OK == hr) {
  45. *ppINetCfg = pINetCfg;
  46. pINetCfg->AddRef();
  47. }
  48. else {
  49. if (pnclock) {
  50. pnclock->ReleaseWriteLock();
  51. }
  52. }
  53. }
  54. if (pnclock) {
  55. pnclock->Release();
  56. }
  57. //Transfer ownership to caller.
  58. pINetCfg->Release();
  59. }
  60. return hr;
  61. }
  62. DWORD
  63. pAddOrRemoveIpv6(BOOL fAddIpv6)
  64. {
  65. HRESULT hr;
  66. INetCfg* pINetCfg;
  67. DWORD dwErr = NO_ERROR;
  68. hr = HrCreateINetCfg (TRUE, &pINetCfg);
  69. if (S_OK == hr) {
  70. INetCfgClassSetup* pSetup;
  71. // Get the setup interface used for installing
  72. // and uninstalling components.
  73. //
  74. hr = pINetCfg->QueryNetCfgClass (
  75. &GUID_DEVCLASS_NETTRANS,
  76. IID_INetCfgClassSetup,
  77. (VOID**)&pSetup);
  78. if (S_OK == hr) {
  79. OBO_TOKEN OboToken;
  80. INetCfgComponent* pIComp;
  81. ZeroMemory (&OboToken, sizeof(OboToken));
  82. OboToken.Type = OBO_USER;
  83. if (fAddIpv6) {
  84. hr = pSetup->Install (
  85. L"MS_TCPIP6",
  86. &OboToken,
  87. 0, 0, NULL, NULL,
  88. &pIComp);
  89. if (pIComp) {
  90. pIComp->Release();
  91. }
  92. } else {
  93. // Need to remove the component.
  94. // Find it first.
  95. //
  96. hr = pINetCfg->FindComponent (
  97. L"MS_TCPIP6",
  98. &pIComp);
  99. if (S_OK == hr) {
  100. hr = pSetup->DeInstall (
  101. pIComp,
  102. &OboToken,
  103. NULL);
  104. pIComp->Release();
  105. } else {
  106. dwErr = ERROR_OKAY;
  107. }
  108. }
  109. if (SUCCEEDED(hr)) {
  110. if (NETCFG_S_REBOOT == hr) {
  111. hr = S_OK;
  112. DisplayMessage(g_hModule, EMSG_REBOOT_NEEDED);
  113. dwErr = ERROR_SUPPRESS_OUTPUT;
  114. } else {
  115. dwErr = ERROR_OKAY;
  116. }
  117. } else {
  118. if (NETCFG_E_NEED_REBOOT == hr) {
  119. DisplayMessage(g_hModule, EMSG_REBOOT_NEEDED);
  120. dwErr = ERROR_SUPPRESS_OUTPUT;
  121. } else {
  122. dwErr = hr;
  123. }
  124. }
  125. pSetup->Release();
  126. }
  127. hr = pINetCfg->Uninitialize();
  128. if (SUCCEEDED(hr)) {
  129. INetCfgLock *pnclock;
  130. // Get the locking interface
  131. hr = pINetCfg->QueryInterface(IID_INetCfgLock,
  132. reinterpret_cast<LPVOID *>(&pnclock));
  133. if (SUCCEEDED(hr)) {
  134. // Attempt to lock the INetCfg for read/write
  135. hr = pnclock->ReleaseWriteLock();
  136. pnclock->Release();
  137. }
  138. }
  139. pINetCfg->Release();
  140. }
  141. else if (NETCFG_E_NO_WRITE_LOCK == hr) {
  142. // Message has already been printed
  143. dwErr = ERROR_SUPPRESS_OUTPUT;
  144. }
  145. else if (HRESULT_FROM_WIN32(ERROR_ACCESS_DENIED) == hr) {
  146. dwErr = ERROR_ACCESS_DENIED;
  147. }
  148. else {
  149. dwErr = hr;
  150. }
  151. return dwErr;
  152. }
  153. EXTERN_C
  154. DWORD
  155. IsIpv6Installed(
  156. BOOL *bInstalled)
  157. {
  158. HRESULT hr = S_OK;
  159. BOOL fInitCom = TRUE;
  160. BOOL fPresent = FALSE;
  161. DWORD dwErr = NO_ERROR;
  162. // Initialize COM.
  163. //
  164. hr = CoInitializeEx( NULL,
  165. COINIT_DISABLE_OLE1DDE | COINIT_APARTMENTTHREADED );
  166. if (RPC_E_CHANGED_MODE == hr) {
  167. // If we changed mode, then we won't uninitialize COM when we are done.
  168. //
  169. hr = S_OK;
  170. fInitCom = FALSE;
  171. }
  172. if (SUCCEEDED(hr)) {
  173. HRESULT hr;
  174. INetCfg* pINetCfg;
  175. hr = HrCreateINetCfg (FALSE, &pINetCfg);
  176. if (S_OK == hr) {
  177. fPresent = (S_OK == pINetCfg->FindComponent(L"MS_TCPIP6", NULL));
  178. pINetCfg->Uninitialize();
  179. pINetCfg->Release();
  180. } else {
  181. dwErr = hr;
  182. }
  183. if (fInitCom) {
  184. CoUninitialize();
  185. }
  186. } else {
  187. dwErr = hr;
  188. }
  189. *bInstalled = fPresent;
  190. return dwErr;
  191. }
  192. EXTERN_C
  193. DWORD
  194. AddOrRemoveIpv6 (
  195. IN BOOL fAddIpv6)
  196. {
  197. HRESULT hr = S_OK;
  198. BOOL fInitCom = TRUE;
  199. DWORD dwErr = NO_ERROR;
  200. // Initialize COM.
  201. //
  202. hr = CoInitializeEx( NULL,
  203. COINIT_DISABLE_OLE1DDE | COINIT_APARTMENTTHREADED );
  204. if (RPC_E_CHANGED_MODE == hr) {
  205. // If we changed mode, then we won't uninitialize COM when we are done.
  206. //
  207. hr = S_OK;
  208. fInitCom = FALSE;
  209. }
  210. if (SUCCEEDED(hr)) {
  211. dwErr = pAddOrRemoveIpv6(fAddIpv6);
  212. if (fInitCom) {
  213. CoUninitialize();
  214. }
  215. }
  216. else {
  217. dwErr = hr;
  218. }
  219. return dwErr;
  220. }