1

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!

1
  • I found the problem - the type cast did not like the array not being zero-based, changing that and the problem is fixed. Commented Nov 19, 2017 at 21:40

1 Answer 1

2

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!

Sign up to request clarification or add additional context in comments.

1 Comment

You could also simply use a WideString array which Delphi would convert automatically to a zero-based variant array of BSTR.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.