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)
    ['{A6B19902-04B2-11D3-BABB-00105AD12225}']
    function Get_ContextClasses(out contextList: PSafeArray): HResult;
stdcall;
    function SetContext(const contextClass: WideString; const
contextSubject: WideString): HResult; stdcall;
  end;

Here's the implementation:

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

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

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

    elItem := SysAllocString(strPtr);

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

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

    SafeArrayUnlock(contextList);
    Result := S_OK;
end;

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.

amil...@shwd.com

Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.