简体   繁体   English

如何将字符串数组从C#传递到Delphi dll?

[英]How can I pass string array from C# to Delphi dll?

I need to pass open array of strings from C# to Delphi dll. 我需要将开放的字符串数组从C#传递到Delphi dll。 Sets the length of array in delphi and return it back to C#. 在delphi中设置数组的长度,并将其返回给C#。 Are they any ways? 有什么办法吗?

For the beginning I'm trying this code in C#: 首先,我在C#中尝试以下代码:

public struct TStrSample
{
    [MarshalAs(UnmanagedType.BStr)]
    public string Name;
}

[DllImport(@"EPConvs.dll", CallingConvention = CallingConvention.StdCall)]
public static extern void PopulateStrArray([In, Out] [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)] TStrSample[] arr,
                                           ref int len);


[Microsoft.SqlServer.Server.SqlProcedure]
public static void hcTable(SqlBytes blob)
{
    TStrSample[] arr = new TStrSample[10];
    int len = arr.Length;
    for (int i = 0; i < len; i++)
    {
        arr[i].Name = i.ToString();
    }
    PopulateStrArray(arr, ref len);
}

Delphi part: 德尔福部分:

type
  TStrSample = record
    Name: WideString;
  end;
  PStrSample = ^TStrSample;

procedure PopulateStrArray(arr: PStrSample; var len: Integer); stdcall;
var
  i: Integer;
  returnArray: array of TStrSample;
begin
  returnArray := @arr;
  for i := 0 to Len-1 do
    returnArray[i].Name := 'YES = ' + IntToStr(i);
end;

But it does not work. 但这行不通。 What is wrong? 怎么了?

I've changed Delphi code and now it's working. 我已经更改了Delphi代码,现在可以使用了。 Thanks to David Heffernan . 感谢David Heffernan

type
  TStrSample = record
    Name: WideString;
  end;
  PStrSample = ^TStrSample;

procedure PopulateStrArray(arr: PStrSample; var len: Integer); stdcall;
var
  i: Integer;
begin
  for i := 0 to len - 1 do
  begin
    arr^.Name := 'YES = ' + IntToStr(i);
    Inc(arr);
  end;
end;

Is there some possibility to pass an open array to Delphi? 是否有可能将开放数组传递给Delphi?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM