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.

84 lines
2.6 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 2000-2002.
  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. // security review 2/22/2002 BryanWal ok - needed for deletion
  43. KEY_ALL_ACCESS, // desired security access
  44. NULL, // address of key security structure
  45. &m_hSubKey, // address of buffer for opened handle
  46. &dwDisposition); // address of disposition value buffer
  47. ASSERT (lResult == ERROR_SUCCESS);
  48. if ( lResult != ERROR_SUCCESS )
  49. {
  50. _TRACE (0, L"RegCreateKeyEx (%s) failed: %d\n", pszKey,
  51. lResult);
  52. }
  53. }
  54. }
  55. else
  56. {
  57. _TRACE (0, L"IGPEInformation::GetRegistryKey (%s) failed: 0x%x\n",
  58. fIsMachineType ?
  59. L"GPO_SECTION_MACHINE" : L"GPO_SECTION_USER",
  60. hr);
  61. }
  62. }
  63. }
  64. CPolicyKey::~CPolicyKey ()
  65. {
  66. if ( m_hSubKey )
  67. ::RegCloseKey (m_hSubKey);
  68. if ( m_hKeyGroupPolicy )
  69. ::RegCloseKey (m_hKeyGroupPolicy);
  70. }
  71. HKEY CPolicyKey::GetKey () const
  72. {
  73. if (m_hSubKey)
  74. return m_hSubKey;
  75. else
  76. {
  77. return m_hKeyGroupPolicy;
  78. }
  79. }