Access _CopyArray Procedure

Is there a way to access (and call) procedures, such as _CopyArray, that are defined in the interface on the device system?

NB: I am trying to create a routine that makes a deep clone of any dynamic array and does not use Delphi 2010 (using Delphi 2007).

The reason I am trying to solve this problem without using Copy is because I only have a pointer where the dynamic array (pointer) is located, plus a reference of type info. I cannot call the Copy function because it implicitly needs to fill in the info type.

SOLUTION: You need to reference it by replacing _ with @ and viewing it with the system.

procedure CopyArray( dest, source, typeInfo: Pointer; cnt: Integer );
asm
    PUSH    dword ptr [EBP+8]
    CALL    system.@CopyArray
end;

type
    PObject = ^TObject;

function TMessageRTTI.CloneDynArray( Source: Pointer; T: TTypeRecord ): Pointer;
var
    TypeInfo: TTypeRecord;
    L:        Integer;
    PObj:     PObject;
    PArr:     PPointer;
begin
    Assert( T.TypeKind = tkDynArray );

    // set size of array
    Result := nil;
    L := Length( TIntegerDynArray( Source ) );
    if L = 0 then Exit;
    DynArraySetLength( Result, T.TypeInfo, 1, @L );

    if Assigned( T.TypeData^.elType ) then TypeInfo := ByTypeInfo( T.TypeData^.elType^ ) else TypeInfo := nil;
    if Assigned( TypeInfo ) then begin
        case TypeInfo.TypeKind of
            tkClass: begin
                PObj := Result;
                while L > 0 do begin
                    PObj^ := CloneObject( PObject( Source )^ );
                    Inc( PObject( Source ) );
                    Inc( PObj );
                    Dec( L );
                end;
            end;
            tkDynArray: begin
                PArr := Result;
                while L > 0 do begin
                    PArr^ := CloneDynArray( PPointer( Source )^, TypeInfo );
                    Inc( PPointer( Source ) );
                    Inc( PArr );
                    Dec( L );
                end;
            end;
            else CopyArray( Result, Source, TypeInfo.TypeInfo, L );
        end;
    end else begin
        // We can simply clone the data
        Move( Source^, Result^, L * T.ElementSize );
    end;
end;
+3
source share
2

, "" , _CopyArray, is _IsClass Copy ..

, , Delphi . Makefile RTL System.pas SysInit.pas, , _ @. _CopyArray, , @CopyArray. , BASM (), .

+4

Andreas Rejbrand - _CopyArray , . , _CopyArray ( debug.dcu ):

procedure TForm1.Button4Click(Sender: TObject);
type
  TArr2D = array of TBytes;

var
  A, B: TArr2D;

begin
  A:= TArr2D.Create(TBytes.Create(1, 2, 3), TBytes.Create(4, 5));
  B:= Copy(A);
  Button4.Caption:= IntToStr(B[1, 1]);
end;
+3

Source: https://habr.com/ru/post/1743383/


All Articles