怎样读取注册表中的二进制值并与一个checkBox1关联? ( 积分: 100 )

L

LGS

Unregistered / Unconfirmed
GUEST, unregistred user!
我需要读取注册表中
HKEY_CURRENT_USER, 'Software/Microsoft/Windows/CurrentVersion/Explorer'下“link"中的值,当“link"的二进制值为“00 00 00 00”时让复选框checkBox1处于选中状态,否则使checkBox1处于未选中状态。
我试了很多种方法都在读取二进制值时出错,请各位给予帮助!
 
procedure TForm1.Button3Click(Sender: TObject);
var
sReg: TRegistry;
sBuf: array[0..1024] of char;
begin
sReg := TRegistry.Create;
try
sReg.RootKey := HKEY_CURRENT_USER;
sReg.OpenKey('Software/Microsoft/Windows/CurrentVersion/Explorer',False);
sReg.ReadBinaryData('link',sBuf,SizeOf(sBuf));
ShowMessage(String(sBuf));
finally
sReg.CloseKey;
sReg.Free;
end;
end;
 
procedure TForm1.Button2Click(Sender: TObject);
var
sReg: TRegistry;
sBuf: array[0..1024] of char;
aa:string;
i:integer;
begin
sReg := TRegistry.Create;
try
sReg.RootKey := HKEY_CURRENT_USER;
sReg.OpenKey('Software/Microsoft/Windows/CurrentVersion/Explorer',False);
// aa:=inttostr(sReg.ReadBinaryData('link',sBuf,SizeOf(sBuf)))+'';
for i := 2 to sReg.ReadBinaryData('Link',sbuf,sizeof(sbuf)) do
aa := aa + ' ' + inttohex(dword(sbuf[i-1]),2);

ShowMessage(aa);
finally
sReg.CloseKey;
sReg.Free;
end;
end;

这样子,刚好读出来的是' 00 00 00'你判断就好了,如果是这样子的字符串,就checkbox1.checked:=true;
 
用liyinwei兄的代码最后显示的结果是一个向左的“▲”符号
asksomeone的代码显示的结果为“00 00 00”,而实际的结果是“00 00 00 00”
好像和实际的结果不合,还有更好的吗
 
function RegReadBuffer(KeyName, Name: string; RootKey: HKEY =
HKEY_LOCAL_MACHINE): string;
var
Buffer: array of Byte;
hReg: HKEY;
I:Integer;
cbType, cbResult: DWORD;
begin
cbType := REG_NONE;
try
if (RegOpenKeyEx(RootKey, PAnsiChar(KeyName), 0, KEY_QUERY_VALUE, hReg) =
ERROR_SUCCESS)
and (RegQueryValueEx(hReg, PChar(Name), nil, @cbType, nil, @cbResult) =
ERROR_SUCCESS)
and (cbType=REG_BINARY) then
begin
SetLength(Buffer,cbResult);
if RegQueryValueEx(hReg, PChar(Name), nil, nil, @Buffer[0],
@cbResult) = ERROR_SUCCESS then
for I:=0 to cbResult-1 do
begin
if Result<>'' then Result:=Result+' '
Result:=Result+IntToHex(Buffer,2);
end
else
raise Excetion.Create('ERROR!');
end
else
raise Excetion.Create('ERROR!');
finally
RegCloseKey(hReg);
end;
end;
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
577
import
I
顶部