Skip to content

Commit

Permalink
Merge pull request #287 from raisingthefloor/master
Browse files Browse the repository at this point in the history
Morphic for Windows v1.3.1
  • Loading branch information
christopher-rtf authored Jul 20, 2021
2 parents 1f75dca + 774a0ec commit 1dce107
Showing 1 changed file with 25 additions and 5 deletions.
30 changes: 25 additions & 5 deletions Morphic.Windows.Native/Registry/RegistryKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -317,11 +317,26 @@ private static IMorphicResult SetValueForHandle(UIntPtr handle, string? name, ui

public IMorphicResult RegisterForValueChangeNotification(RegistryKeyChangedEvent eventHandler)
{
if (_disposed == true)
{
return IMorphicResult.ErrorResult;
}

var waitHandle = new ManualResetEvent(false);

// NOTE: REG_NOTIFY_CHANGE_LAST_SET will trigger on any changes to the key's values
// NOTE: registration will auto-unregister after the wait handle is trigger once. Registration will also auto-unregister when the RegistryKey is closed/disposed
var regNotifyErrorCode = PInvoke.AdvApi32.RegNotifyChangeKeyValue(_handle, false, PInvoke.AdvApi32.RegNotifyFilter.REG_NOTIFY_CHANGE_LAST_SET, waitHandle.SafeWaitHandle, true);
PInvoke.Win32ErrorCode regNotifyErrorCode;
try
{
// NOTE: if _handle has been disposed, this will throw an ObjectDisposedException
regNotifyErrorCode = PInvoke.AdvApi32.RegNotifyChangeKeyValue(_handle, false, PInvoke.AdvApi32.RegNotifyFilter.REG_NOTIFY_CHANGE_LAST_SET, waitHandle.SafeWaitHandle, true);
}
catch(ObjectDisposedException ex)
{
return IMorphicResult.ErrorResult;
}
//
switch (regNotifyErrorCode)
{
case PInvoke.Win32ErrorCode.ERROR_SUCCESS:
Expand Down Expand Up @@ -427,11 +442,16 @@ private static void ListenForRegistryKeyChanges()
}
}
}
// re-register the registry key for notification (using its existing event handler), since Windows auto-unregisters registrations every time the handle is triggered
var registerForValuechangeNotificationResult = notificationPoolEntry.RegistryKey.RegisterForValueChangeNotification(notificationPoolEntry.EventHandler);
if (registerForValuechangeNotificationResult.IsError)
//
// if the entry we just removed hasn't been disposed, re-register it for notifications
if(notificationPoolEntry.RegistryKey._disposed == false)
{
Debug.Assert(false, "Could not re-register registry key for notification after raising event.");
// re-register the registry key for notification (using its existing event handler), since Windows auto-unregisters registrations every time the handle is triggered
var registerForValuechangeNotificationResult = notificationPoolEntry.RegistryKey.RegisterForValueChangeNotification(notificationPoolEntry.EventHandler);
if (registerForValuechangeNotificationResult.IsError)
{
Debug.Assert(false, "Could not re-register registry key for notification after raising event.");
}
}
}
}
Expand Down

0 comments on commit 1dce107

Please sign in to comment.