怎么样从hKey这个句柄得到具体的打开键值 ( 积分: 50 )

  • 主题发起人 主题发起人 菜鸟的问题
  • 开始时间 开始时间

菜鸟的问题

Unregistered / Unconfirmed
GUEST, unregistred user!
hKey这个是由RegOpenkeyExW或是RegCreatekeyExW打开的句柄,怎么由这个句柄得到具体的键值?<br>能否告诉下是哪个API?
 
在Delphi中如何对注册表进行操作<br><br>Windows注册表跟踪记录了操作系统和各种在系统上安装的应用程序的有关信息。注册表是一个分层数据库。该数据库包含以特定格式表示的配置信息,并且可以直接通过注册表编辑器程序编辑,也可以通过应用程序修改。在早期的Windows版本中,INI文件粗略地完成了这一功能,尽管注册表与INI文件有一些相似之处,但不管怎样,由于组织结构的原因,注册表比传统的INI文件要复杂得多。&nbsp;<br>注册表分成若干个主要部分,各个部分(或者说关键字)包含了不同种类的系统信息。比如说,当你创建一个应用程序时,你可以在HKEY_LOCAL_MACHINE这个部分的SOFTWARE子节中注册你的配置信息。应用程序可以把配置信息以多种数据类型(包括字符串、货币、日期、浮点数和布尔值)的形式保存在关键字中。本文通过实例介绍在Delphi的应用程序中如何对Windows&nbsp;98的注册表进行操作(如:创建关键字、获取一个关键字的数据值、删除关键字、删除关键字中的数据值等),下列示例程序均在Delphi&nbsp;4.0中调试通过,请放心使用。&nbsp;<br>Delphi中定义了一个Tregistry类,通过使用这个类中封装的很多有关对注册表操作的方法和属性可以完成对注册表的操作。&nbsp;<br>1、&nbsp;在注册表中创建一个新的关键字&nbsp;<br>Tregistry类中有一个CreateKey方法,使用该方法可以在注册表中创建一个新的关键字,该方法的原型声明为:function&nbsp;CreateKey(const&nbsp;Key:&nbsp;string)&nbsp;:&nbsp;Boolean;&nbsp;&nbsp;<br>示例代码如下:&nbsp;<br>unit&nbsp;passwd;&nbsp;<br>interface&nbsp;<br>uses&nbsp;<br>Windows,&nbsp;Messages,&nbsp;SysUtils,&nbsp;Classes,&nbsp;Graphics,&nbsp;Controls,&nbsp;Forms,&nbsp;Dialogs,&nbsp;<br>StdCtrls,&nbsp;Registry;&nbsp;<br>type&nbsp;<br>Tpassword&nbsp;=&nbsp;class(TForm)&nbsp;<br>Label1:&nbsp;TLabel;&nbsp;<br>Button1:&nbsp;TButton;&nbsp;<br>procedure&nbsp;Button1Click(Sender:&nbsp;TObject);&nbsp;<br>private&nbsp;<br>{&nbsp;Private&nbsp;declarations&nbsp;}&nbsp;<br>public&nbsp;<br>{&nbsp;Public&nbsp;declarations&nbsp;}&nbsp;<br>end;&nbsp;<br>implementation&nbsp;<br>{$R&nbsp;*.DFM}&nbsp;<br>procedure&nbsp;Tpassword.Button1Click(Sender:&nbsp;TObject);&nbsp;<br>var&nbsp;<br>MyReg&nbsp;:&nbsp;TRegistry;&nbsp;<br>begin&nbsp;<br>MyReg&nbsp;:=&nbsp;TRegistry.Create;&nbsp;<br>MyReg.RootKey&nbsp;:=&nbsp;HKEY_LOCAL_MACHINE;&nbsp;<br>try&nbsp;<br>if&nbsp;MyReg.OpenKey('/SOFTWARE/',FALSE)&nbsp;then&nbsp;<br>if&nbsp;not&nbsp;MyReg.KeyExists('Passwd')&nbsp;then&nbsp;<br>begin&nbsp;<br>MyReg.CreateKey('Passwd');&nbsp;<br>if&nbsp;MyReg.OpenKey('/SOFTWARE/Passwd',FALSE)&nbsp;then&nbsp;<br>Label1.Caption&nbsp;:=&nbsp;'关键字Passwd已建立!'&nbsp;<br>else&nbsp;<br>Label1.Caption&nbsp;:=&nbsp;'关键字Passwd无法建立!';&nbsp;<br>end&nbsp;<br>else&nbsp;<br>Label1.Caption&nbsp;:=&nbsp;'关键字Passwd已经存在!'&nbsp;<br>else&nbsp;<br>Label1.Caption&nbsp;:=&nbsp;'注册表打不开!';&nbsp;<br>MyReg.CloseKey;&nbsp;<br>finally&nbsp;<br>MyReg.Free;&nbsp;<br>end;&nbsp;<br>end;&nbsp;<br>end.&nbsp;<br>2、&nbsp;向注册表关键字中写入相关的数据值&nbsp;<br>在Tregistry类中提供了一系列的Write方法用来写入与当前关键字相关的数据值。常用方法的原型定义如下:&nbsp;<br>procedure&nbsp;WriteString(const&nbsp;Name,&nbsp;Value&nbsp;:&nbsp;string);&nbsp;<br>procedure&nbsp;WriteInteger(const&nbsp;Name&nbsp;:&nbsp;string&nbsp;;&nbsp;Value&nbsp;:&nbsp;Integer);&nbsp;<br>procedure&nbsp;WriteFloat(const&nbsp;Name&nbsp;:&nbsp;string&nbsp;;&nbsp;Value&nbsp;:&nbsp;Double);&nbsp;<br>procedure&nbsp;WriteTime(const&nbsp;Name&nbsp;:&nbsp;string&nbsp;;&nbsp;Value&nbsp;:&nbsp;TDateTime);&nbsp;<br>procedure&nbsp;WriteBool(const&nbsp;Name&nbsp;:&nbsp;string&nbsp;;&nbsp;Value&nbsp;:&nbsp;Boolean);&nbsp;<br>示例代码:&nbsp;<br>procedure&nbsp;TForm1.Button1Click(Sender:&nbsp;TObject);&nbsp;<br>var&nbsp;<br>MyReg&nbsp;:&nbsp;TRegistry;&nbsp;<br>begin&nbsp;<br>MyReg&nbsp;:=&nbsp;TRegistry.Create;&nbsp;<br>MyReg.RootKey&nbsp;:=&nbsp;HKEY_LOCAL_MACHINE;&nbsp;<br>try&nbsp;<br>if&nbsp;not&nbsp;MyReg.OpenKey('/SOFTWARE/',FALSE)&nbsp;then&nbsp;;&nbsp;<br>if&nbsp;not&nbsp;MyReg.KeyExists('Passwd')&nbsp;then&nbsp;<br>MyReg.CreateKey('Passwd');&nbsp;<br>if&nbsp;not&nbsp;MyReg.OpenKey('/SOFTWARE/Passwd',FALSE)&nbsp;then&nbsp;;&nbsp;<br>MyReg.WriteString('pwd1','mypassword1');&nbsp;<br>MyReg.WriteInteger('pd2',19642);&nbsp;<br>MyReg.CloseKey;&nbsp;<br>finally&nbsp;<br>MyReg.Free;&nbsp;<br>end;&nbsp;<br>end;&nbsp;<br>3、&nbsp;从注册表关键字中读出相关的数据值&nbsp;<br>在Tregistry类中还提供了与Write方法相对应用的用来读出与当前关键字相关的数据值。常用方法的原型定义如下:&nbsp;<br>founction&nbsp;ReadString(const&nbsp;Name&nbsp;:&nbsp;string)&nbsp;:&nbsp;string;&nbsp;<br>founction&nbsp;ReadInteger(const&nbsp;Name&nbsp;:&nbsp;string)&nbsp;:&nbsp;Integer;&nbsp;<br>founction&nbsp;ReadFloat(const&nbsp;Name&nbsp;:&nbsp;string)&nbsp;:&nbsp;Double;&nbsp;&nbsp;<br>founction&nbsp;ReadTime(const&nbsp;Name&nbsp;:&nbsp;string)&nbsp;:&nbsp;TdateTime;&nbsp;<br>founction&nbsp;ReadBool(const&nbsp;Name)&nbsp;:&nbsp;Boolean;&nbsp;&nbsp;<br>示例程序如下:&nbsp;<br>procedure&nbsp;TForm1.Button1Click(Sender:&nbsp;TObject);&nbsp;<br>var&nbsp;<br>MyReg&nbsp;:&nbsp;TRegistry;&nbsp;<br>begin&nbsp;<br>MyReg&nbsp;:=&nbsp;TRegistry.Create;&nbsp;<br>MyReg.RootKey&nbsp;:=&nbsp;HKEY_LOCAL_MACHINE;&nbsp;<br>try&nbsp;<br>if&nbsp;not&nbsp;MyReg.OpenKey('/SOFTWARE/',FALSE)&nbsp;then&nbsp;;&nbsp;<br>if&nbsp;not&nbsp;MyReg.KeyExists('Passwd')&nbsp;then&nbsp;;&nbsp;<br>if&nbsp;not&nbsp;MyReg.OpenKey('/SOFTWARE/Passwd',FALSE)&nbsp;then&nbsp;;&nbsp;<br>Label1.Caption&nbsp;:=&nbsp;MyReg.ReadString('pwd1');&nbsp;<br>Label2.Caption&nbsp;:=&nbsp;IntToStr(MyReg.ReadInteger('pd2'));&nbsp;<br>MyReg.CloseKey;&nbsp;<br>finally&nbsp;<br>MyReg.Free;&nbsp;<br>end;&nbsp;<br>end;&nbsp;<br>4、&nbsp;从注册表删除关键字或指定的数据值&nbsp;<br>使用Tregistry中提供的DeleteKey和DeleteValue方法可以删除指定的关键字和数据值。这两个方法的原型定义如下:&nbsp;<br>function&nbsp;DeleteKey(const&nbsp;Key&nbsp;:&nbsp;string)&nbsp;:&nbsp;Boolean;&nbsp;<br>function&nbsp;DeleteValue(const&nbsp;Key&nbsp;:&nbsp;string)&nbsp;:&nbsp;Boolean;&nbsp;<br>使用DeleteKey方法删除指定的关键字时,如果被删除的关键字在任何层次有子关键字,它们将同时被删除。上面两个方法在执行时,如果删除成功,则返回True;否则返回False。&nbsp;<br>示例程序如下:&nbsp;<br>procedure&nbsp;TForm1.Button1Click(Sender:&nbsp;TObject);&nbsp;<br>var&nbsp;<br>MyReg&nbsp;:&nbsp;TRegistry;&nbsp;<br>begin&nbsp;<br>MyReg&nbsp;:=&nbsp;TRegistry.Create;&nbsp;<br>MyReg.RootKey&nbsp;:=&nbsp;HKEY_LOCAL_MACHINE;&nbsp;<br>try&nbsp;<br>if&nbsp;not&nbsp;MyReg.OpenKey('/SOFTWARE/Passwd',FALSE)&nbsp;then&nbsp;;&nbsp;<br>if&nbsp;MyReg.DeleteValue('pwd1')&nbsp;then&nbsp;<br>label1.Caption&nbsp;:=&nbsp;'value&nbsp;[pwd1]&nbsp;has&nbsp;deleted!';&nbsp;<br>MyReg.CloseKey;&nbsp;<br>if&nbsp;not&nbsp;MyReg.OpenKey('/SOFTWARE/',FALSE)&nbsp;then&nbsp;;&nbsp;<br>if&nbsp;MyReg.DeleteKey('Passwd')&nbsp;then&nbsp;<br>label2.Caption&nbsp;:=&nbsp;'Key&nbsp;[passwd]&nbsp;has&nbsp;deleted!';&nbsp;<br>MyReg.CloseKey;&nbsp;<br>finally&nbsp;<br>MyReg.Free;&nbsp;<br>end;&nbsp;<br>end;&nbsp;<br>通过上面的实例我们简单介绍了Delphi中对注册表的相关操作。在Tregistry类中还有很多对注册表的关键字和数据值的操作方法,但其基本的操作方法和上面的示例程序在同小异,限于篇幅就不赘述了。
 
