Maintaining a Clean Registry

[Previous] [Next]

As I have pointed out several times in this chapter, the responsibility of avoiding registry clutter rests on the application developer. Your server (or any other application you write) should not only attempt to make efficient use of the system registry but also clean up after itself when it is finished.

The system provides two functions for deleting data from the registry: RegDeleteKey and RegDeleteValue. These functions are prototyped as follows:

 LONG RegDeleteValue( HKEY hkey, PCTSTR pszValueName); LONG RegDeleteKey( HKEY hkey, PCTSTR pszSubKey); 

You might be surprised to find out that deleting data is not a completely trivial topic. The RegDeleteValue function is as simple as it looks, assuming your application has security permission to delete a particular key. However, the possibility of your application not having permission to delete a key forced the developers to implement RegDeleteKey with the following major restriction: RegDeleteKey will delete only a key that contains no subkeys. The reason for this restriction becomes clear with a little thought on the matter—your process might not have permission to delete a subkey in the tree below the key that you wish to delete. The system cannot simply traverse the tree and check security permissions before beginning the delete procedure, because doing so could be a lengthy operation, and the permissions might actually change on a subkey after the security check has passed. This, in turn, could cause the deletion of subkeys to fail in the middle of the delete operation, leaving a partially destroyed registry tree.

Although RegDeleteKey will not delete a key with subkeys, the developers of the Windows shell implemented a function similar to RegDeleteKey that will. This function is named SHDeleteKey and is defined as follows:

 DWORD SHDeleteKey( HKEY hkey, PCTSTR pszSubKey); 

Your process should have permission to delete all underlying keys before calling SHDeleteKey. If your process does not have permission to delete all underlying keys when it calls SHDeleteKey, the function detects this and returns "Access denied." Note that SHDeleteKey suffers from the same previously mentioned race condition in which, if permissions for a key change during a call to SHDeleteKey, SHDeleteKey can leave a partially deleted tree.



Programming Server-Side Applications for Microsoft Windows 2000
Programming Server-Side Applications for Microsoft Windows 2000 (Microsoft Programming)
ISBN: 0735607532
EAN: 2147483647
Year: 2000
Pages: 126

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net