Add butil exit macros.
Sean Hall committed
Aug 29, 2020 at 21:28 UTC
281ad838c5001f988aeea06a6f06ce2cc6c0991d
2 files changed
+78
-63
src/dutil/butil.cpp
+46
-60
@@ -1,7 +1,19 @@
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
-#include "butil.h"
4
+
5
+// Exit macros
6
+#define ButilExitOnLastError(x, s, ...) ExitOnLastErrorSource(DUTIL_SOURCE_BUTIL, x, s, __VA_ARGS__)
7
+#define ButilExitOnLastErrorDebugTrace(x, s, ...) ExitOnLastErrorDebugTraceSource(DUTIL_SOURCE_BUTIL, x, s, __VA_ARGS__)
8
+#define ButilExitWithLastError(x, s, ...) ExitWithLastErrorSource(DUTIL_SOURCE_BUTIL, x, s, __VA_ARGS__)
9
+#define ButilExitOnFailure(x, s, ...) ExitOnFailureSource(DUTIL_SOURCE_BUTIL, x, s, __VA_ARGS__)
10
+#define ButilExitOnRootFailure(x, s, ...) ExitOnRootFailureSource(DUTIL_SOURCE_BUTIL, x, s, __VA_ARGS__)
11
+#define ButilExitOnFailureDebugTrace(x, s, ...) ExitOnFailureDebugTraceSource(DUTIL_SOURCE_BUTIL, x, s, __VA_ARGS__)
12
+#define ButilExitOnNull(p, x, e, s, ...) ExitOnNullSource(DUTIL_SOURCE_BUTIL, p, x, e, s, __VA_ARGS__)
13
+#define ButilExitOnNullWithLastError(p, x, s, ...) ExitOnNullWithLastErrorSource(DUTIL_SOURCE_BUTIL, p, x, s, __VA_ARGS__)
14
+#define ButilExitOnNullDebugTrace(p, x, e, s, ...) ExitOnNullDebugTraceSource(DUTIL_SOURCE_BUTIL, p, x, e, s, __VA_ARGS__)
15
+#define ButilExitOnInvalidHandleWithLastError(p, x, s, ...) ExitOnInvalidHandleWithLastErrorSource(DUTIL_SOURCE_BUTIL, p, x, s, __VA_ARGS__)
16
+#define ButilExitOnWin32Error(e, x, s, ...) ExitOnWin32ErrorSource(DUTIL_SOURCE_BUTIL, e, x, s, __VA_ARGS__)
17
18
// constants
19
// From engine/registration.h
@@ -10,31 +22,20 @@ const LPCWSTR BUNDLE_REGISTRATION_REGISTRY_BUNDLE_UPGRADE_CODE = L"BundleUpgrade
22
const LPCWSTR BUNDLE_REGISTRATION_REGISTRY_BUNDLE_PROVIDER_KEY = L"BundleProviderKey";
23
24
// Forward declarations.
25
+/********************************************************************
26
+OpenBundleKey - Opens the bundle uninstallation key for a given bundle
27
+
28
+NOTE: caller is responsible for closing key
29
+********************************************************************/
30
static HRESULT OpenBundleKey(
14
- __in LPCWSTR wzBundleId,
31
+ __in_z LPCWSTR wzBundleId,
32
__in BUNDLE_INSTALL_CONTEXT context,
33
__inout HKEY *key);
34
18
-/********************************************************************
19
-BundleGetBundleInfo - Queries the bundle installation metadata for a given property
20
-
21
-RETURNS:
22
- E_INVALIDARG
23
- An invalid parameter was passed to the function.
24
- HRESULT_FROM_WIN32(ERROR_UNKNOWN_PRODUCT)
25
- The bundle is not installed
26
- HRESULT_FROM_WIN32(ERROR_UNKNOWN_PROPERTY)
27
- The property is unrecognized
28
- HRESULT_FROM_WIN32(ERROR_MORE_DATA)
29
- A buffer is too small to hold the requested data.
30
- E_NOTIMPL:
31
- Tried to read a bundle attribute for a type which has not been implemented
32
-
33
- All other returns are unexpected returns from other dutil methods.
34
-********************************************************************/
35
+
36
extern "C" HRESULT DAPI BundleGetBundleInfo(
36
- __in LPCWSTR wzBundleId,
37
- __in LPCWSTR wzAttribute,
37
+ __in_z LPCWSTR wzBundleId,
38
+ __in_z LPCWSTR wzAttribute,
39
__out_ecount_opt(*pcchValueBuf) LPWSTR lpValueBuf,
40
__inout_opt LPDWORD pcchValueBuf
41
)
@@ -51,39 +52,39 @@ extern "C" HRESULT DAPI BundleGetBundleInfo(
52
53
if ((lpValueBuf && !pcchValueBuf) || !wzBundleId || !wzAttribute)
54
{
54
- ExitOnFailure(hr = E_INVALIDARG, "An invalid parameter was passed to the function.");
55
+ ButilExitOnFailure(hr = E_INVALIDARG, "An invalid parameter was passed to the function.");
56
}
57
58
if (FAILED(hr = OpenBundleKey(wzBundleId, context = BUNDLE_INSTALL_CONTEXT_MACHINE, &hkBundle)) &&
59
FAILED(hr = OpenBundleKey(wzBundleId, context = BUNDLE_INSTALL_CONTEXT_USER, &hkBundle)))
60
{
60
- ExitOnFailure(E_FILENOTFOUND == hr ? HRESULT_FROM_WIN32(ERROR_UNKNOWN_PRODUCT) : hr, "Failed to locate bundle uninstall key path.");
61
+ ButilExitOnFailure(E_FILENOTFOUND == hr ? HRESULT_FROM_WIN32(ERROR_UNKNOWN_PRODUCT) : hr, "Failed to locate bundle uninstall key path.");
62
}
63
64
// If the bundle doesn't have the property defined, return ERROR_UNKNOWN_PROPERTY
65
hr = RegGetType(hkBundle, wzAttribute, &dwType);
65
- ExitOnFailure(E_FILENOTFOUND == hr ? HRESULT_FROM_WIN32(ERROR_UNKNOWN_PROPERTY) : hr, "Failed to locate bundle property.");
66
+ ButilExitOnFailure(E_FILENOTFOUND == hr ? HRESULT_FROM_WIN32(ERROR_UNKNOWN_PROPERTY) : hr, "Failed to locate bundle property.");
67
68
switch (dwType)
69
{
70
case REG_SZ:
71
hr = RegReadString(hkBundle, wzAttribute, &sczValue);
71
- ExitOnFailure(hr, "Failed to read string property.");
72
+ ButilExitOnFailure(hr, "Failed to read string property.");
73
break;
74
case REG_DWORD:
75
hr = RegReadNumber(hkBundle, wzAttribute, &dwValue);
75
- ExitOnFailure(hr, "Failed to read dword property.");
76
+ ButilExitOnFailure(hr, "Failed to read dword property.");
77
78
hr = StrAllocFormatted(&sczValue, L"%d", dwValue);
78
- ExitOnFailure(hr, "Failed to format dword property as string.");
79
+ ButilExitOnFailure(hr, "Failed to format dword property as string.");
80
break;
81
default:
81
- ExitOnFailure(hr = E_NOTIMPL, "Reading bundle info of type 0x%x not implemented.", dwType);
82
+ ButilExitOnFailure(hr = E_NOTIMPL, "Reading bundle info of type 0x%x not implemented.", dwType);
83
84
}
85
86
hr = ::StringCchLengthW(sczValue, STRSAFE_MAX_CCH, reinterpret_cast<UINT_PTR*>(&cchSource));
86
- ExitOnFailure(hr, "Failed to calculate length of string");
87
+ ButilExitOnFailure(hr, "Failed to calculate length of string");
88
89
if (lpValueBuf)
90
{
@@ -91,11 +92,11 @@ extern "C" HRESULT DAPI BundleGetBundleInfo(
92
if (*pcchValueBuf <= cchSource)
93
{
94
*pcchValueBuf = ++cchSource;
94
- ExitOnFailure(hr = HRESULT_FROM_WIN32(ERROR_MORE_DATA), "A buffer is too small to hold the requested data.");
95
+ ButilExitOnFailure(hr = HRESULT_FROM_WIN32(ERROR_MORE_DATA), "A buffer is too small to hold the requested data.");
96
}
97
98
hr = ::StringCchCatNExW(lpValueBuf, *pcchValueBuf, sczValue, cchSource, NULL, NULL, STRSAFE_FILL_BEHIND_NULL);
98
- ExitOnFailure(hr, "Failed to copy the property value to the output buffer.");
99
+ ButilExitOnFailure(hr, "Failed to copy the property value to the output buffer.");
100
101
*pcchValueBuf = cchSource++;
102
}
@@ -107,19 +108,8 @@ LExit:
108
return hr;
109
}
110
110
-/********************************************************************
111
-BundleEnumRelatedBundle - Queries the bundle installation metadata for installs with the given upgrade code
112
-
113
-NOTE: lpBundleIdBuff is a buffer to receive the bundle GUID. This buffer must be 39 characters long.
114
- The first 38 characters are for the GUID, and the last character is for the terminating null character.
115
-RETURNS:
116
- E_INVALIDARG
117
- An invalid parameter was passed to the function.
118
-
119
- All other returns are unexpected returns from other dutil methods.
120
-********************************************************************/
111
HRESULT DAPI BundleEnumRelatedBundle(
122
- __in LPCWSTR wzUpgradeCode,
112
+ __in_z LPCWSTR wzUpgradeCode,
113
__in BUNDLE_INSTALL_CONTEXT context,
114
__inout PDWORD pdwStartIndex,
115
__out_ecount(MAX_GUID_CHARS+1) LPWSTR lpBundleIdBuf
@@ -141,22 +131,22 @@ HRESULT DAPI BundleEnumRelatedBundle(
131
132
if (!wzUpgradeCode || !lpBundleIdBuf || !pdwStartIndex)
133
{
144
- ExitOnFailure(hr = E_INVALIDARG, "An invalid parameter was passed to the function.");
134
+ ButilExitOnFailure(hr = E_INVALIDARG, "An invalid parameter was passed to the function.");
135
}
136
137
hr = RegOpen(hkRoot, BUNDLE_REGISTRATION_REGISTRY_UNINSTALL_KEY, KEY_READ, &hkUninstall);
148
- ExitOnFailure(hr, "Failed to open bundle uninstall key path.");
138
+ ButilExitOnFailure(hr, "Failed to open bundle uninstall key path.");
139
140
for (DWORD dwIndex = *pdwStartIndex; !fUpgradeCodeFound; dwIndex++)
141
{
142
hr = RegKeyEnum(hkUninstall, dwIndex, &sczUninstallSubKey);
153
- ExitOnFailure(hr, "Failed to enumerate bundle uninstall key path.");
143
+ ButilExitOnFailure(hr, "Failed to enumerate bundle uninstall key path.");
144
145
hr = StrAllocFormatted(&sczUninstallSubKeyPath, L"%ls\\%ls", BUNDLE_REGISTRATION_REGISTRY_UNINSTALL_KEY, sczUninstallSubKey);
156
- ExitOnFailure(hr, "Failed to allocate bundle uninstall key path.");
146
+ ButilExitOnFailure(hr, "Failed to allocate bundle uninstall key path.");
147
148
hr = RegOpen(hkRoot, sczUninstallSubKeyPath, KEY_READ, &hkBundle);
159
- ExitOnFailure(hr, "Failed to open uninstall key path.");
149
+ ButilExitOnFailure(hr, "Failed to open uninstall key path.");
150
151
// If it's a bundle, it should have a BundleUpgradeCode value of type REG_SZ (old) or REG_MULTI_SZ
152
hr = RegGetType(hkBundle, BUNDLE_REGISTRATION_REGISTRY_BUNDLE_UPGRADE_CODE, &dwType);
@@ -173,7 +163,7 @@ HRESULT DAPI BundleEnumRelatedBundle(
163
{
164
case REG_SZ:
165
hr = RegReadString(hkBundle, BUNDLE_REGISTRATION_REGISTRY_BUNDLE_UPGRADE_CODE, &sczValue);
176
- ExitOnFailure(hr, "Failed to read BundleUpgradeCode string property.");
166
+ ButilExitOnFailure(hr, "Failed to read BundleUpgradeCode string property.");
167
if (CSTR_EQUAL == ::CompareStringW(LOCALE_INVARIANT, NORM_IGNORECASE, sczValue, -1, wzUpgradeCode, -1))
168
{
169
*pdwStartIndex = dwIndex;
@@ -186,7 +176,7 @@ HRESULT DAPI BundleEnumRelatedBundle(
176
break;
177
case REG_MULTI_SZ:
178
hr = RegReadStringArray(hkBundle, BUNDLE_REGISTRATION_REGISTRY_BUNDLE_UPGRADE_CODE, &rgsczBundleUpgradeCodes, &cBundleUpgradeCodes);
189
- ExitOnFailure(hr, "Failed to read BundleUpgradeCode multi-string property.");
179
+ ButilExitOnFailure(hr, "Failed to read BundleUpgradeCode multi-string property.");
180
181
for (DWORD i = 0; i < cBundleUpgradeCodes; i++)
182
{
@@ -206,7 +196,7 @@ HRESULT DAPI BundleEnumRelatedBundle(
196
break;
197
198
default:
209
- ExitOnFailure(hr = E_NOTIMPL, "BundleUpgradeCode of type 0x%x not implemented.", dwType);
199
+ ButilExitOnFailure(hr = E_NOTIMPL, "BundleUpgradeCode of type 0x%x not implemented.", dwType);
200
201
}
202
@@ -215,10 +205,10 @@ HRESULT DAPI BundleEnumRelatedBundle(
205
if (lpBundleIdBuf)
206
{
207
hr = ::StringCchLengthW(sczUninstallSubKey, STRSAFE_MAX_CCH, reinterpret_cast<UINT_PTR*>(&cchUninstallSubKey));
218
- ExitOnFailure(hr, "Failed to calculate length of string");
208
+ ButilExitOnFailure(hr, "Failed to calculate length of string");
209
210
hr = ::StringCchCopyNExW(lpBundleIdBuf, MAX_GUID_CHARS + 1, sczUninstallSubKey, cchUninstallSubKey, NULL, NULL, STRSAFE_FILL_BEHIND_NULL);
221
- ExitOnFailure(hr, "Failed to copy the property value to the output buffer.");
211
+ ButilExitOnFailure(hr, "Failed to copy the property value to the output buffer.");
212
}
213
214
break;
@@ -241,13 +231,9 @@ LExit:
231
return hr;
232
}
233
244
-/********************************************************************
245
-OpenBundleKey - Opens the bundle uninstallation key for a given bundle
234
247
-NOTE: caller is responsible for closing key
248
-********************************************************************/
235
HRESULT OpenBundleKey(
250
- __in LPCWSTR wzBundleId,
236
+ __in_z LPCWSTR wzBundleId,
237
__in BUNDLE_INSTALL_CONTEXT context,
238
__inout HKEY *key)
239
{
@@ -259,10 +245,10 @@ HRESULT OpenBundleKey(
245
LPWSTR sczKeypath = NULL;
246
247
hr = StrAllocFormatted(&sczKeypath, L"%ls\\%ls", BUNDLE_REGISTRATION_REGISTRY_UNINSTALL_KEY, wzBundleId);
262
- ExitOnFailure(hr, "Failed to allocate bundle uninstall key path.");
248
+ ButilExitOnFailure(hr, "Failed to allocate bundle uninstall key path.");
249
250
hr = RegOpen(hkRoot, sczKeypath, KEY_READ, key);
265
- ExitOnFailure(hr, "Failed to open bundle uninstall key path.");
251
+ ButilExitOnFailure(hr, "Failed to open bundle uninstall key path.");
252
253
LExit:
254
ReleaseStr(sczKeypath);
src/dutil/inc/butil.h
+32
-3
@@ -12,15 +12,44 @@ enum BUNDLE_INSTALL_CONTEXT
12
BUNDLE_INSTALL_CONTEXT_USER,
13
};
14
15
+
16
+/********************************************************************
17
+BundleGetBundleInfo - Queries the bundle installation metadata for a given property
18
+
19
+RETURNS:
20
+ E_INVALIDARG
21
+ An invalid parameter was passed to the function.
22
+ HRESULT_FROM_WIN32(ERROR_UNKNOWN_PRODUCT)
23
+ The bundle is not installed
24
+ HRESULT_FROM_WIN32(ERROR_UNKNOWN_PROPERTY)
25
+ The property is unrecognized
26
+ HRESULT_FROM_WIN32(ERROR_MORE_DATA)
27
+ A buffer is too small to hold the requested data.
28
+ E_NOTIMPL:
29
+ Tried to read a bundle attribute for a type which has not been implemented
30
+
31
+ All other returns are unexpected returns from other dutil methods.
32
+********************************************************************/
33
HRESULT DAPI BundleGetBundleInfo(
16
- __in LPCWSTR szBundleId, // Bundle code
17
- __in LPCWSTR szAttribute, // attribute name
34
+ __in_z LPCWSTR szBundleId, // Bundle code
35
+ __in_z LPCWSTR szAttribute, // attribute name
36
__out_ecount_opt(*pcchValueBuf) LPWSTR lpValueBuf, // returned value, NULL if not desired
37
__inout_opt LPDWORD pcchValueBuf // in/out buffer character count
38
);
39
40
+/********************************************************************
41
+BundleEnumRelatedBundle - Queries the bundle installation metadata for installs with the given upgrade code
42
+
43
+NOTE: lpBundleIdBuff is a buffer to receive the bundle GUID. This buffer must be 39 characters long.
44
+ The first 38 characters are for the GUID, and the last character is for the terminating null character.
45
+RETURNS:
46
+ E_INVALIDARG
47
+ An invalid parameter was passed to the function.
48
+
49
+ All other returns are unexpected returns from other dutil methods.
50
+********************************************************************/
51
HRESULT DAPI BundleEnumRelatedBundle(
23
- __in LPCWSTR lpUpgradeCode,
52
+ __in_z LPCWSTR lpUpgradeCode,
53
__in BUNDLE_INSTALL_CONTEXT context,
54
__inout PDWORD pdwStartIndex,
55
__out_ecount(MAX_GUID_CHARS+1) LPWSTR lpBundleIdBuf