C#指向struct array

时间:2018-04-22 18:20:00

标签: c# pascal

您好我想创建一个指向struct数组的指针 我有这样的2个结构

public struct TRecord
{
    string Key;
    string Value;
}

public struct TDB
{
    public int Count;
    TRecord[] Records;
}

但是因为我正在为Delphi应用程序编写一个插件,它传递DBPointer指针(DBPointer有一个数据数组)我需要将DBPointer指向TDB,这样我就可以从中获取数据了。尝试这样做时

public unsafe TDB* DBPointer; //DBPointer points to TDB struct which has struct array

我得到了

Error   CS0208  Cannot take the address of, get the size of, or declare a pointer to a managed type

当我读到时,我无法声明指向字符串和结构的指针 所以我现在不知道该怎么做,因为如果用pascal写这个就可以了。

type TRecord = record
    Key: shortstring;
    Value:  shortstring;
end;

type TPRecord = record
    Key: PChar;
    Value:  PChar;
end;

type TDB = record
    Count: integer;
    Records: array [0..255] of TRecord;
end;
PTDB = ^TDB;

function ReadValue(Key: PChar; DBPointer: PTDB): PChar; stdcall;
var a,i: integer;
    SKey: shortstring;
begin
Result:='';
SKey:=shortstring(Key);
a:=DBPointer^.Count;
if (a>length(DBPointer^.Records)) then a:=length(DBPointer^.Records);    //ochrana
for i:=1 to a do
begin
if (DBPointer^.Records[i-1].Key=SKey) then
    begin Result:=StrPCopy(Param1,DBPointer^.Records[i-1].Value); exit; end;
    end;
end;

感谢Anwsering和Best Regards

0 个答案:

没有答案