Author: William Gerbert
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:
1 2 {Local. Read the registry for the given key}3 4 function GetKeyValue(const key: string): string;
5 var6 hkey: THandle;
7 buf: array[0..255] of Char;
8 n: Longint;
9 begin10 Result := '';
11 if regOpenKeyEx(HKEY_CLASSES_ROOT, @key[1], 0, KEY_READ, hKey) = ERROR_SUCCESS
12 then13 begin14 n := 255;
15 if regQueryValue(hKey, nil, buf, n) = ERROR_SUCCESS then16 begin17 Result := StrPas(buf);
18 end;
19 RegCloseKey(hkey);
20 end21 else22 Result := '';
23 end;
24 25 {Local. Look through the Registry looking for the descriptions of the given 26 extension.}27 28 function GetDescription(Extension: string): string;
29 var30 intermediate: string;
31 begin32 {Get intermediate value}33 intermediate := GetKeyValue(Extension);
34 if intermediate <> '' then35 begin36 {Look up definition for the full description}37 Result := GetKeyValue(intermediate);
38 end39 else40 Result := '';
41 end;
42 43 {Local. Read the registry for the description of the given file extension.}44 45 function getExtensionDescription(const extension: string): string;
46 var47 description: string;
48 begin49 {Try to get the description from the registry}50 description := GetDescription(extension);
51 {Make sure we have a description to present to the user}52 if description = '' then53 description := extension + ' file';
54 {Return the description to the caller}55 Result := description;
56 end;