在32位和64位环境下,有一些有用的过程可以读取关键寄存器。首先查看您的系统是什么。
Type TTypWin32Or64 = (Bit32,Bit64);
var TypWin32Or64 :TTypWin32Or64;
Procedure TypeOS(var TypWin32Or64:TTypWin32Or64 ) ;
if DirectoryExists('c:\Windows\SysWOW64')
then TypWin32Or64:=Bit64
else TypWin32Or64:=Bit32;
Procedure for looking Key or Create it in a Registry
procedure TForm1.ReadKeyFromRegister(TypWin32Or64:TTypWin32Or64;
TypeKey:Longword;
PathToKey:string;
TypeValueRead :Integer;
NameValueToRead:string;
var ValueStrReaded:Ansistring;
var ValueByteReaded:Pchar;
ReadOnly:boolean);
var
Reg : TRegistry;
S_TStr : TStrings;
VersBios : string;
Pos0 : integer;
Key : HKEY;
Value : Array of Byte;
nValueLen : Integer;
const KEY_WOW64_64KEY = $0100 ;
begin
case TypWin32Or64 of
Bit32: Reg := TRegistry.Create;
Bit64:Reg := TRegistry.Create(KEY_WRITE OR KEY_WOW64_64KEY);
end;
try
Reg.RootKey := TypeKey;
if ReadOnly
then Reg.OpenKeyReadOnly(PathToKey)
else Reg.OpenKey(PathToKey, false);
case TypeValueRead of
REG_SZ: ValueStrReaded:=Reg.ReadString(NameValueToRead);
REG_BINARY : begin
nValueLen := Reg.GetDataSize(NameValueToRead);
ValueByteReaded:=Pchar(Reg.ReadBinaryData(NameValueToRead, PByte(Value), nValueLen));
end;
REG_DWORD : begin
end;
REG_MULTI_SZ: begin
S_TStr := ReadMultirowKey(Reg,NameValueToRead);
/in a enviroment 64 bit not good work - better use
VersBios:=S_TStr.Text;
Pos0:=Pos(#0,VersBios);
if Pos0 > 0 then
begin
delete(VersBios,Pos0,1);
insert(' ', VersBios,Pos0);
end;
VersBios := StringReplace(VersBios,#$D#$A, ' ' , [rfReplaceAll, rfIgnoreCase]);
ValueStrReaded:=VersBios;
end;
end;
Reg.CloseKey;
finally
Reg.Free;
end;
end;
Sample for reading system Key from Registry
Reading UsedWindowsVersion_Full
ReadKeyFromRegister(TypWin32Or64,
HKEY_LOCAL_MACHINE,
'SOFTWARE\Microsoft\Windows NT\CurrentVersion',
REG_SZ,
'BuildLabEx',
UsedWindowsVersion_Full,
sPchar ,
True );
For read ProductId(also called Product Key Windows)
ReadKeyFromRegister( TypWin32Or64,
HKEY_LOCAL_MACHINE,
'SOFTWARE\Microsoft\Windows NT\CurrentVersion',
REG_SZ,
'ProductId',
SerialKey,
sPchar,
True );
For reading Product Name
ReadKeyFromRegister(TypWin32Or64,
HKEY_LOCAL_MACHINE,
'SOFTWARE\Microsoft\Windows NT\CurrentVersion',
REG_SZ,
'ProductName',
UsedWindowsName,
sPchar ,
True );
Procedure for look miltiline key as i.e Version Bios - also 64 envir.
procedure TForm1.ReadREG_MULTI_SZ(const CurrentKey: HKey; const
Subkey, ValueName: string;
Strings: TStrings; var ValueStrRead: String);
var
valueType: DWORD;
valueLen: DWORD;
p, buffer: PChar;
key: HKEY;
begin
Strings.Clear;
if RegOpenKeyEx(CurrentKey,
PChar(Subkey),
0, KEY_READ, key) = ERROR_SUCCESS then
begin
SetLastError(RegQueryValueEx(key,
PChar(ValueName),
nil,
@valueType,
nil,
@valueLen));
if GetLastError = ERROR_SUCCESS then
if valueType = REG_MULTI_SZ then
begin
GetMem(buffer, valueLen);
try
RegQueryValueEx(key,
PChar(ValueName),
nil,
nil,
PBYTE(buffer),
@valueLen);
p := buffer;
while p^ <> #0 do
begin
Strings.Add(p);
Inc(p, lstrlen(p) + 1)
end
finally
ValueStrRead:=Strings.Text;
ValueStrRead:=StringReplace(ValueStrRead,#$D#$A, ' ' , [rfReplaceAll, rfIgnoreCase]);
FreeMem(buffer);
end
end
else
raise ERegistryException.Create('Stringlist expected')
else
raise ERegistryException.Create('Cannot Read MULTI_SZ Value/');
end;
end;