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.

184 lines
3.3 KiB

  1. //+--------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1996 - 1999
  5. //
  6. // File: reg.cpp
  7. //
  8. // Contents: certsrv setup reg apis
  9. //
  10. //---------------------------------------------------------------------------
  11. #include <pch.cpp>
  12. #pragma hdrstop
  13. #include <assert.h>
  14. #include <shlwapi.h>
  15. #define __dwFILE__ __dwFILE_OCMSETUP_REG_CPP__
  16. DWORD
  17. mySHCopyKey(
  18. IN HKEY hkeySrc,
  19. IN LPCWSTR wszSrcSubKey,
  20. IN HKEY hkeyDest,
  21. IN DWORD fReserved)
  22. {
  23. DWORD err;
  24. __try
  25. {
  26. err = SHCopyKey(hkeySrc, wszSrcSubKey, hkeyDest, fReserved);
  27. _LeaveIfError(err, "SHCopyKey");
  28. }
  29. __except(err = myHEXCEPTIONCODE(), EXCEPTION_EXECUTE_HANDLER)
  30. {
  31. }
  32. return(err);
  33. }
  34. DWORD
  35. mySHDeleteKey(
  36. IN HKEY hkey,
  37. IN LPCWSTR pszSubKey)
  38. {
  39. DWORD err;
  40. __try
  41. {
  42. err = SHDeleteKey(hkey, pszSubKey);
  43. _LeaveIfError(err, "SHDeleteKey");
  44. }
  45. __except(err = myHEXCEPTIONCODE(), EXCEPTION_EXECUTE_HANDLER)
  46. {
  47. }
  48. return(err);
  49. }
  50. LONG
  51. myRegRenameKey(
  52. HKEY hKey, // handle to an open key
  53. LPCTSTR lpSrcKey, // address of old name of subkey
  54. LPCTSTR lpDesKey, // address of new name of subkey
  55. PHKEY phkResult) // address of buffer for opened handle of new subkey
  56. {
  57. LONG lerr;
  58. HKEY hDesKey = NULL;
  59. if (NULL == lpSrcKey || NULL == lpDesKey)
  60. {
  61. lerr = ERROR_INVALID_PARAMETER;
  62. goto error;
  63. }
  64. // open destination key sure it doesn't exist
  65. lerr = RegOpenKeyEx(
  66. hKey,
  67. lpDesKey,
  68. 0,
  69. KEY_ALL_ACCESS,
  70. &hDesKey);
  71. if (ERROR_SUCCESS == lerr)
  72. {
  73. // destination exists, stop
  74. lerr = ERROR_FILE_EXISTS;
  75. goto error;
  76. }
  77. else if (ERROR_FILE_NOT_FOUND != lerr)
  78. {
  79. goto error;
  80. }
  81. assert(NULL == hDesKey);
  82. lerr = RegCreateKeyEx(
  83. hKey,
  84. lpDesKey,
  85. 0,
  86. NULL,
  87. REG_OPTION_NON_VOLATILE,
  88. KEY_ALL_ACCESS,
  89. NULL,
  90. &hDesKey,
  91. NULL);
  92. if (ERROR_SUCCESS != lerr)
  93. {
  94. goto error;
  95. }
  96. lerr = mySHCopyKey(hKey, lpSrcKey, hDesKey, 0);
  97. if (ERROR_SUCCESS != lerr)
  98. {
  99. goto error;
  100. }
  101. lerr = mySHDeleteKey(hKey, lpSrcKey);
  102. if (ERROR_SUCCESS != lerr)
  103. {
  104. goto error;
  105. }
  106. if (NULL != phkResult)
  107. {
  108. *phkResult = hDesKey;
  109. hDesKey = NULL;
  110. }
  111. // done
  112. lerr = ERROR_SUCCESS;
  113. error:
  114. if (NULL != hDesKey)
  115. {
  116. RegCloseKey(hDesKey);
  117. }
  118. return lerr;
  119. }
  120. HRESULT
  121. myRenameCertRegKey(
  122. IN WCHAR const *pwszSrcCAName,
  123. IN WCHAR const *pwszDesCAName)
  124. {
  125. HRESULT hr;
  126. WCHAR *pwszSrcPath = NULL;
  127. WCHAR *pwszDesPath = NULL;
  128. if (0 == lstrcmpi(pwszSrcCAName, pwszDesCAName))
  129. {
  130. // destination is the same as source, done
  131. goto done;
  132. }
  133. hr = myFormCertRegPath(pwszSrcCAName, NULL, NULL, TRUE, &pwszSrcPath);
  134. _JumpIfError(hr, error, "formCertRegPath");
  135. hr = myFormCertRegPath(pwszDesCAName, NULL, NULL, TRUE, &pwszDesPath);
  136. _JumpIfError(hr, error, "formCertRegPath");
  137. hr = myRegRenameKey(
  138. HKEY_LOCAL_MACHINE,
  139. pwszSrcPath,
  140. pwszDesPath,
  141. NULL);
  142. if ((HRESULT) ERROR_SUCCESS != hr)
  143. {
  144. hr = HRESULT_FROM_WIN32(hr);
  145. _JumpError(hr, error, "myRegMoveKey");
  146. }
  147. done:
  148. hr = S_OK;
  149. error:
  150. if (NULL != pwszSrcPath)
  151. {
  152. LocalFree(pwszSrcPath);
  153. }
  154. if (NULL != pwszDesPath)
  155. {
  156. LocalFree(pwszDesPath);
  157. }
  158. return hr;
  159. }