Why does Reg­Notify­Change­Key­Value stop notifying once the key is deleted?

Raymond Chen

The Reg­Notify­Change­Key­Value function lets you when when a change occurs to a registry key. But once the key is deleted, the notifications stop. There is no notification if a new key is created with the same name. Why not?

Well, because that’s a new key. It’s not the key that you passed to Reg­Notify­Change­Key­Value.

The end of a registry key’s lifecycle is its deletion. Once it’s gone, it’s not coming back. Any handle to the registry key is now a zombie. A new registry key be created with the same name as the one that was deleted, but that’s a new key, which is not the same as the key that you were monitoring.

It’s like saying, “You see that house at 123 Main Street? Let me know when ownership of that house changes.” When the house is sold, you get a notification.

And then you get a notification that the house has been demolished.

That’s the last notification you’ll get. If a new house is built at 123 Main Street, you won’t get any notifications about changes in ownership of that house, because that’s not the house that you subscribed to. Your subscription is for the house that was demolished, and it’s still demolished.

(A more mundane example would be monitoring changes in a file. Once the file is deleted, its lifetime is over, and there won’t be any more notifications. If another file is created with the same name, well, it’s nice that it has the same name, but it’s not the same file. It’s a brand new file that happens to have the same name as the old one.)

So what can you do if you want to monitor the new key, too?

Open the new key and call Reg­Notify­Change­Key­Value to start monitoring it.

If the new key doesn’t exist yet, you could create it. If you don’t want to create it, but you want to be notified when it is created, then you’ll have to set a notification on its parent (or closest ancestor that exists), watching for creation of subkeys. (You don’t need to watch recursively. You’ll get notified with the direct child key is created, at which point you can find the closest extent ancestor and repeat.)

5 comments

Discussion is closed. Login to edit/delete existing comments.

  • Gacel Perfinian 0

    Raymond, the old links from blogs.msdn.microsoft.com (and probably the variant with the msdn.com one) is broken.

  • Henke37 0

    And then we get into the matter of the file deletion example not working because of the file swap trick… Possibly.

  • Mystery Man 0

    The analogy in this post, I’m afraid, is flawed. Landowners do not lose their ownership of their lands when a building on the land gets demolished. And even though you might hear the term “that house is sold”, it is, in fact, the land that is sold.

    I’m not sure why you tried to explain things with a real-world analogy. The title already scares away anyone not familiar with the subject.

    • Henke37 0

      Some people really do own the house but not the land. Said people are generally up for a bad time.

      • cheong00 0

        Yup.

        Technically all land deeds in Hong Kong will expire on 2047, and this had caused some problem with banks on mortgage, and the government “land auction”.

Feedback usabilla icon