@@ -271,15 +271,16 @@ private void DeleteKeyInRegistryView(string keyName, RegistryView registryView)
271271 }
272272 }
273273
274- private object GetValueInRegistryView ( string keyName , string valueName , RegistryView registryView ) {
274+ private object GetValueInRegistryView ( string keyName , string valueName , out RegistryValueKind ? valueKind , RegistryView registryView ) {
275275 using ( RegistryKey registryKey = OpenKeyInRegistryView ( keyName , false , registryView ) ) {
276+ valueKind = null ;
277+
276278 if ( registryKey == null ) {
277279 // key does not exist
278280 return null ;
279281 }
280282
281283 object value = registryKey . GetValue ( valueName , null , RegistryValueOptions . DoNotExpandEnvironmentNames ) ;
282- RegistryValueKind ? valueKind = null ;
283284
284285 try {
285286 valueKind = GetValueKindInRegistryView ( keyName , valueName , registryView ) ;
@@ -331,8 +332,24 @@ private void SetValueInRegistryView(string keyName, string valueName, object val
331332 }
332333 break ;
333334 }
334-
335- registryKey . SetValue ( valueName , value , valueKind ) ;
335+
336+ try {
337+ registryKey . SetValue ( valueName , value , valueKind ) ;
338+ } catch {
339+ try {
340+ object _value = GetValueInRegistryView ( keyName , valueName , out RegistryValueKind ? _valueKind , registryView ) ;
341+
342+ // if it's an exact string match, don't worry about it
343+ if ( valueKind == _valueKind ) {
344+ if ( ( value as string ) . Equals ( _value as string , StringComparison . Ordinal ) ) {
345+ return ;
346+ }
347+ }
348+ } catch {
349+ // fail silently
350+ }
351+ throw ;
352+ }
336353 }
337354 }
338355
@@ -538,7 +555,7 @@ private bool CompareKeys(RegistryView registryView, RegistryStateElement registr
538555 return true ;
539556 }
540557
541- private bool CompareValues ( string value , RegistryView registryView , RegistryStateElement registryStateElement , RegistryStateElement activeRegistryStateElement , string activeCurrentUser = null , bool activeAdministrator = true ) {
558+ private bool CompareValues ( string value , RegistryValueKind ? valueKind , RegistryView registryView , RegistryStateElement registryStateElement , RegistryStateElement activeRegistryStateElement , string activeCurrentUser = null , bool activeAdministrator = true ) {
542559 // caller needs to decide what to do if value is null
543560 if ( value == null ) {
544561 throw new ArgumentNullException ( "The value is null." ) ;
@@ -552,32 +569,6 @@ private bool CompareValues(string value, RegistryView registryView, RegistryStat
552569 }
553570 }
554571
555- RegistryValueKind ? valueKind = null ;
556-
557- try {
558- valueKind = GetValueKindInRegistryView (
559- GetUserKeyValueName (
560- registryStateElement . KeyName ,
561- activeCurrentUser ,
562- activeAdministrator
563- ) ,
564-
565- registryStateElement . ValueName ,
566- registryView
567- ) ;
568- } catch ( SecurityException ex ) {
569- // value exists but we can't get it
570- LogExceptionToLauncher ( ex ) ;
571- throw new TaskRequiresElevationException ( "Accessing the value \" " + registryStateElement . ValueName + "\" in key \" " + registryStateElement . KeyName + "\" requires elevation." ) ;
572- } catch ( UnauthorizedAccessException ex ) {
573- // value exists but we can't get it
574- LogExceptionToLauncher ( ex ) ;
575- throw new TaskRequiresElevationException ( "Accessing the value \" " + registryStateElement . ValueName + "\" in key \" " + registryStateElement . KeyName + "\" requires elevation." ) ;
576- } catch {
577- // value doesn't exist
578- valueKind = null ;
579- }
580-
581572 string registryStateElementValue = registryStateElement . Value ;
582573
583574 if ( registryStateElementValue != null ) {
@@ -903,33 +894,19 @@ public override void Activate(string templateName) {
903894 // it should not get saved
904895 keyName = GetUserKeyValueName ( registryStateElement . KeyName ) ;
905896 value = null ;
906-
907- try {
908- valueKind = GetValueKindInRegistryView ( keyName , registryStateElement . ValueName , registryView ) ;
909- } catch ( SecurityException ex ) {
910- // value exists but we can't get it
911- LogExceptionToLauncher ( ex ) ;
912- throw new TaskRequiresElevationException ( "Accessing the value \" " + registryStateElement . ValueName + "\" in key \" " + keyName + "\" requires elevation." ) ;
913- } catch ( UnauthorizedAccessException ex ) {
914- // value exists but we can't get it
915- LogExceptionToLauncher ( ex ) ;
916- throw new TaskRequiresElevationException ( "Accessing the value \" " + registryStateElement . ValueName + "\" in key \" " + keyName + "\" requires elevation." ) ;
917- } catch {
918- // value doesn't exist
919- valueKind = null ;
920- }
897+ valueKind = null ;
921898
922899 activeRegistryStateElement = new RegistryStateElement {
923900 Type = registryStateElement . Type ,
924901 KeyName = registryStateElement . KeyName ,
925- ValueName = registryStateElement . ValueName ,
926- ValueKind = valueKind
902+ ValueName = registryStateElement . ValueName
927903 } ;
928904
929905 if ( registryStateElement . Type == TYPE . KEY ) {
930906 // we create a key
931907 activeRegistryStateElement . Type = TYPE . KEY ;
932908 activeRegistryStateElement . Value = null ;
909+ activeRegistryStateElement . ValueKind = null ;
933910
934911 try {
935912 activeRegistryStateElement . _Deleted = TestKeyDeletedInRegistryView ( keyName , registryView ) ;
@@ -954,7 +931,7 @@ public override void Activate(string templateName) {
954931 }
955932
956933 try {
957- value = GetValueInRegistryView ( keyName , registryStateElement . ValueName , registryView ) as string ;
934+ value = GetValueInRegistryView ( keyName , registryStateElement . ValueName , out valueKind , registryView ) as string ;
958935 } catch ( SecurityException ex ) {
959936 // value exists but we can't get it
960937 LogExceptionToLauncher ( ex ) ;
@@ -990,12 +967,14 @@ public override void Activate(string templateName) {
990967 // or, we edit a value that exists
991968 activeRegistryStateElement . Type = TYPE . VALUE ;
992969 activeRegistryStateElement . Value = value ;
970+ activeRegistryStateElement . ValueKind = valueKind ;
993971 activeRegistryStateElement . _Deleted = null ;
994972 } else {
995973 // we create a value
996974 // the value, and the key it belonged to, does not exist
997975 activeRegistryStateElement . Type = TYPE . KEY ;
998976 activeRegistryStateElement . Value = null ;
977+ activeRegistryStateElement . ValueKind = null ;
999978 activeRegistryStateElement . _Deleted = keyDeleted ;
1000979 }
1001980 }
@@ -1166,6 +1145,7 @@ public void Deactivate(MODIFICATIONS_REVERT_METHOD modificationsRevertMethod) {
11661145 bool activeAdministrator = activeModificationsElement . RegistryStates . _Administrator . GetValueOrDefault ( ) ;
11671146 string keyName = null ;
11681147 string value = null ;
1148+ RegistryValueKind ? valueKind = null ;
11691149 bool clear = false ;
11701150
11711151 if ( activeAdministrator && ! TestLaunchedAsAdministratorUser ( ) ) {
@@ -1211,7 +1191,7 @@ public void Deactivate(MODIFICATIONS_REVERT_METHOD modificationsRevertMethod) {
12111191 keyName = GetUserKeyValueName ( registryStateElement . KeyName , activeCurrentUser , activeAdministrator ) ;
12121192
12131193 try {
1214- value = GetValueInRegistryView ( keyName , registryStateElement . ValueName , registryView ) as string ;
1194+ value = GetValueInRegistryView ( keyName , registryStateElement . ValueName , out valueKind , registryView ) as string ;
12151195 } catch ( SecurityException ex ) {
12161196 // value exists but we can't get it
12171197 LogExceptionToLauncher ( ex ) ;
@@ -1237,6 +1217,7 @@ public void Deactivate(MODIFICATIONS_REVERT_METHOD modificationsRevertMethod) {
12371217 // value still exists
12381218 if ( ! CompareValues (
12391219 value ,
1220+ valueKind ,
12401221 registryView ,
12411222 registryStateElement ,
12421223 activeRegistryStateElement ,
@@ -1255,6 +1236,7 @@ public void Deactivate(MODIFICATIONS_REVERT_METHOD modificationsRevertMethod) {
12551236 } else {
12561237 if ( ! CompareValues (
12571238 value ,
1239+ valueKind ,
12581240 registryView ,
12591241 registryStateElement ,
12601242 activeRegistryStateElement ,
@@ -1531,6 +1513,7 @@ private void ModificationAdded(RegistryTraceData registryTraceData) {
15311513 // http://learn.microsoft.com/en-us/windows/win32/etw/registry-typegroup1
15321514 ulong safeKeyHandle = registryTraceData . KeyHandle & 0x00000000FFFFFFFF ;
15331515 string value = null ;
1516+ RegistryValueKind ? valueKind = null ;
15341517 RegistryView registryView = modificationsElement . RegistryStates . BinaryType == BINARY_TYPE . SCS_64BIT_BINARY ? RegistryView . Registry64 : RegistryView . Registry32 ;
15351518
15361519 if ( safeKeyHandle == 0 ) {
@@ -1540,19 +1523,15 @@ private void ModificationAdded(RegistryTraceData registryTraceData) {
15401523 modificationsElement . RegistryStates . BinaryType
15411524 ) ;
15421525
1543- try {
1544- registryStateElement . ValueKind = GetValueKindInRegistryView ( registryStateElement . KeyName , registryStateElement . ValueName , registryView ) ;
1545- } catch {
1546- // value doesn't exist
1547- registryStateElement . ValueKind = null ;
1548- }
1549-
1526+ valueKind = null ;
1527+
15501528 try {
15511529 value = ReplaceStartupPathEnvironmentVariable (
15521530 LengthenValue (
15531531 GetValueInRegistryView (
15541532 registryStateElement . KeyName ,
15551533 registryStateElement . ValueName ,
1534+ out valueKind ,
15561535 registryView
15571536 ) as string ,
15581537
@@ -1578,6 +1557,7 @@ private void ModificationAdded(RegistryTraceData registryTraceData) {
15781557 }
15791558
15801559 registryStateElement . Type = TYPE . VALUE ;
1560+ registryStateElement . ValueKind = valueKind ;
15811561
15821562 if ( value == null ) {
15831563 try {
@@ -1613,19 +1593,15 @@ private void ModificationAdded(RegistryTraceData registryTraceData) {
16131593 modificationsElement . RegistryStates . BinaryType
16141594 ) ;
16151595
1616- try {
1617- registryStateElement . ValueKind = GetValueKindInRegistryView ( registryStateElement . KeyName , registryStateElement . ValueName , registryView ) ;
1618- } catch {
1619- // value doesn't exist
1620- registryStateElement . ValueKind = null ;
1621- }
1596+ valueKind = null ;
16221597
16231598 try {
16241599 value = ReplaceStartupPathEnvironmentVariable (
16251600 LengthenValue (
16261601 GetValueInRegistryView (
16271602 registryStateElement . KeyName ,
16281603 registryStateElement . ValueName ,
1604+ out valueKind ,
16291605 registryView
16301606 ) as string ,
16311607
@@ -1639,6 +1615,7 @@ private void ModificationAdded(RegistryTraceData registryTraceData) {
16391615 }
16401616
16411617 registryStateElement . Type = TYPE . VALUE ;
1618+ registryStateElement . ValueKind = valueKind ;
16421619
16431620 if ( value == null ) {
16441621 try {
@@ -1798,6 +1775,7 @@ private void KCBStopped(RegistryTraceData registryTraceData) {
17981775 // we'll be finding these in a second
17991776 RegistryStateElement registryStateElement = null ;
18001777 string value = null ;
1778+ RegistryValueKind ? valueKind = null ;
18011779
18021780 RegistryView registryView = modificationsElement . RegistryStates . BinaryType == BINARY_TYPE . SCS_64BIT_BINARY ? RegistryView . Registry64 : RegistryView . Registry32 ;
18031781
@@ -1823,19 +1801,15 @@ private void KCBStopped(RegistryTraceData registryTraceData) {
18231801 modificationsElement . RegistryStates . BinaryType
18241802 ) ;
18251803
1826- try {
1827- registryStateElement . ValueKind = GetValueKindInRegistryView ( registryStateElement . KeyName , registryStateElement . ValueName , registryView ) ;
1828- } catch {
1829- // value doesn't exist
1830- registryStateElement . ValueKind = null ;
1831- }
1804+ valueKind = null ;
18321805
18331806 try {
18341807 value = ReplaceStartupPathEnvironmentVariable (
18351808 LengthenValue (
18361809 GetValueInRegistryView (
18371810 registryStateElement . KeyName ,
18381811 registryStateElement . ValueName ,
1812+ out valueKind ,
18391813 registryView
18401814 ) as string ,
18411815
@@ -1861,6 +1835,7 @@ private void KCBStopped(RegistryTraceData registryTraceData) {
18611835 }
18621836
18631837 registryStateElement . Type = TYPE . VALUE ;
1838+ registryStateElement . ValueKind = valueKind ;
18641839
18651840 if ( value == null ) {
18661841 try {
0 commit comments