Link to home
Start Free TrialLog in
Avatar of tayto
tayto

asked on

Read Binary Data WITHOUT TRegistry

Does anyone have code that can read binary data held in registry?

Avatar of robert_marquardt
robert_marquardt

Get The Jedi code Library from http://sourceforge.net/projects/jcl
It contains RegReadBinary which serves your needs.

Here is an excerpt (which will not compile):

function RegReadBinary(const RootKey: HKEY; const Key, Name: string; var Value; const ValueSize: Cardinal): Cardinal;
var
  RegKey: HKEY;
  Size: DWORD;
  RegKind: DWORD;
  Ret: Longint;
begin
  Result := 0;
  if RegOpenKeyEx(RootKey, RelativeKey(Key), 0, KEY_READ, RegKey) = ERROR_SUCCESS then
  begin
    RegKind := 0;
    Size := 0;
    Ret := RegQueryValueEx(RegKey, PChar(Name), nil, @RegKind, nil, @Size);
    if Ret = ERROR_SUCCESS then
      if RegKind in cRegBinKinds then
      begin
        if Size > ValueSize then
          Size := ValueSize;
        RegQueryValueEx(RegKey, PChar(Name), nil, @RegKind, @Value, @Size);
        Result := Size;
      end;
    RegCloseKey(RegKey);
    if not (RegKind in cRegBinKinds) then
      ValueError(Key, Name);
  end
  else
    ReadError(Key);
end;
simple example:
// place TLabel component Named Label1 on your form
// you should change registry key name to apropriate (key in code is for testing purposes only)

procedure TForm1.FormCreate(Sender: TObject);
var
  hk: hkey;
  buf: pointer;
  pc: PChar;
  i:integer;
  dwType, dwSize: DWORD;
begin
  Label1.Caption := '';
  // try to open key
  if RegOpenKeyEx(HKEY_LOCAL_MACHINE, 'SOFTWARE\Microsoft\Cryptography\AutoEnrollment\AEDirectoryCache\c1430299-b699-4e17-80dd-a6c7f27551dd',
    0, KEY_READ, hk) = ERROR_SUCCESS then begin
  try
    dwType := 0; dwSize := 0;
    // first get data size and data type
    if (RegQueryValueEx(hk, 'AEMaxUSN', nil, @dwType, nil, @dwSize) = ERROR_SUCCESS)
      and (dwType = REG_BINARY) then
    begin
      GetMem(buf, dwSize);
      try
        // get actual data and display it as hex on label
        if RegQueryValueEx(hk, 'AEMaxUSN', nil, @dwType, buf, @dwSize) = ERROR_SUCCESS then
        begin
          pc := buf;
          for i := 1 to dwSize do begin
            Label1.Caption := Label1.Caption + IntToHex(integer(pc^), 2);
            inc(pc);
          end;
        end;
      finally
        freeMem(buf);
      end;
    end;
  finally
    RegCloseKey(hk);
  end;
  end;
end;

wbr, mo.
ASKER CERTIFIED SOLUTION
Avatar of geobul
geobul

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi :-)   (is see you have been asking some Q's about the registry)

 I wonder exactly what sort of data you are placing inside the registry?
Myself I never ever use the registry...instead I store the data inside the
exe itself, I like that better than registry or ini files. You can update the data
at runtime or at program shutdown or startup...depending on what sort of
data it is. It can be stored near the dos stub and read back from ram if your data
is not over a few hundred bytes...this makes it a wee bit faster...(and if you use
UPX to compress your exe then you can store even more data)

 If you would like some demo code showing how to store data in the exe instead
of the registry let me know and I will send some :-)