Returning Safearray of BSTR from Interface method call

I'm having problems returning a safearray of BSTR to a VB
user.  I can actually get the array through with the proper
dimensions, it's just that the single string element in the array is
always an empty string.

I'm developing an ActiveForm based control.
In my control, I'm implementing an interface that
I imported through the IDE.

The interface looks like this:

IContextReceiver = interface(IUnknown)
    function Get_ContextClasses(out contextList: PSafeArray): HResult;
    function SetContext(const contextClass: WideString; const
contextSubject: WideString): HResult; stdcall;

Here's the implementation:

function TEntryControl.Get_ContextClasses(out contextList: PSafeArray):
  V: Variant;
  sList: TStringList;
  VarBounds: array[0..0] of TVarArrayBound;
  Index: array[0..0] of Integer;
  elItem: TBStr;
  strPtr: POleStr;

    VarBounds[0].LowBound := 0;
    VarBounds[0].ElementCount := 1;

    Index[0] := 0;
    strPtr := 'Foo';

    elItem := SysAllocString(strPtr);

    contextList := SafeArrayCreate(VT_BSTR, 1, VarBounds);

    if SafeArrayPutElement(contextList, Index, elItem) <> 0
Then         OutputDebugString('SafeArrayPut failed');

    Result := S_OK;

I have been through many different iterations for this implementation
all with the same results.

I have tried using VarArrayCreate, SafeArrayCreateVector ... etc.

In VB, the array is actually returned with the correct dimensions, so
my feeling is that the array is created OK.
Perhaps there's a problem with my string, since VB reports the
single element as an empty string ("") in the VB de{*word*81}.

Any ideas.

Sent via
Share what you know. Learn what you don't.