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.

83 lines
2.5 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 2000-2001.
  5. //
  6. // File: PolicyKey.cpp
  7. //
  8. // Contents: Implementation of CPolicyKey
  9. //
  10. //----------------------------------------------------------------------------
  11. #include "stdafx.h"
  12. #include <gpedit.h>
  13. #include "PolicyKey.h"
  14. #ifdef _DEBUG
  15. #ifndef ALPHA
  16. #define new DEBUG_NEW
  17. #endif
  18. #undef THIS_FILE
  19. static char THIS_FILE[] = __FILE__;
  20. #endif
  21. CPolicyKey::CPolicyKey (IGPEInformation* pGPEInformation, PCWSTR pszKey, bool fIsMachineType)
  22. : m_hKeyGroupPolicy (0),
  23. m_hSubKey (0)
  24. {
  25. ASSERT (pszKey);
  26. if ( pGPEInformation )
  27. {
  28. HRESULT hr = pGPEInformation->GetRegistryKey (
  29. fIsMachineType ? GPO_SECTION_MACHINE : GPO_SECTION_USER,
  30. &m_hKeyGroupPolicy);
  31. ASSERT (SUCCEEDED (hr));
  32. if ( SUCCEEDED (hr) )
  33. {
  34. if ( pszKey && pszKey[0] )
  35. {
  36. DWORD dwDisposition = 0;
  37. LONG lResult = ::RegCreateKeyEx (m_hKeyGroupPolicy, // handle of an open key
  38. pszKey, // address of subkey name
  39. 0, // reserved
  40. L"", // address of class string
  41. REG_OPTION_NON_VOLATILE, // special options flag
  42. KEY_ALL_ACCESS, // desired security access
  43. NULL, // address of key security structure
  44. &m_hSubKey, // address of buffer for opened handle
  45. &dwDisposition); // address of disposition value buffer
  46. ASSERT (lResult == ERROR_SUCCESS);
  47. if ( lResult != ERROR_SUCCESS )
  48. {
  49. _TRACE (0, L"RegCreateKeyEx (%s) failed: %d\n", pszKey,
  50. lResult);
  51. }
  52. }
  53. }
  54. else
  55. {
  56. _TRACE (0, L"IGPEInformation::GetRegistryKey (%s) failed: 0x%x\n",
  57. fIsMachineType ?
  58. L"GPO_SECTION_MACHINE" : L"GPO_SECTION_USER",
  59. hr);
  60. }
  61. }
  62. }
  63. CPolicyKey::~CPolicyKey ()
  64. {
  65. if ( m_hSubKey )
  66. VERIFY (ERROR_SUCCESS == ::RegCloseKey (m_hSubKey));
  67. if ( m_hKeyGroupPolicy )
  68. VERIFY (ERROR_SUCCESS == ::RegCloseKey (m_hKeyGroupPolicy));
  69. }
  70. HKEY CPolicyKey::GetKey () const
  71. {
  72. if (m_hSubKey)
  73. return m_hSubKey;
  74. else
  75. {
  76. return m_hKeyGroupPolicy;
  77. }
  78. }