我试图在Delphi中编写一个DLL,以允许我的C#应用程序访问Advantage数据库(使用VS2013,但无法直接访问数据)。
我的问题是打电话后,C#中的数组充满了空值。
Delphi DLL代码:
TItem = record
Id : Int32;
Description : PWideChar;
end;
function GetNumElements(const ATableName: PWideChar): Integer; stdcall;
var recordCount : Integer;
begin
... // code to get the number of records from ATableName
Result := recordCount;
end;
procedure GetTableData(const ATableName: PWideChar; const AIdField: PWideChar;
const ADataField: PWideChar; result: array of TItem); stdcall;
begin
... // ATableName, AIdField, and ADataField are used to query the specific table, then I loop through the records and add each one to result array
index := -1;
while not Query.Eof do begin
Inc(index);
result[index].Id := Query.FieldByName(AIdField).AsInteger;
result[index].Description := PWideChar(Query.FieldByName(ADataField).AsString);
Query.Next;
end;
... // cleanup stuff (freeing created objects, etc)
end;
这似乎正在工作。我使用ShowMessage来显示输入的信息以及之后的信息。
C#代码:
[StructLayoutAttribute(LayoutKind.Explicit)] // also tried LayoutKind.Sequential without FieldOffset
public struct TItem
{
[FieldOffset(0)]
public Int32 Id;
[MarshalAs(UnmanagedType.LPWStr),FieldOffset(sizeof(Int32))]
public string Description;
}
public static extern void GetTableData(
[MarshalAs(UnmanagedType.LPWStr)] string tableName,
[MarshalAs(UnmanagedType.LPWStr)] string idField,
[MarshalAs(UnmanagedType.LPWStr)] string dataField,
[MarshalAs(UnmanagedType.LPArray)] TItem[] items, int high);
public void GetListItems()
{
int numProjects = GetNumElements("Project");
TItems[] projectItems = new TItem[numProjects];
GetTableData("Project", "ProjectId", "ProjectName", projectItems, numProjects);
}
这段代码执行,没有任何类型的错误,但是当我遍历projectItems时,每个返回
Id = 0
Description = null
我可以看到很多问题。首先,我将这样声明结构:
[StructLayoutAttribute(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
public struct TItem
{
public Int32 Id;
[MarshalAs(UnmanagedType.BStr)]
public string Description;
}
您将需要使用,UnmanagedType.BStr
以便可以在非托管端分配字符串,并在托管端释放该字符串。另一种方法是将其编组为,LPWStr
但随后您将不得不CoTaskMemAlloc
在非托管方面进行分配。
Delphi记录变为:
type
TItem = record
Id : Int32;
Description : WideString;
end;
通过查看以下行,您可以清楚地看到代码是错误的:
result[index].Description := PWideChar(Query.FieldByName(ADataField).AsString);
在这里,您result[index].Description
指向函数返回时将被释放的内存。
尝试使用Delphi开放式阵列充其量是有风险的。我不会那样做。如果您坚持这样做,则至少应注意传递的值high
,而不要写在数组末尾。而且,您应该传递正确的高价。那是projectItems.Length-1
。
现在,您正在为数组使用按值传递,因此您在Delphi代码中编写的任何内容都无法找到返回C#代码的方式。更重要的是,C#代码[In]
默认情况下具有封送处理功能,因此即使您切换为通过var,封送处理程序也不会将项目封送回projectItems
托管方。
就个人而言,我将停止使用开放数组,并且要明确:
function GetTableData(
ATableName: PWideChar;
AIdField: PWideChar;
ADataField: PWideChar;
Items: PItem;
ItemsLen: Integer
): Integer; stdcall;
这里Items
指向数组中的第一项,并ItemsLen
给出所提供数组的长度。函数返回值应为复制到数组的项目数。
要实现此目的,请使用指针算法或($POINTERMATH ON}
。我更喜欢后一种选择。我认为我不需要证明这一点。
在C#端,您有:
[DllImport(dllname, CharSet=CharSet.Unicode)]
public static extern int GetTableData(
string tableName,
string idField,
string dataField,
[In,Out] TItem[] items,
int itemsLen
);
这样称呼它:
int len = GetTableData("Project", "ProjectId", "ProjectName", projectItems,
projectItems.Length);
// here you can check that the expected number of items were copied
综上所述,我确实怀疑编组器是否会编组一系列不可引用的类型。我有一种感觉。在这种情况下,您的主要选择是:
IntPtr
记录中。用分配CoTaskMemAlloc
。用破坏被管理端Marshal.FreeCoTaskMem
。我个人会选择后一种方法。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句