@joebigelow / wix / commits / 128c4604

Add support for REG_EXPAND_SZ

Rob Mensching committed Jul 23, 2018 at 21:38 UTC 128c46049fb73f18edd248d2a6e80a933cbe2d80
1 file changed +60 -27
src/dutil/regutil.cpp
+60 -27
@@ -17,6 +17,13 @@ static PFN_REGDELETEVALUEW vpfnRegDeleteValueW = ::RegDeleteValueW;
17 static HMODULE vhAdvApi32Dll = NULL;
18 static BOOL vfRegInitialized = FALSE;
19
20 +static HRESULT WriteStringToRegistry(
21 + __in HKEY hk,
22 + __in_z_opt LPCWSTR wzName,
23 + __in_z_opt LPCWSTR wzValue,
24 + __in DWORD dwType
25 +);
26 +
27 /********************************************************************
28 RegInitialize - initializes regutil
29
@@ -221,7 +228,7 @@ extern "C" HRESULT DAPI RegDelete(
228 while (E_NOMOREITEMS != (hr = RegKeyEnum(hkKey, 0, &pszEnumeratedSubKey)))
229 {
230 ExitOnFailure(hr, "Failed to enumerate key 0");
224 -
231 +
232 hr = PathConcat(wzSubKey, pszEnumeratedSubKey, &pszRecursiveSubKey);
233 ExitOnFailure(hr, "Failed to concatenate paths while recursively deleting subkeys. Path1: %ls, Path2: %ls", wzSubKey, pszEnumeratedSubKey);
234
@@ -733,6 +740,21 @@ LExit:
740 }
741
742
743 +/********************************************************************
744 +RegWriteExpandString - writes a registry key value as an expand string.
745 +
746 +Note: if wzValue is NULL the value will be removed.
747 +*********************************************************************/
748 +extern "C" HRESULT DAPI RegWriteExpandString(
749 + __in HKEY hk,
750 + __in_z_opt LPCWSTR wzName,
751 + __in_z_opt LPCWSTR wzValue
752 +)
753 +{
754 + return WriteStringToRegistry(hk, wzName, wzValue, REG_EXPAND_SZ);
755 +}
756 +
757 +
758 /********************************************************************
759 RegWriteString - writes a registry key value as a string.
760
@@ -744,30 +766,7 @@ extern "C" HRESULT DAPI RegWriteString(
766 __in_z_opt LPCWSTR wzValue
767 )
768 {
747 - HRESULT hr = S_OK;
748 - DWORD er = ERROR_SUCCESS;
749 - DWORD cbValue = 0;
750 -
751 - if (wzValue)
752 - {
753 - hr = ::StringCbLengthW(wzValue, DWORD_MAX, reinterpret_cast<size_t*>(&cbValue));
754 - ExitOnFailure(hr, "Failed to determine length of registry value: %ls", wzName);
755 -
756 - er = vpfnRegSetValueExW(hk, wzName, 0, REG_SZ, reinterpret_cast<const BYTE *>(wzValue), cbValue);
757 - ExitOnWin32Error(er, hr, "Failed to set registry value: %ls", wzName);
758 - }
759 - else
760 - {
761 - er = vpfnRegDeleteValueW(hk, wzName);
762 - if (ERROR_FILE_NOT_FOUND == er || ERROR_PATH_NOT_FOUND == er)
763 - {
764 - er = ERROR_SUCCESS;
765 - }
766 - ExitOnWin32Error(er, hr, "Failed to delete registry value: %ls", wzName);
767 - }
768 -
769 -LExit:
770 - return hr;
769 + return WriteStringToRegistry(hk, wzName, wzValue, REG_SZ);
770 }
771
772
@@ -791,7 +790,7 @@ extern "C" HRESULT DAPI RegWriteStringFormatted(
790 va_end(args);
791 ExitOnFailure(hr, "Failed to allocate %ls value.", wzName);
792
794 - hr = RegWriteString(hk, wzName, sczValue);
793 + hr = WriteStringToRegistry(hk, wzName, sczValue, REG_SZ);
794
795 LExit:
796 ReleaseStr(sczValue);
@@ -845,7 +844,7 @@ HRESULT DAPI RegWriteStringArray(
844 {
845 hr = ::StringCchCopyW(wzCopyDestination, dwTotalStringSize, rgwzValues[i]);
846 ExitOnFailure(hr, "failed to copy string: %ls", rgwzValues[i]);
848 -
847 +
848 dwTemp -= lstrlenW(rgwzValues[i]) + 1;
849 wzCopyDestination += lstrlenW(rgwzValues[i]) + 1;
850 }
@@ -924,3 +923,37 @@ extern "C" HRESULT DAPI RegQueryKey(
923 LExit:
924 return hr;
925 }
926 +
927 +
928 +static HRESULT WriteStringToRegistry(
929 + __in HKEY hk,
930 + __in_z_opt LPCWSTR wzName,
931 + __in_z_opt LPCWSTR wzValue,
932 + __in DWORD dwType
933 +)
934 +{
935 + HRESULT hr = S_OK;
936 + DWORD er = ERROR_SUCCESS;
937 + DWORD cbValue = 0;
938 +
939 + if (wzValue)
940 + {
941 + hr = ::StringCbLengthW(wzValue, DWORD_MAX, reinterpret_cast<size_t*>(&cbValue));
942 + ExitOnFailure(hr, "Failed to determine length of registry value: %ls", wzName);
943 +
944 + er = vpfnRegSetValueExW(hk, wzName, 0, dwType, reinterpret_cast<const BYTE *>(wzValue), cbValue);
945 + ExitOnWin32Error(er, hr, "Failed to set registry value: %ls", wzName);
946 + }
947 + else
948 + {
949 + er = vpfnRegDeleteValueW(hk, wzName);
950 + if (ERROR_FILE_NOT_FOUND == er || ERROR_PATH_NOT_FOUND == er)
951 + {
952 + er = ERROR_SUCCESS;
953 + }
954 + ExitOnWin32Error(er, hr, "Failed to delete registry value: %ls", wzName);
955 + }
956 +
957 +LExit:
958 + return hr;
959 +}