aboutsummaryrefslogtreecommitdiff
path: root/src/dutil/polcutil.cpp
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/dutil/polcutil.cpp111
1 files changed, 111 insertions, 0 deletions
diff --git a/src/dutil/polcutil.cpp b/src/dutil/polcutil.cpp
new file mode 100644
index 00000000..1cc29e61
--- /dev/null
+++ b/src/dutil/polcutil.cpp
@@ -0,0 +1,111 @@
1// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information.
2
3#include "precomp.h"
4
5const LPCWSTR REGISTRY_POLICIES_KEY = L"SOFTWARE\\Policies\\";
6
7static HRESULT OpenPolicyKey(
8 __in_z LPCWSTR wzPolicyPath,
9 __out HKEY* phk
10 );
11
12
13extern "C" HRESULT DAPI PolcReadNumber(
14 __in_z LPCWSTR wzPolicyPath,
15 __in_z LPCWSTR wzPolicyName,
16 __in DWORD dwDefault,
17 __out DWORD* pdw
18 )
19{
20 HRESULT hr = S_OK;
21 HKEY hk = NULL;
22
23 hr = OpenPolicyKey(wzPolicyPath, &hk);
24 if (E_FILENOTFOUND == hr || E_PATHNOTFOUND == hr)
25 {
26 ExitFunction1(hr = S_FALSE);
27 }
28 ExitOnFailure(hr, "Failed to open policy key: %ls", wzPolicyPath);
29
30 hr = RegReadNumber(hk, wzPolicyName, pdw);
31 if (E_FILENOTFOUND == hr || E_PATHNOTFOUND == hr)
32 {
33 ExitFunction1(hr = S_FALSE);
34 }
35 ExitOnFailure(hr, "Failed to open policy key: %ls, name: %ls", wzPolicyPath, wzPolicyName);
36
37LExit:
38 ReleaseRegKey(hk);
39
40 if (S_FALSE == hr || FAILED(hr))
41 {
42 *pdw = dwDefault;
43 }
44
45 return hr;
46}
47
48extern "C" HRESULT DAPI PolcReadString(
49 __in_z LPCWSTR wzPolicyPath,
50 __in_z LPCWSTR wzPolicyName,
51 __in_z_opt LPCWSTR wzDefault,
52 __deref_out_z LPWSTR* pscz
53 )
54{
55 HRESULT hr = S_OK;
56 HKEY hk = NULL;
57
58 hr = OpenPolicyKey(wzPolicyPath, &hk);
59 if (E_FILENOTFOUND == hr || E_PATHNOTFOUND == hr)
60 {
61 ExitFunction1(hr = S_FALSE);
62 }
63 ExitOnFailure(hr, "Failed to open policy key: %ls", wzPolicyPath);
64
65 hr = RegReadString(hk, wzPolicyName, pscz);
66 if (E_FILENOTFOUND == hr || E_PATHNOTFOUND == hr)
67 {
68 ExitFunction1(hr = S_FALSE);
69 }
70 ExitOnFailure(hr, "Failed to open policy key: %ls, name: %ls", wzPolicyPath, wzPolicyName);
71
72LExit:
73 ReleaseRegKey(hk);
74
75 if (S_FALSE == hr || FAILED(hr))
76 {
77 if (NULL == wzDefault)
78 {
79 ReleaseNullStr(*pscz);
80 }
81 else
82 {
83 hr = StrAllocString(pscz, wzDefault, 0);
84 }
85 }
86
87 return hr;
88}
89
90
91// internal functions
92
93static HRESULT OpenPolicyKey(
94 __in_z LPCWSTR wzPolicyPath,
95 __out HKEY* phk
96 )
97{
98 HRESULT hr = S_OK;
99 LPWSTR sczPath = NULL;
100
101 hr = PathConcat(REGISTRY_POLICIES_KEY, wzPolicyPath, &sczPath);
102 ExitOnFailure(hr, "Failed to combine logging path with root path.");
103
104 hr = RegOpen(HKEY_LOCAL_MACHINE, sczPath, KEY_READ, phk);
105 ExitOnFailure(hr, "Failed to open policy registry key.");
106
107LExit:
108 ReleaseStr(sczPath);
109
110 return hr;
111}