Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to open a registry key for reading data without requesting write access Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
28-Sep-02
Category
Win API
Language
Delphi 2.x
Views
108
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			 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   var
6     hkey: THandle;
7     buf: array[0..255] of Char;
8     n: Longint;
9   begin
10    Result := '';
11    if regOpenKeyEx(HKEY_CLASSES_ROOT, @key[1], 0, KEY_READ, hKey) = ERROR_SUCCESS 
12  then
13    begin
14      n := 255;
15      if regQueryValue(hKey, nil, buf, n) = ERROR_SUCCESS then
16      begin
17        Result := StrPas(buf);
18      end;
19      RegCloseKey(hkey);
20    end
21    else
22      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  var
30    intermediate: string;
31  begin
32    {Get intermediate value}
33    intermediate := GetKeyValue(Extension);
34    if intermediate <> '' then
35    begin
36      {Look up definition for the full description}
37      Result := GetKeyValue(intermediate);
38    end
39    else
40      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  var
47    description: string;
48  begin
49    {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 = '' then
53      description := extension + ' file';
54    {Return the description to the caller}
55    Result := description;
56  end;


			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC