Solved

Migrating to XE2

Posted on 2013-10-23
5
471 Views
Last Modified: 2013-10-28
Hi,


Migrationg to Xe2 from 2007 I am getting following error
Error=
      ERegistryException with message 'Invalid data type for 'SystemBIOSVersion'
Code Line=
      sl := ReadMultirowKey(RegistryV,'SystemBIOSVersion');

Regards
Allan


unit RegistryBIOS ;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs,Registry, StdCtrls;

  function GetRegInfoWinNT(JobType:String):String ;
  function ReadMultirowKey(reg: TRegistry; Key: string): TStrings;

implementation

function GetRegInfoWinNT(JobType:String):String ;
var   // Usage  GetRegInfoWinNT('BIOSDate') or GetRegInfoWinNT('BIOSVersion')
  Registryv       : TRegistry;
  RegPath         : string;
  sl              : TStrings;
begin
  RegPath := '\HARDWARE\DESCRIPTION\System';
  registryv:=TRegistry.Create(KEY_QUERY_VALUE) ; // To overcome windows access rights.
  registryv.rootkey:=HKEY_LOCAL_MACHINE;
  sl := nil;
  try
    registryv.OpenKeyReadOnly(RegPath);
    if JobType='BIOSDate' then
      result:=RegistryV.ReadString('SystemBIOSDate')
    else if JobType='BIOSVersion' then
    begin
      sl := ReadMultirowKey(RegistryV,'SystemBIOSVersion');
      result:=sl.Text ;
    end;
  except
  end;
  Registryv.Free;
  if Assigned(sl) then sl.Free;
end;

function ReadMultirowKey(reg: TRegistry; Key: string): TStrings;
const 
  bufsize = 100;
var
  i: integer;
  s1: string;
  sl: TStringList;
  bin: array[1..bufsize] of char;
begin
  try
    result := nil;
    sl := nil;
    sl := TStringList.Create;
    if not Assigned(reg) then
      raise Exception.Create('TRegistry object not assigned.');
    FillChar(bin,bufsize,#0);
    reg.ReadBinaryData(Key,bin,bufsize);
    i := 1;
    s1 := '';
    while i < bufsize do
    begin
      if ord(bin[i]) >= 32 then
        s1 := s1 + bin[i]
      else
      begin
        if Length(s1) > 0 then
        begin
          sl.Add(s1);
          s1 := '';
        end;
      end;
      inc(i);
    end;
    result := sl;
  except
    sl.Free;
    raise;
  end;
end;


end.

Open in new window

0
Comment
Question by:Allan_Fernandes
  • 2
  • 2
5 Comments
 
LVL 25

Expert Comment

by:Sinisa Vuk
Comment Utility
Check if key values exists before do Read and check for data type too (if is compatible with ReadBinaryData)...
...
if reg.ValueExists(Key) then
    if reg.GetDataType(Key) = rdBinary then

Open in new window


... maybe value is of another type ....
0
 
LVL 36

Expert Comment

by:Geert Gruwez
Comment Utility
i'm guessing you get lots of warning about variable might not have been initialised ?

using the line
  var := nil
only takes away the warning, it doesn't actually solve the fault

your code reworked , with the fault removed
i had to change the 2nd function to a procedure

unit RegistryBIOS ;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs,Registry, StdCtrls;

  function GetRegInfoWinNT(JobType:String):String ;
  procedure ReadMultirowKey(reg: TRegistry; Key: string; Data: TStrings);

implementation

function GetRegInfoWinNT(JobType:String):String ;
var   // Usage  GetRegInfoWinNT('BIOSDate') or GetRegInfoWinNT('BIOSVersion')
  Registryv       : TRegistry;
  RegPath         : string;
  sl              : TStrings;
begin
  RegPath := '\HARDWARE\DESCRIPTION\System';
  registryv:=TRegistry.Create(KEY_QUERY_VALUE) ; // To overcome windows access rights.
  try
    registryv.rootkey:=HKEY_LOCAL_MACHINE;
    registryv.OpenKeyReadOnly(RegPath);
    if JobType='BIOSDate' then
      result:=RegistryV.ReadString('SystemBIOSDate')
    else if JobType='BIOSVersion' then
    begin
      sl := TStringList.Create;
      try
        ReadMultirowKey(RegistryV,'SystemBIOSVersion', sl);
        result:=sl.Text ;
      finally
        sl.Free;
      end;
    end;
  finally
    Registryv.Free;
  end;
end;

procedure ReadMultirowKey(reg: TRegistry; Key: string; Data: TStrings);
const 
  bufsize = 100;
var
  i: integer;
  s1: string;
  bin: array[1..bufsize] of char;
begin
  if not Assigned(reg) then
    raise Exception.Create('TRegistry object not assigned.');
  if not Assigned(data) then 
    raise Exception.Create('Data variable not assigned');
  FillChar(bin,bufsize,#0);
  reg.ReadBinaryData(Key,bin,bufsize);
  i := 1;
  s1 := '';
  while i < bufsize do
  begin
    if ord(bin[i]) >= 32 then
      s1 := s1 + bin[i]
    else
    begin
      if Length(s1) > 0 then
      begin
        data.Add(s1);
        s1 := '';
      end;
    end;
    inc(i);
  end;
end;

end.

Open in new window

0
 

Author Comment

by:Allan_Fernandes
Comment Utility
I appreciate the effort you are taking, but my problem was not resolved.
Later I found the solution at
http://www.swissdelphicenter.ch/torry/showcode.php?id=1431
0
 

Author Comment

by:Allan_Fernandes
Comment Utility
I've requested that this question be closed as follows:

Accepted answer: 0 points for Allan_Fernandes's comment #a39599590

for the following reason:

-----
0
 
LVL 36

Accepted Solution

by:
Geert Gruwez earned 500 total points
Comment Utility
ow,  that problem :)

i thought i'd resolve the code faults first, wait on your feedback and then proceed
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now