Search code examples
pngdelphi-7rtti

How know if TPNGObject have a valid header?


I've the following class:

    TPNGButton = class(TNeoGraphicControl)
    private
        FImageDown: TPNGObject;
        fImageNormal: TPNGObject;
        fImageOver: TPNGObject;
    ...

    public
    ...
        constructor Create(AOwner: TComponent); override;
        destructor Destroy; override;
    published
        property ImageNormal: TPNGObject read fImageNormal write SetImageNormal;
        property ImageDown: TPNGObject read FImageDown write SetImageDown;
        property ImageOver: TPNGObject read FImageOver write SetImageOver;
    ...
end;

I'm using a function below to copy properties of the objFrom to objTo as the parameter.

procedure CopyObject(ObjFrom, ObjTo: TObject);
var
  PropInfos: PPropList;
  PropInfo: PPropInfo;
  Count, Loop: Integer;
  OrdVal: Longint;
  StrVal: String;
  FloatVal: Extended;
  MethodVal: TMethod;
begin
  { Iterate thru all published fields and properties of source }
  { copying them to target }

  { Find out how many properties we'll be considering }
  Count := GetPropList(ObjFrom.ClassInfo, tkAny, nil);
  { Allocate memory to hold their RTTI data }
  GetMem(PropInfos, Count * SizeOf(PPropInfo));
  try
    { Get hold of the property list in our new buffer }
    GetPropList(ObjFrom.ClassInfo, tkAny, PropInfos);
    { Loop through all the selected properties }
    for Loop := 0 to Count - 1 do
    begin
      PropInfo := GetPropInfo(ObjTo.ClassType, PropInfos^[Loop]^.Name);
      { Check the general type of the property }
      { and read/write it in an appropriate way }
      case PropInfos^[Loop]^.PropType^.Kind of
        tkInteger, tkChar, tkEnumeration,
        tkSet, tkClass{$ifdef Win32}, tkWChar{$endif}:
        begin
          OrdVal := GetOrdProp(ObjFrom, PropInfos^[Loop]);
          if Assigned(PropInfo) and (Assigned(PropInfo^.SetProc)) then
            SetOrdProp(ObjTo, PropInfo, OrdVal); //here happens the bug...
        end;
        tkFloat:
        begin
          FloatVal := GetFloatProp(ObjFrom, PropInfos^[Loop]);
          if Assigned(PropInfo) and (Assigned(PropInfo^.SetProc)) then
            SetFloatProp(ObjTo, PropInfo, FloatVal);
        end;
        {$ifndef DelphiLessThan3}
        tkWString,
        {$endif}
        {$ifdef Win32}
        tkLString,
        {$endif}
        tkString:
        begin
          { Avoid copying 'Name' - components must have unique names }
          if UpperCase(PropInfos^[Loop]^.Name) = 'NAME' then
            Continue;
          StrVal := GetStrProp(ObjFrom, PropInfos^[Loop]);
          if Assigned(PropInfo) and (Assigned(PropInfo^.SetProc)) then
            SetStrProp(ObjTo, PropInfo, StrVal);
        end;
        tkMethod:
        begin
          MethodVal := GetMethodProp(ObjFrom, PropInfos^[Loop]);
          if Assigned(PropInfo) and (Assigned(PropInfo^.SetProc)) then
            SetMethodProp(ObjTo, PropInfo, MethodVal);
        end
      end
    end
  finally
    FreeMem(PropInfos, Count * SizeOf(PPropInfo));
  end;
end;

But, When the PNGObject property is passed in the SetOrdProp, the Delphi return the following exception: Exception

My question is: How can i know if the PNGObject have a valid header before SetOrdProp? Or to another way to solve this problem...

Others comments

  • Using the Assign method as the following code and commenting the CopyObject function:

      TControl(objCtrlZ.Referencia).Assign(Component);
      // "Component" is objFrom and objCtrlZ.Referencia is objTo
      //  CopyObject(Component, objCtrlZ.Referencia);
    

The Delphi catch the following exception:

Exception 2


Solution

  • I solved my problem.

    To verify if the TPNGObject have a valid header i used this code:

    objTemp := GetObjectProp(ObjFrom,PropInfos^[Loop]);
    if ((TPNGObject(objTemp).Chunks.Count <> 0) and (TPNGObject(objTemp).Chunks.Item[0] is TChunkIHDR)) then begin ... end;
    

    The first line i get the property TPNGObject and as always the object is assigned the objTemp can't get the AV.

    To validate the Header i verify in Chunks if count is different of zero and if Item[0] is TChunkIHDR to know if is a valid header or not.

    Thank you!