Solved

get class string of registry key

Posted on 2001-07-28
9
609 Views
Last Modified: 2011-09-20
How can i get the classname of a registry key?

just like this:

var
 openkeyhandle : hkey;
 classname     : string;

begin
 RegOpenKeyEx(HKEY_LOCAL_MACHINE, PChar('reg_subkey'), 0,KEY_ALL_ACCESS, OpenKeyHandle);
 
 classname := getClassname(openKeyHandle);

 regclosekey(openkeyhandle);

end;

I've tried RegQueryInfoKey and RegEnumKeyEx but
i couldn't get the classname:

var
 openkeyhandle : hkey;
 ClassName : array of char;
 classnameLength : Cardinal;

begin
 RegOpenKeyEx(HKEY_LOCAL_MACHINE, PChar('reg_subkey'), 0,KEY_ALL_ACCESS, OpenKeyHandle);
 
 SetLEngth(ClassName, 255);
 ClassNameLength := Length(ClassName);

RegQueryInfoKey (OpenKeyHandle, PCHAR(ClassName), @ClassNameLength,
                 NIL, NIL, NIL, NIL, NIL, NIL, NIL, NIL, NIL);

 SetLEngth(ClassName, 0);

 regclosekey(openkeyhandle);
end;

I hope anybody can help me!!!
Bug





0
Comment
Question by:bugroger
  • 4
  • 3
  • 2
9 Comments
 
LVL 17

Expert Comment

by:inthe
ID: 6329864
hi,
er registry keys dont have classnames ?
they are just strings or dwords etc..

what information exactly are you trying to get ?
0
 
LVL 2

Author Comment

by:bugroger
ID: 6329885

i want to get the registry path form a HKEY.
ex.:
 
RegOpenKeyEx(HKEY_LOCAL_MACHINE, PChar('Software\Microsoft'), 0,KEY_ALL_ACCESS, OpenKeyHandle);
 s := getClassName(OpenKeyHandle);  

 -> s = 'HKEY_LOCAL_MACHINE\Software\Microsoft'


0
 
LVL 2

Author Comment

by:bugroger
ID: 6329890
I think i have chosen the wrong expression.
I mean "Class String".

0
 
LVL 20

Expert Comment

by:Madshi
ID: 6329911
Hi Barry, look at the documentation about RegEnumKeyEx, there you'll find that there is really something like a class name for each registry key. But only in NT based systems. And it's not what Bug meant. Just wanted to mention that, because you said, there would be no class names in the registry...   :-)

Bug, what you want is not possible by using documented APIs, I'm sorry...   :-(

Regards, Madshi.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 17

Expert Comment

by:inthe
ID: 6330117
mmm yep i see what you mean and should be returned in second param of RegQueryInfoKey as you have done above but none of my tests worked.
i know RegQueryInfoKey was working cause i used all the values instead of nil's and could get all other infos like amount of subkeys and amount of values etc but no class string .maybe it just dont work,i dont see any point in needing it or any code in other language that works either (doesnt work in vc++ either).
0
 
LVL 17

Accepted Solution

by:
inthe earned 50 total points
ID: 6330148
ahh got it :-)

the key needs to be actually created with the class string .
usually it is ignored therefore majority of keys done have it,i havent found one that has one except my test ones..

below ive done example ,create a key with a class string and then you can read it anytime later using pretty much your code from above:


type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  TempKeyStr : pchar = 'Software\BarryTest';
  TempKey : HKey;
  ClassStr : array[0..254] of char;

 implementation

{$R *.DFM}

uses registry;

procedure TForm1.Button1Click(Sender: TObject);
var
r : tregistry;
begin
r := tregistry.create;
with r do try
rootkey := HKEY_LOCAL_MACHINE;
begin
RegCreateKeyEx(rootkey,TempKeyStr, 0 ,pchar('test'),
REG_OPTION_NON_VOLATILE,KEY_ALL_ACCESS,nil,TempKey,nil);
end;
finally
r.closekey;
end;
r.free;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
r : tregistry;
OpenKeyHandle : HKEY;
classStrLength : Cardinal;
begin
classStr := '';
r := tregistry.create;
with r do try
rootkey := HKEY_LOCAL_MACHINE;
begin
RegOpenKeyEx(rootkey,TempKeyStr,0,KEY_ALL_ACCESS,OpenKeyHandle);
ClassStrLength := Length(ClassStr);
RegQueryInfoKey(OpenKeyHandle,ClassStr, @ClassStrLength,
                NIL, NIL, NIL, NIL, NIL, NIL, NIL, NIL, NIL);
end;
finally
r.closekey;
end;
r.free;
showmessage(classStr);
end;


so basically the problem is not with your code its just because the keys were never created with a class string.

Regards Barry
0
 
LVL 2

Author Comment

by:bugroger
ID: 6330299
Sorry, but your code does not work Barry.
I think, like Madshi said,
 it only works "in NT based systems".

And it's not really what i want.

But you have answered "this" question and so
you will get the points :-)


Madshi,
 you will get some points too!
 Look at Question "Points for Madshi"


Thanks,
 Bug


 
0
 
LVL 17

Expert Comment

by:inthe
ID: 6330322
thanks,i am using win2k so the code works fine for me.

says in the help file about differences for nt and win95,the return handles have difference results on each if they are invalid etc..
0
 
LVL 20

Expert Comment

by:Madshi
ID: 6330373
Hi Barry, Bug wanted to have the key name from the key handle. He was talking about the "class name" but didn't mean it.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now