Registry Access through the Win32 API
By defining Win32 API functions using the Declare statement, you can access the entire registry and perform all operations that are supported by the Win32 API upon the registry. In order to do so, however, you need to define a number of constants in your code.
First, each top-level registry key corresponds to an intrinsic constant, as follows:
Quote:
Public Const HKEY_CLASSES_ROOT = &H80000000
Public Const HKEY_CURRENT_CONFIG = &H80000005
Public Const HKEY_CURRENT_USER = &H80000001
Public Const HKEY_LOCAL_MACHINE = &H80000002
Public Const HKEY_USERS = &H80000003
|
Second, each of the registry functions returns a numeric code indicating its success or failure. A successful function call returns the following:
Quote:
Public Const ERROR_SUCCESS = 0&
Among the constants representing error codes are the following:
Public Const ERROR_FILE_NOT_FOUND = 2& ' Registry path does not exist
Public Const ERROR_ACCESS_DENIED = 5& ' Requested permissions not available
Public Const ERROR_INVALID_HANDLE = 6& ' Invalid handle or top-level key
Public Const ERROR_BAD_NETPATH = 53 ' Network path not found
Public Const ERROR_INVALID_PARAMETER = 87 ' Bad parameter to a Win32 API function
Public Const ERROR_CALL_NOT_IMPLEMENTED = 120& ' Function valid only in WinNT/2000?XP
Public Const ERROR_INSUFFICIENT_BUFFER = 122 ' Buffer too small to hold data
Public Const ERROR_BAD_PATHNAME = 161 ' Registry path does not exist
Public Const ERROR_NO_MORE_ITEMS = 259& ' Invalid enumerated value
Public Const ERROR_BADDB = 1009 ' Corrupted registry
Public Const ERROR_BADKEY = 1010 ' Invalid registry key
Public Const ERROR_CANTOPEN = 1011& ' Cannot open registry key
Public Const ERROR_CANTREAD = 1012& ' Cannot read from registry key
Public Const ERROR_CANTWRITE = 1013& ' Cannot write to registry key
Public Const ERROR_REGISTRY_RECOVERED = 1014& ' Recovery of part of registry successful
Public Const ERROR_REGISTRY_CORRUPT = 1015& ' Corrupted registry
Public Const ERROR_REGISTRY_IO_FAILED = 1016& ' Input/output operation failed
Public Const ERROR_NOT_REGISTRY_FILE = 1017& ' Input file not in registry file format
Public Const ERROR_KEY_DELETED = 1018& ' Key already deleted
Public Const ERROR_KEY_HAS_CHILDREN = 1020& ' Key has subkeys & cannot be deleted
|
Third, some of the registry functions in the Win32 API have a parameter whose value is a security access mask indicating the rights that the current process must have in order for the function to execute successfully. The following are the possible values:
Quote:
Public Const KEY_CREATE_LINK = &H20
Public Const KEY_CREATE_SUB_KEY = &H4
Public Const KEY_ENUMERATE_SUB_KEYS = &H8
Public Const KEY_NOTIFY = &H10
Public Const KEY_QUERY_VALUE = &H1
Public Const KEY_READ = ((STANDARD_RIGHTS_READ Or KEY_QUERY_VALUE
Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY)
And (Not SYNCHRONIZE))
Public Const KEY_SET_VALUE = &H2
Public Const KEY_WRITE = ((STANDARD_RIGHTS_WRITE Or KEY_SET_VALUE
Or KEY_CREATE_SUB_KEY) And (Not SYNCHRONIZE))
Public Const KEY_EXECUTE = ((KEY_READ) And (Not SYNCHRONIZE))
Public Const KEY_ALL_ACCESS = ((STANDARD_RIGHTS_ALL Or KEY_QUERY_VALUE
Or KEY_SET_VALUE Or KEY_CREATE_SUB_KEY
Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY
Or KEY_CREATE_LINK) And (Not SYNCHRONIZE))
|
A bit of background about the operation of the registry functions in the Win32 is also important. Registry keys are system objects, and like any system object in the Win32 API, they are accessible through their handles. In other words, to work with a registry key, you must open it. In some form, the function that opens the key will return (usually in a parameter passed to the function by reference) a handle to the registry key, or hKey.