title: Reading Byte Array From Delphi Pointer In C#
body: <p>I have asked question before.
  <a href="https://stackoverflow.com/questions/30041480/call-delphi-function-from-c-sharp">Call Delphi Function From C#</a></p>
  
  <p>I have added two methods like this.</p>
  
  <p><strong>C#</strong> </p>
  
  <pre><code>public interface IStringFunctions
  {
    [MethodImplAttribute(MethodImplOptions.PreserveSig)]
    void SetValueAsByteArray(IntPtr DataPointer, int DataLength);
  
    [MethodImplAttribute(MethodImplOptions.PreserveSig)]
    IntPtr GetValueAsByteArray(out int DataLength);
  }
  if (instance != null)
  {
    // Sending Pointer of Byte Array  To Delphi Function. 
    byte[] inputBytes = new byte[3];
    inputBytes[0] = 65;
    inputBytes[1] = 66;
    inputBytes[2] = 67;
  
    IntPtr unmanagedPointer = Marshal.AllocHGlobal(inputBytes.Length);
    Marshal.Copy(inputBytes, 0, unmanagedPointer, inputBytes.Length);
    instance.SetValueAsByteArray(unmanagedPointer, inputBytes.Length);
  
  
    // Getting Byte Array from Pointer 
    int dataLength = 0;
    IntPtr outPtr = instance.GetValueAsByteArray(out dataLength);
    byte[] outBytes = new byte[dataLength];
    Marshal.Copy(outPtr, outBytes, 0, dataLength);
    string resultStr = System.Text.Encoding.UTF8.GetString(outBytes);             
  }
  </code></pre>
  
  <p><strong>Delphi DLL</strong></p>
  
  <pre><code> TStringFunctions = class(TInterfacedObject, IStringFunctions)
    private 
      FValueAsByteArray: TByteArray;
    public
      procedure SetValueAsByteArray(DataPointer:Pointer;DataLength:Integer); stdcall;
      function GetValueAsByteArray(out DataLength:Integer): Pointer; stdcall;
    end;
  
  procedure TStringFunctions.SetValueAsByteArray(DataPointer:Pointer;DataLength:Integer); stdcall; export;
  var
    Source: Pointer;
    SourceSize: Integer;
    Destination: TByteArray;
  begin
    Source := DataPointer;
    SourceSize := DataLength;
    SetLength(Destination, SourceSize);
    Move(Source^, Destination[0], SourceSize);
  
    FValueAsByteArray := Destination;
  
    ShowMessage(TEncoding.UTF8.GetString(TBytes(Destination)));
    ShowMessage(IntToStr(Length(Destination)));
    ShowMessage('DataLength:'+IntToStr(DataLength));
  end;
  
  function TStringFunctions.GetValueAsByteArray(out DataLength:Integer): Pointer; stdcall; export;
  begin
    DataLength := Length(FValueAsByteArray);
  
    ShowMessage(TEncoding.UTF8.GetString(TBytes(FValueAsByteArray)));
    ShowMessage(IntToStr(Length(FValueAsByteArray)));
  
    Result := Addr(FValueAsByteArray);
  end;
  </code></pre>
  
  <p><strong>SetValueAsByteArray worked.</strong></p>
  
  <p>But GetValueAsByteArray method incorrect pointer and bytes.</p>
  
  <p>How to read correct pointer of <strong>FValueAsByteArray</strong> and bytes[] in c# ?</p>
  
  <p>What's my fault ?</p>