I'm trying to write a DLL in Delphi to allow my C# app to access an Advantage database (using VS2013 and not been able to access the data directly).
My issue is after I make the call, the array in C# is full of null values.
The Delphi DLL code:
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;
This appears to be working. I've used ShowMessage to show what the information going in looks like and what it looks like after.
The C# Code:
[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);
}
This code executes, no errors of any kind, but when I iterate through projectItems each one returns
Id = 0
Description = null