Search code examples
delphicasting64-bitrecordtobject

Invalid typecast: convert record to tobject on 64-bit platform


it works on 32-bit platform.but not 64-bit here is the exzample

  TVerbInfo = packed record
    Verb: Smallint;
    Flags: Word;
  end;

var
  VerbInfo: TVerbInfo;
  strList : TStringList;
  verb : Smallint;
  flags : Word;
begin
  strList := TStringList.create();
  .....
  verbInfo.verb := verb;
  verbInfo.flags := flags;
  strList.addObject('verb1',TObject(VerbInfo));  //invalid typecast happened here
end;

can anyone help me? thank you very much


Solution

  • You can try something like this:

    function MakeVerbInfoObject(const AVerbInfo: TVerbInfo): TObject;
    begin
      Result := nil;
      Move(AVerbInfo, Result, SizeOf(AVerbInfo));
    end;
    
    strList.addObject('verb1', MakeVerbInfoObject(VerbInfo));
    

    To retrieve the value you can use a corresponding function like

    function GetVerbInfoFromObject(AObject: TObject): TVerbInfo;
    begin
      Move(AObject, Result, SizeOf(Result));
    end;
    
    VerbInfo := GetVerbInfoFromObject(strList.Objects[idx]);