在DELPHI写入注册表的问题? (100分)

  • 主题发起人 主题发起人 ngdsjck
  • 开始时间 开始时间
N

ngdsjck

Unregistered / Unconfirmed
GUEST, unregistred user!
用DELPHI开发注册表修改工具,请问如何写入二进制值,请举一个例了?
 
procedure TForm1.Button1Click(Sender: TObject);
var
Reg: TRegistry;
begin
Reg := TRegistry.Create;
Reg.RootKey := HKEY_LOCAL_MACHINE;
if Reg.OpenKey('/Hardware/Description/System'
+ '/CentralProcessor/0', False) then begin
ShowMessage(Reg.ReadString('Identifier'));
Reg.CloseKey;
end; // if
Reg.Free;
end;
 
SEVN:
我提出的问题是如何在注册表中写入二进制数据?而不是字符串。
 
看这个
原版的Delphi7的帮助,最权威了
Retrieves a binary value from a specified data value associated with the current key.
Delphi syntax:
function ReadBinaryData(const Name: String; var Buffer; BufSize: Integer): Integer;
C++ syntax:
int __fastcall ReadBinaryData(const AnsiString Name, void *Buffer, int BufSize);
Description
Call ReadBinaryData to read a binary value from a specified data value associated with the current key. Name is the name of the data value to read. Buffer is the application variable into which to read the registry data. Buffer must be large enough to hold all of the data returned. BufSize specifies the size of Buffer.
If successful, ReadBinaryData writes the requested data into Buffer and returns the number of bytes written. If the Registry entry contains a known type (such as a string), ReadBinaryData raises an exception.
Note: Binary data is typically a complex data structure (a record in Delphi or struct in C++). It might also be an icon or a bitmap although Microsoft recommends against storing graphics objects in the registry for performance reasons.
 
var
Reg: TRegistry;
Buf: integer;
begin
Buf:=2; //二进制的十进制值,自己转换.
Reg := TRegistry.Create;
try
Reg.RootKey := HKEY_LOCAL_MACHINE;
if Reg.OpenKey('/Software/New', False) then //自己新建的New主键
begin
Reg.WriteBinaryData('Binary',Buf,SizeOf(Buf)); //写入Binary键值
end
finally
Reg.CloseKey;
Reg.Free;
end;
end;
 
我程序代码中的语句!
var
flags: DWORD;
begin
flage:=0;
with TRegistry.Create do
try
RootKey:=HKEY_CURRENT_USER;
if Run then
begin
OpenKey('/Software/Microsoft/Windows/CurrentVersion/Explorer',True);
WriteBinaryData('Link',flags,sizeof(flags))
end
else
begin
OpenKey('/Software/Microsoft/Windows/CurrentVersion/Explorer',False);
DeleteValue('Link');
end;
CloseKey;
finally
Free;
end;
 
往注册表中写二进制值关键在于要先写入缓存,再写入注册表,分两步做就没有问题!
看看写入二进制的方法的定义:
Procedure WriteBinaryData(const Name:string;var Buffer;BufSize:integer);
Name:包含待存数据键值的名称;Buffer:一条记录或是数据缓冲区;BufSize:指定Buffer的大小
只要理解上面过程,你甚至可以将你的图片数据写到注册表中!
 
http://www.delphibbs.com/delphibbs/dispq.asp?lid=1260020
 
多人接受答案了。
 
后退
顶部