From 5d8375007754101ff2889d0e79486c8f9b7cf5ab Mon Sep 17 00:00:00 2001 From: Rob Mensching Date: Sun, 3 Sep 2017 11:22:38 -0700 Subject: Initial commit --- src/dutil/polcutil.cpp | 111 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 src/dutil/polcutil.cpp (limited to 'src/dutil/polcutil.cpp') 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 @@ +// 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. + +#include "precomp.h" + +const LPCWSTR REGISTRY_POLICIES_KEY = L"SOFTWARE\\Policies\\"; + +static HRESULT OpenPolicyKey( + __in_z LPCWSTR wzPolicyPath, + __out HKEY* phk + ); + + +extern "C" HRESULT DAPI PolcReadNumber( + __in_z LPCWSTR wzPolicyPath, + __in_z LPCWSTR wzPolicyName, + __in DWORD dwDefault, + __out DWORD* pdw + ) +{ + HRESULT hr = S_OK; + HKEY hk = NULL; + + hr = OpenPolicyKey(wzPolicyPath, &hk); + if (E_FILENOTFOUND == hr || E_PATHNOTFOUND == hr) + { + ExitFunction1(hr = S_FALSE); + } + ExitOnFailure(hr, "Failed to open policy key: %ls", wzPolicyPath); + + hr = RegReadNumber(hk, wzPolicyName, pdw); + if (E_FILENOTFOUND == hr || E_PATHNOTFOUND == hr) + { + ExitFunction1(hr = S_FALSE); + } + ExitOnFailure(hr, "Failed to open policy key: %ls, name: %ls", wzPolicyPath, wzPolicyName); + +LExit: + ReleaseRegKey(hk); + + if (S_FALSE == hr || FAILED(hr)) + { + *pdw = dwDefault; + } + + return hr; +} + +extern "C" HRESULT DAPI PolcReadString( + __in_z LPCWSTR wzPolicyPath, + __in_z LPCWSTR wzPolicyName, + __in_z_opt LPCWSTR wzDefault, + __deref_out_z LPWSTR* pscz + ) +{ + HRESULT hr = S_OK; + HKEY hk = NULL; + + hr = OpenPolicyKey(wzPolicyPath, &hk); + if (E_FILENOTFOUND == hr || E_PATHNOTFOUND == hr) + { + ExitFunction1(hr = S_FALSE); + } + ExitOnFailure(hr, "Failed to open policy key: %ls", wzPolicyPath); + + hr = RegReadString(hk, wzPolicyName, pscz); + if (E_FILENOTFOUND == hr || E_PATHNOTFOUND == hr) + { + ExitFunction1(hr = S_FALSE); + } + ExitOnFailure(hr, "Failed to open policy key: %ls, name: %ls", wzPolicyPath, wzPolicyName); + +LExit: + ReleaseRegKey(hk); + + if (S_FALSE == hr || FAILED(hr)) + { + if (NULL == wzDefault) + { + ReleaseNullStr(*pscz); + } + else + { + hr = StrAllocString(pscz, wzDefault, 0); + } + } + + return hr; +} + + +// internal functions + +static HRESULT OpenPolicyKey( + __in_z LPCWSTR wzPolicyPath, + __out HKEY* phk + ) +{ + HRESULT hr = S_OK; + LPWSTR sczPath = NULL; + + hr = PathConcat(REGISTRY_POLICIES_KEY, wzPolicyPath, &sczPath); + ExitOnFailure(hr, "Failed to combine logging path with root path."); + + hr = RegOpen(HKEY_LOCAL_MACHINE, sczPath, KEY_READ, phk); + ExitOnFailure(hr, "Failed to open policy registry key."); + +LExit: + ReleaseStr(sczPath); + + return hr; +} -- cgit v1.2.3-55-g6feb