如何将字符串数组从C#传递到Delphi dll
本文关键字:Delphi dll 字符串 数组 | 更新日期: 2023-09-27 17:58:22
我需要将字符串的开放数组从C#传递到Delphi dll。在delphi中设置数组的长度,并将其返回到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部分:
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;
但它不起作用。怎么了?
我已经更改了Delphi代码,现在它可以工作了。感谢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;
是否有可能将开放数组传递给Delphi