简体   繁体   English

从C#到Delphi(托管到非托管)的结构体数组(包含结构体数组)

[英]Marshalling array of struct (that contains an array of struct) from C# to Delphi (managed to unmanaged)

The C# struct: C#结构:

public struct Table
{
    [MarshalAs(UnmanagedType.LPArray)]
    public Parameters[] parameters;
    public string Name;
    etc...
}

public struct Parameters
{
    public string parameterName;
    public string parametervalue;
}

I don't know the length of the 'Parameters[] parameters' array at design time. 我在设计时不知道'Parameters [] parameters'数组的长度。

So I could have: 所以我可以有:

Table[] tables = new Table[RandomNumber];

for (int i = 0; i < RandomNumber; i++)
{
    for (int j=0; j<AnotherRandomNumber; j++)
    {
        tables[i].parameters[j] = new Parameters() { parameterName = "", parametervalue = "" };
    }
}

Now I want to pass the tables array back to unmanaged code (Delphi) 现在我想将表数组传递回非托管代码(Delphi)

The interface looks like this: 该界面如下所示:

int GetTables([Out, MarshalAs(UnmanagedType.LPArray)] out Table[] results);

And the method in C#: 以及C#中的方法:

public int GetTables(out ReportTable[] results)

But the Delphi code gives an error: 但是Delphi代码给出了一个错误:

Cannot marshal field ... arrays fileds must be paired with ByValArray or SafeArray. 无法封送字段...数组文件必须与ByValArray或SafeArray配对。

I've tried the various UnmanagedType.xxx combinations but get errors no matter what. 我已经尝试过各种UnmanagedType.xxx组合,但是无论如何都会出现错误。

Any ideas? 有任何想法吗?

Thanks W 谢谢W

I've managed to 'fix' my silly issues! 我设法“解决”了我的愚蠢问题! I replaced this: 我替换了这个:

public struct Table
{
    [MarshalAs(UnmanagedType.LPArray)]
    public Parameters[] parameters;
    public string Name;
    etc...
}

with: 与:

[MarshalAs(UnmanagedType.ByValArray)]

I was under the impression before that I had to pass the size of the stcurt array but that is not the case. 在给我印象之前,我不得不传递stcurt数组的大小,但事实并非如此。

Thanks W 谢谢W

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

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