Dave Lane
Dave Lane

Reputation: 41

In Delphi automation server, how to return a string array to client (string[])

I am developing an automation server in Delphi and a couple of properties need to return arrays of strings. The specification asks for (with examples in 3 languages):

C# 
string[] Names { get; }

Visual Basic 
ReadOnly Property Names As String()
    Get

Visual C++ 
property array<String^>^ Names {
    array<String^>^ get ();
}

I have tried many way to do this an am stuck including SafeArrays and Variants as the RIDL type. My latest attempt (so I have at least one example) is:

function TFW.Get_Names: OleVariant; safecall;

var
  I : integer;
  NumFilters:integer;
  Filters:FieldsType;
  V:OleVariant;

begin
  NumFilters:=SplitFields(Filters,FilterNames,',','"');

  V := VarArrayCreate([1,NumFilters], VT_BSTR);

  for I := 1 to NumFilters do
    V[I]:=Filters[I];

  Get_Names:=V;  
end;

The client application in this case complains with the error:

"Unable to cast object of type 'System.String[*]' to type 'System.String[]'."

Thanks in advance!

Upvotes: 1

Views: 497

Answers (1)

Dave Lane
Dave Lane

Reputation: 41

The solution is to create the variant array zero-based, instead of 1...X):

V := VarArrayCreate([0,NumFilters-1], VT_BSTR);

  for I := 0 to NumFilters-1 do
    V[I]:=Filters[I+1];

Case closed!

Upvotes: 2

Related Questions