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.

200 lines
4.3 KiB

  1. //depot/Lab04_N/Base/screg/winreg/server/regdkey.c#5 - integrate change 12179 (text)
  2. /*++
  3. Copyright (c) 1991 Microsoft Corporation
  4. Module Name:
  5. Regdkey.c
  6. Abstract:
  7. This module contains the server side implementation for the Win32
  8. Registry API to delete a key. That is:
  9. - BaseRegDeleteKey
  10. Author:
  11. David J. Gilman (davegi) 15-Nov-1991
  12. Notes:
  13. See the Notes in Regkey.c.
  14. --*/
  15. #include <rpc.h>
  16. #include "regrpc.h"
  17. #include "localreg.h"
  18. #include "regclass.h"
  19. #include <malloc.h>
  20. #ifdef LOCAL
  21. #include "tsappcmp.h"
  22. #include <wow64reg.h>
  23. #endif
  24. error_status_t
  25. BaseRegDeleteKey(
  26. HKEY hKey,
  27. PUNICODE_STRING lpSubKey
  28. )
  29. /*++
  30. Routine Description:
  31. Delete a key.
  32. Arguments:
  33. hKey - Supplies a handle to an open key. The lpSubKey pathname
  34. parameter is relative to this key handle. Any of the predefined
  35. reserved handles or a previously opened key handle may be used for
  36. hKey.
  37. lpSubKey - Supplies the downward key path to the key to delete. May
  38. NOT be NULL.
  39. Return Value:
  40. Returns ERROR_SUCCESS (0) for success; error-code for failure.
  41. Notes:
  42. If successful, RegDeleteKey removes the key at the desired location
  43. from the registration database. The entire key, including all of its
  44. values, will be removed. The key to be deleted may NOT have children,
  45. otherwise the call will fail. There must not be any open handles that
  46. refer to the key to be deleted, otherwise the call will fail. DELETE
  47. access to the key being deleted is required.
  48. --*/
  49. {
  50. OBJECT_ATTRIBUTES Obja;
  51. NTSTATUS Status;
  52. NTSTATUS StatusCheck;
  53. HKEY KeyHandle;
  54. BOOL fSafeToDelete;
  55. #ifdef LOCAL
  56. UNICODE_STRING TmpStr = *lpSubKey; //used to keep original SubKey string
  57. #endif //LOCAL
  58. ASSERT( IsPredefinedRegistryHandle( hKey ) == FALSE );
  59. //
  60. // Impersonate the client.
  61. //
  62. RPC_IMPERSONATE_CLIENT( NULL );
  63. //
  64. // Subtract the NULL from the string length. This was added
  65. // by the client so that RPC would transmit the whole thing.
  66. //
  67. lpSubKey->Length -= sizeof( UNICODE_NULL );
  68. #ifdef LOCAL
  69. //
  70. // see if this key is a special key in HKCR
  71. //
  72. if (REG_CLASS_IS_SPECIAL_KEY(hKey) ||
  73. ( (gdwRegistryExtensionFlags & TERMSRV_ENABLE_PER_USER_CLASSES_REDIRECTION)
  74. && ExtractClassKey(&hKey,lpSubKey) ) ) {
  75. //
  76. // if this is a class registration, we call a special routine
  77. // to open this key
  78. //
  79. Status = BaseRegOpenClassKey(
  80. hKey,
  81. lpSubKey,
  82. 0,
  83. MAXIMUM_ALLOWED,
  84. &KeyHandle);
  85. if (!NT_SUCCESS(Status)) {
  86. goto cleanup;
  87. }
  88. } else
  89. #endif // LOCAL
  90. {
  91. //
  92. // Initialize the OBJECT_ATTRIBUTES structure and open the sub key
  93. // so that it can then be deleted.
  94. //
  95. InitializeObjectAttributes(
  96. &Obja,
  97. lpSubKey,
  98. OBJ_CASE_INSENSITIVE,
  99. hKey,
  100. NULL
  101. );
  102. Status = NtOpenKey(
  103. &KeyHandle,
  104. DELETE,
  105. &Obja
  106. );
  107. }
  108. #ifdef LOCAL
  109. if (gpfnTermsrvDeleteKey) {
  110. //
  111. // Remove the key from the Terminal Server registry tracking database
  112. //
  113. gpfnTermsrvDeleteKey(KeyHandle);
  114. }
  115. #endif
  116. //
  117. // If for any reason the key could not be opened, return the error.
  118. //
  119. if( NT_SUCCESS( Status )) {
  120. //
  121. // Call the Nt APIs to delete and close the key.
  122. //
  123. #if defined(_WIN64) & defined ( LOCAL)
  124. HKEY hWowKey = Wow64OpenRemappedKeyOnReflection (KeyHandle);
  125. #endif //wow64 reflection case
  126. Status = NtDeleteKey( KeyHandle );
  127. StatusCheck = NtClose( KeyHandle );
  128. ASSERT( NT_SUCCESS( StatusCheck ));
  129. #if defined(_WIN64) & defined ( LOCAL)
  130. if ( (NT_SUCCESS( Status )) && (hWowKey != NULL))
  131. Wow64RegDeleteKey (hWowKey, NULL);
  132. if (hWowKey != NULL)
  133. NtClose (hWowKey);
  134. #endif //wow64 reflection case
  135. }
  136. #ifdef LOCAL
  137. cleanup:
  138. *lpSubKey = TmpStr;
  139. #endif
  140. RPC_REVERT_TO_SELF();
  141. //
  142. // Map the NTSTATUS code to a Win32 Registry error code and return.
  143. //
  144. return (error_status_t)RtlNtStatusToDosError( Status );
  145. }