aboutsummaryrefslogtreecommitdiff
path: root/src/libs/dutil/WixToolset.DUtil/polcutil.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/libs/dutil/WixToolset.DUtil/polcutil.cpp')
-rw-r--r--src/libs/dutil/WixToolset.DUtil/polcutil.cpp126
1 files changed, 126 insertions, 0 deletions
diff --git a/src/libs/dutil/WixToolset.DUtil/polcutil.cpp b/src/libs/dutil/WixToolset.DUtil/polcutil.cpp
new file mode 100644
index 00000000..1fdfa18c
--- /dev/null
+++ b/src/libs/dutil/WixToolset.DUtil/polcutil.cpp
@@ -0,0 +1,126 @@
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
5
6// Exit macros
7#define PolcExitOnLastError(x, s, ...) ExitOnLastErrorSource(DUTIL_SOURCE_POLCUTIL, x, s, __VA_ARGS__)
8#define PolcExitOnLastErrorDebugTrace(x, s, ...) ExitOnLastErrorDebugTraceSource(DUTIL_SOURCE_POLCUTIL, x, s, __VA_ARGS__)
9#define PolcExitWithLastError(x, s, ...) ExitWithLastErrorSource(DUTIL_SOURCE_POLCUTIL, x, s, __VA_ARGS__)
10#define PolcExitOnFailure(x, s, ...) ExitOnFailureSource(DUTIL_SOURCE_POLCUTIL, x, s, __VA_ARGS__)
11#define PolcExitOnRootFailure(x, s, ...) ExitOnRootFailureSource(DUTIL_SOURCE_POLCUTIL, x, s, __VA_ARGS__)
12#define PolcExitOnFailureDebugTrace(x, s, ...) ExitOnFailureDebugTraceSource(DUTIL_SOURCE_POLCUTIL, x, s, __VA_ARGS__)
13#define PolcExitOnNull(p, x, e, s, ...) ExitOnNullSource(DUTIL_SOURCE_POLCUTIL, p, x, e, s, __VA_ARGS__)
14#define PolcExitOnNullWithLastError(p, x, s, ...) ExitOnNullWithLastErrorSource(DUTIL_SOURCE_POLCUTIL, p, x, s, __VA_ARGS__)
15#define PolcExitOnNullDebugTrace(p, x, e, s, ...) ExitOnNullDebugTraceSource(DUTIL_SOURCE_POLCUTIL, p, x, e, s, __VA_ARGS__)
16#define PolcExitOnInvalidHandleWithLastError(p, x, s, ...) ExitOnInvalidHandleWithLastErrorSource(DUTIL_SOURCE_POLCUTIL, p, x, s, __VA_ARGS__)
17#define PolcExitOnWin32Error(e, x, s, ...) ExitOnWin32ErrorSource(DUTIL_SOURCE_POLCUTIL, e, x, s, __VA_ARGS__)
18#define PolcExitOnGdipFailure(g, x, s, ...) ExitOnGdipFailureSource(DUTIL_SOURCE_POLCUTIL, g, x, s, __VA_ARGS__)
19
20const LPCWSTR REGISTRY_POLICIES_KEY = L"SOFTWARE\\Policies\\";
21
22static HRESULT OpenPolicyKey(
23 __in_z LPCWSTR wzPolicyPath,
24 __out HKEY* phk
25 );
26
27
28extern "C" HRESULT DAPI PolcReadNumber(
29 __in_z LPCWSTR wzPolicyPath,
30 __in_z LPCWSTR wzPolicyName,
31 __in DWORD dwDefault,
32 __out DWORD* pdw
33 )
34{
35 HRESULT hr = S_OK;
36 HKEY hk = NULL;
37
38 hr = OpenPolicyKey(wzPolicyPath, &hk);
39 if (E_FILENOTFOUND == hr || E_PATHNOTFOUND == hr)
40 {
41 ExitFunction1(hr = S_FALSE);
42 }
43 PolcExitOnFailure(hr, "Failed to open policy key: %ls", wzPolicyPath);
44
45 hr = RegReadNumber(hk, wzPolicyName, pdw);
46 if (E_FILENOTFOUND == hr || E_PATHNOTFOUND == hr)
47 {
48 ExitFunction1(hr = S_FALSE);
49 }
50 PolcExitOnFailure(hr, "Failed to open policy key: %ls, name: %ls", wzPolicyPath, wzPolicyName);
51
52LExit:
53 ReleaseRegKey(hk);
54
55 if (S_FALSE == hr || FAILED(hr))
56 {
57 *pdw = dwDefault;
58 }
59
60 return hr;
61}
62
63extern "C" HRESULT DAPI PolcReadString(
64 __in_z LPCWSTR wzPolicyPath,
65 __in_z LPCWSTR wzPolicyName,
66 __in_z_opt LPCWSTR wzDefault,
67 __deref_out_z LPWSTR* pscz
68 )
69{
70 HRESULT hr = S_OK;
71 HKEY hk = NULL;
72
73 hr = OpenPolicyKey(wzPolicyPath, &hk);
74 if (E_FILENOTFOUND == hr || E_PATHNOTFOUND == hr)
75 {
76 ExitFunction1(hr = S_FALSE);
77 }
78 PolcExitOnFailure(hr, "Failed to open policy key: %ls", wzPolicyPath);
79
80 hr = RegReadString(hk, wzPolicyName, pscz);
81 if (E_FILENOTFOUND == hr || E_PATHNOTFOUND == hr)
82 {
83 ExitFunction1(hr = S_FALSE);
84 }
85 PolcExitOnFailure(hr, "Failed to open policy key: %ls, name: %ls", wzPolicyPath, wzPolicyName);
86
87LExit:
88 ReleaseRegKey(hk);
89
90 if (S_FALSE == hr || FAILED(hr))
91 {
92 if (NULL == wzDefault)
93 {
94 ReleaseNullStr(*pscz);
95 }
96 else
97 {
98 hr = StrAllocString(pscz, wzDefault, 0);
99 }
100 }
101
102 return hr;
103}
104
105
106// internal functions
107
108static HRESULT OpenPolicyKey(
109 __in_z LPCWSTR wzPolicyPath,
110 __out HKEY* phk
111 )
112{
113 HRESULT hr = S_OK;
114 LPWSTR sczPath = NULL;
115
116 hr = PathConcat(REGISTRY_POLICIES_KEY, wzPolicyPath, &sczPath);
117 PolcExitOnFailure(hr, "Failed to combine logging path with root path.");
118
119 hr = RegOpen(HKEY_LOCAL_MACHINE, sczPath, KEY_READ, phk);
120 PolcExitOnFailure(hr, "Failed to open policy registry key.");
121
122LExit:
123 ReleaseStr(sczPath);
124
125 return hr;
126}