How to open a registry key for reading data without requesting write access (Views: 29)
Problem/Question/Abstract: How can an application open a registry key for reading data without requesting write access? TRegistry seems to open keys always for ReadWrite, which fails on WindowsNT if the user has no write permission on that key (which is the default for HKEY_LOCAL_MACHINE if the user is no Administrator). I want to write user independant registry data into HKEY_LOCAL_MACHINE during installation (which is supposed to be the standard according to the WIN API Help) and have the program, which is normally not run by an administrator, to read these data on startup. Since everything in TRegistry is static - as always when I want to inherit from a VCL anchestor - I cannot simply write a descendant of TRegistry that overrides the OpenKey and GetKey methods. Do I have to patch the source or to copy and modify the whole TRegistry code or am I missing something obvious? Answer: Alternatively you can directly use the Win32API calls: {Local. Read the registry for the given key} function GetKeyValue(const key: string): string; var hkey: THandle; buf: array[0..255] of Char; n: Longint; begin Result := ''; if regOpenKeyEx(HKEY_CLASSES_ROOT, @key[1], 0, KEY_READ, hKey) = ERROR_SUCCESS then begin n := 255; if regQueryValue(hKey, nil, buf, n) = ERROR_SUCCESS then begin Result := StrPas(buf); end; RegCloseKey(hkey); end else Result := ''; end; {Local. Look through the Registry looking for the descriptions of the given extension.} function GetDescription(Extension: string): string; var intermediate: string; begin {Get intermediate value} intermediate := GetKeyValue(Extension); if intermediate <> '' then begin {Look up definition for the full description} Result := GetKeyValue(intermediate); end else Result := ''; end; {Local. Read the registry for the description of the given file extension.} function getExtensionDescription(const extension: string): string; var description: string; begin {Try to get the description from the registry} description := GetDescription(extension); {Make sure we have a description to present to the user} if description = '' then description := extension + ' file'; {Return the description to the caller} Result := description; end; |