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.

191 lines
4.2 KiB

  1. /*++
  2. Copyright (c) 2002 Microsoft Corporation
  3. All rights reserved
  4. Module Name:
  5. splpolicy.cxx
  6. Abstract:
  7. Implements methods for reading Spooler policies.
  8. Author:
  9. Adina Trufinescu (adinatru).
  10. Environment:
  11. User Mode - Win32
  12. Revision History:
  13. --*/
  14. #include "spllibp.hxx"
  15. #pragma hdrstop
  16. #include "splcom.h"
  17. #include "splpolicy.hxx"
  18. CONST TCHAR *szPrintPolicy = TEXT("Software\\Policies\\Microsoft\\Windows NT\\Printers");
  19. CONST TCHAR *szRegisterSpoolerRemoteRpcEndPoint = TEXT("RegisterSpoolerRemoteRpcEndPoint");
  20. /*++
  21. Routine Name
  22. GetSpoolerPolicy
  23. Routine Description:
  24. Reads a Spooler policy.
  25. Arguments:
  26. pszRegValue - pointer to a null-terminated string
  27. pData - pointer to buffer that receives the value's data
  28. pcbSize - pointer to variable that receives the value's size in bytes
  29. pType - pointer to variable that receives the value's type
  30. Return Value:
  31. HRESULT
  32. --*/
  33. HRESULT
  34. GetSpoolerPolicy(
  35. IN PCTSTR pszRegValue,
  36. IN OUT PBYTE pData,
  37. IN OUT PDWORD pcbSize,
  38. OUT PDWORD pType
  39. )
  40. {
  41. HRESULT hr;
  42. HKEY hPrintPolicyKey = NULL;
  43. ULONG Type;
  44. ULONG cbSize;
  45. hr = (pszRegValue && pData && pcbSize) ? S_OK : E_POINTER;
  46. if (SUCCEEDED(hr))
  47. {
  48. hr = HResultFromWin32(RegOpenKeyEx(HKEY_LOCAL_MACHINE,
  49. szPrintPolicy,
  50. 0,
  51. KEY_READ,
  52. &hPrintPolicyKey));
  53. if (SUCCEEDED(hr))
  54. {
  55. hr = HResultFromWin32(RegQueryValueEx(hPrintPolicyKey,
  56. pszRegValue,
  57. NULL,
  58. &Type,
  59. pData,
  60. pcbSize));
  61. if (SUCCEEDED(hr) && pType)
  62. {
  63. *pType = Type;
  64. }
  65. RegCloseKey(hPrintPolicyKey);
  66. }
  67. }
  68. return hr;
  69. }
  70. /*++
  71. Routine Name
  72. GetSpoolerNumericPolicy
  73. Routine Description:
  74. Reads a numeric Spooler policy. Allows setting the default value
  75. if policy is unconfigured.
  76. Arguments:
  77. pszRegValue - pointer to a null-terminated string
  78. DefaultValue - Default value if reading fails
  79. Return Value:
  80. ULONG
  81. --*/
  82. ULONG
  83. GetSpoolerNumericPolicy(
  84. IN PCTSTR pszRegValue,
  85. IN ULONG DefaultValue
  86. )
  87. {
  88. ULONG Value = DefaultValue;
  89. ULONG cbSize = sizeof(DWORD);
  90. HRESULT hr = (pszRegValue) ? S_OK : E_POINTER;
  91. ULONG Type;
  92. if (SUCCEEDED(hr))
  93. {
  94. hr = GetSpoolerPolicy(pszRegValue, (PBYTE)&Value, &cbSize, &Type);
  95. if (FAILED(hr) || Type != REG_DWORD)
  96. {
  97. Value = DefaultValue;
  98. }
  99. }
  100. return Value;
  101. }
  102. /*++
  103. Routine Name
  104. GetSpoolerNumericPolicyValidate
  105. Routine Description:
  106. Reads a numeric Spooler policy. Allows setting the default value
  107. if policy is unconfigured and does validation against the value.
  108. If the value is of another type, or the value is bigger than MaxValue,
  109. it returns the DefaultValue.
  110. Arguments:
  111. pszRegValue - pointer to a null-terminated string
  112. DefaultValue - Default value if reading fails
  113. MaxValue - Maximum value expected.
  114. Return Value:
  115. ULONG
  116. --*/
  117. ULONG
  118. GetSpoolerNumericPolicyValidate(
  119. IN PCTSTR pszRegValue,
  120. IN ULONG DefaultValue,
  121. IN ULONG MaxValue
  122. )
  123. {
  124. ULONG Value = DefaultValue;
  125. ULONG cbSize = sizeof(DWORD);
  126. HRESULT hr = (pszRegValue) ? S_OK : E_POINTER;
  127. ULONG Type;
  128. if (SUCCEEDED(hr))
  129. {
  130. hr = GetSpoolerPolicy(pszRegValue, (PBYTE)&Value, &cbSize, &Type);
  131. if (FAILED(hr) || Type != REG_DWORD || Value > MaxValue)
  132. {
  133. Value = DefaultValue;
  134. }
  135. }
  136. return Value;
  137. }