楼上的,你的答案里面没有回答我的问题,不过还是谢谢你
 
RegQueryValueEx
 
Function&nbsp;ReadRegistryInfo(Key:string):String;<br>var<br>&nbsp;&nbsp;Hwnd_key&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:HKEY;<br>&nbsp;&nbsp;IpcbData,&nbsp;Iptype&nbsp;&nbsp;:PDWORD;<br>&nbsp;&nbsp;Data&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:PByte;<br>begin<br>&nbsp;&nbsp;IpcbData&nbsp;:=&nbsp;PDword(2048);<br>&nbsp;&nbsp;Iptype&nbsp;&nbsp;&nbsp;:=&nbsp;PDWORD(2048);<br>&nbsp;&nbsp;GetMem(Data,&nbsp;2048);<br>&nbsp;&nbsp;if&nbsp;RegOpenKey(HKEY_LOCAL_MACHINE,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'SOFTWARE/Microsoft/Windows&nbsp;NT/CurrentVersion/Winlogon/',<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Hwnd_key)&lt;&gt;&nbsp;ERROR_SUCCESS&nbsp;then<br>&nbsp;&nbsp;&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MessageBox(0,'打开注册表错误!','提示',MB_ICONWARNING);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Exit;<br>&nbsp;&nbsp;&nbsp;&nbsp;end;<br>&nbsp;&nbsp;if&nbsp;RegQueryValueEx(Hwnd_Key,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PChar(Key),<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nil,@Iptype,Data,@IpcbData)&lt;&gt;&nbsp;ERROR_SUCCESS&nbsp;then<br>&nbsp;&nbsp;&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MessageBox(0,'读取注册表错误!','提示',MB_ICONWARNING);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Exit;<br>&nbsp;&nbsp;&nbsp;&nbsp;end;<br>&nbsp;&nbsp;Result:=PChar(Data);<br>&nbsp;&nbsp;FreeMem(Data);<br>&nbsp;&nbsp;RegCloseKey(Hwnd_Key);<br>end;
 
好像没有倒过来的函数啊!
 
没有这样的函数,因为键与路径并非一一对应的,你可以仔细看看TRegistry类的代码,也可以看看RegMon的源代码。包括我自己写的“Sundy注册表监控”软件都采用相同的方法,都是从最开始(创建/打开注册表键)就记录“路径-键”对信息,到最后你只要根据记录的信息就知道键对应的路径了。<br>RegMon采用HASH表,我的采用链表。都可以很好的工作。
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
1K
DelphiTeacher的专栏
D
后退
顶部