Programming with C/C++

The API examples and documentation in earlier sections all present the Registry API in its native C/C++ form. Since many administrators are comfortable with C and/or C++, I’ll start the programming examples by presenting three distinct uses for the Registry API routines I’ve already presented.

Example: Watching a Key for Changes

RegNotifyChangeKeyValue is a little-used, but very useful, routine. It’s only present in Windows 2000 and NT, which perhaps accounts for its relative anonymity. If you need to be notified when a key or its values changes, it’s the best tool for getting you that notification. WatchKey, shown in Example 8-4, is a small utility that takes advantage of RegNotifyChangeKeyValue to warn you when a key you specify has been changed.

How the code works

After a check of its initial command-line arguments, the code performs the following steps:

  1. It identifies which root key “owns” the key you want to monitor; this is required because RegOpenKeyEx needs an already open key (i.e., one of the roots) to open the target key. If it can’t figure out which root the user specified, it prints an error and exits.

  2. It captures the pathname of the key to monitor and uses it, along with the root key, to call RegOpenKeyEx. The key is opened with KEY_READ access, which includes KEY_NOTIFY access too. If the key can’t be opened, the code generates an error message and exits.

  3. The target key is monitored with a call to RegNotifyChangeKeyValue. The code passes ...

Get Managing The Windows 2000 Registry now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.