求救,在Delphi中使用RegOpenKeyEx函数打开注册表???

喜欢编程朋友 2003-10-21 03:28:29
我在工作中遇到了这个问题,不知道该怎么解决,就是在Delphi中怎样使用RegOpenKeyEx的一系列函数访问注册表,并且老是提出HKEY类型不存在,怎样解决,请各位高手帮忙!!!很感激。而我在VC++中这些函数都能很容易运行执行,这是怎么回事。
...全文
245 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
cnhgj 2003-10-22
  • 打赏
  • 举报
回复
uses Registry;

var
rg:Tregistry;

rg:=Tregistry.create;
rg.rootkey:=HKEY_LOCAL_MACHINE;
rg.openkey('SOFTWARE\asdf',true);
rg.writestring('test','test);
rg.closekey;
rg.free;
  • 打赏
  • 举报
回复
var reg:treginifile;
begin
reg:=treginifile.create('xdq');
Reg.RootKey:=HKey_Local_user;
Reg.WriteString('Section1','Value1','12');
Reg.WriteString('Section1','Value2','34');
Reg.Free;
end;
kk_ray 2003-10-22
  • 打赏
  • 举报
回复
最好的办法——看帮助,要什么有什么!
hongqi162 2003-10-21
  • 打赏
  • 举报
回复
1、在注册表中创建一个新的关键字 Tregistry类中有一个CreateKey方法,使用该方法可以在注册表中创建一个新的关键字,该方法的原型声明为:
function CreateKey(const Key: string) : Boolean;
示例代码如下:
unit passwd;
interface
uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, Registry; type Tpassword = class(TForm)
Label1: TLabel;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
implementation
{$R *.DFM}
procedure Tpassword.Button1Click(Sender: TObject); var MyReg : TRegistry;
begin MyReg := TRegistry.Create; MyReg.RootKey := HKEY_LOCAL_MACHINE;
try
if MyReg.OpenKey('\SOFTWARE\',FALSE) then
if not MyReg.KeyExists('Passwd') then
begin MyReg.CreateKey('Passwd');
if MyReg.OpenKey('\SOFTWARE\Passwd',FALSE) then
Label1.Caption := '关键字Passwd已建立!'
else Label1.Caption := '关键字Passwd无法建立!';
end
else Label1.Caption := '关键字Passwd已经存在!'
else Label1.Caption := '注册表打不开!';
MyReg.CloseKey;
finally
MyReg.Free;
end;
end;
end.

2、向注册表关键字中写入相关的数据值 在Tregistry类中提供了一系列的Write方法用来写入与当前关键字相关的数据值。常用方法的原型定义如下:
procedure WriteString(const Name, Value : string);
procedure WriteInteger(const Name : string ; Value : Integer);
procedure WriteFloat(const Name : string ; Value : Double);
procedure WriteTime(const Name : string ; Value : TDateTime);
procedure WriteBool(const Name : string ; Value : Boolean);
示例代码:
procedure TForm1.Button1Click(Sender: TObject); var MyReg : TRegistry;
begin MyReg := TRegistry.Create; MyReg.RootKey := HKEY_LOCAL_MACHINE;
try
if not MyReg.OpenKey('\SOFTWARE\',FALSE) then
if not MyReg.KeyExists('Passwd') then
MyReg.CreateKey('Passwd');
If not MyReg.OpenKey('\SOFTWARE\Passwd',FALSE) then MyReg.WriteString('pwd1','mypassword1');
MyReg.WriteInteger('pd2',19642);
MyReg.CloseKey;
finally
MyReg.Free;
end;
end;

3 从注册表关键字中读出相关的数据值 在Tregistry类中还提供了与Write方法相对应用的用来读出与当前关键字相关的数据值。常用方法的原型定义如下:
founction ReadString(const Name : string) : string;
founction ReadInteger(const Name : string) : Integer;
founction ReadFloat(const Name : string) : Double;
founction ReadTime(const Name : string) : TdateTime;
founction ReadBool(const Name) : Boolean;
示例程序如下:
procedure TForm1.Button1Click(Sender: TObject); var MyReg : TRegistry;
begin
MyReg := TRegistry.Create; MyReg.RootKey := HKEY_LOCAL_MACHINE;
try
if not MyReg.OpenKey('\SOFTWARE\',FALSE) then
if not MyReg.KeyExists('Passwd') then
if not MyReg.OpenKey('\SOFTWARE\Passwd',FALSE) then
Label1.Caption := MyReg.ReadString('pwd1');
Label2.Caption := IntToStr(MyReg.ReadInteger('pd2'));
MyReg.CloseKey;
Finally
MyReg.Free;
end;
end;
chechy 2003-10-21
  • 打赏
  • 举报
回复
好像应该是TRegIniFile吧,手边没有Delphi。
chechy 2003-10-21
  • 打赏
  • 举报
回复
干吗不用TRegIni类呢?非常Easy,用API多累啊。

1,184

社区成员

发帖
与我相关
我的任务
社区描述
Delphi Windows SDK/API
社区管理员
  • Windows SDK/API社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