Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在C语言中从Delphi指针读取字节数组#_C#_Delphi_Pinvoke_Marshalling_Intptr - Fatal编程技术网

C# 在C语言中从Delphi指针读取字节数组#

C# 在C语言中从Delphi指针读取字节数组#,c#,delphi,pinvoke,marshalling,intptr,C#,Delphi,Pinvoke,Marshalling,Intptr,我以前问过这个问题。 我添加了两种类似的方法 C# public interface IStringFunctions { [MethodImplAttribute(MethodImplOptions.PreserveSig)] void SetValueAsByteArray(IntPtr DataPointer, int DataLength); [MethodImplAttribute(MethodImplOptions.PreserveSig)] IntPtr Get

我以前问过这个问题。

我添加了两种类似的方法

C#

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);             
}
Delphi DLL

 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;
SetValueAsByteArray工作。

但是GetValueAsByteArray方法的指针和字节不正确

如何在c#中正确读取FValueAsByteArray和bytes[]的指针

什么是我的错

Result := Addr(FValueAsByteArray);
这是指向数组的指针的地址。您要返回数组的地址:

Result := Pointer(FValueAsByteArray);
如果我是你,我会避免分配非托管内存,让封送器封送字节数组

我已经告诉过你,
导出
是徒劳的。重复我的话让我很沮丧

这是指向数组的指针的地址。您要返回数组的地址:

Result := Pointer(FValueAsByteArray);
如果我是你,我会避免分配非托管内存,让封送器封送字节数组

我已经告诉过你,
导出
是徒劳的。重复我的话让我很沮丧

这是指向数组的指针的地址。您要返回数组的地址:

Result := Pointer(FValueAsByteArray);
如果我是你,我会避免分配非托管内存,让封送器封送字节数组

我已经告诉过你,
导出
是徒劳的。重复我的话让我很沮丧

这是指向数组的指针的地址。您要返回数组的地址:

Result := Pointer(FValueAsByteArray);
如果我是你,我会避免分配非托管内存,让封送器封送字节数组

我已经告诉过你,
导出
是徒劳的。重复我的话让我很沮丧