I am working on a ATL COM project which will be used by both COM-based
clients and .NET clients. One of the interface methods in the ATL project
returns a VARIANT. Depending on the input to the method, the VARIANT may
contain a VT_BSTR, VT_I4, etc. It also may return an array of bytes (
VT_ARRAY | VT_UI1 ). For some reason, my COM-based clients (Visual Basic 6)
are quite happy receiving the array of bytes, but my .NET clients are
throwing a SafeArrayTypeMismatchException (Specified array was not of the
expected type). The implementation looks something like the following. Can
anyone see anything obvious I am doing wrong?
HRESULT CObject::GetData(VARIANT* value)
{
// build the return array
CComSafeArray<char> data;
HRESULT hr = data.Create(10, 0);
if (FAILED(hr)) return hr;
... code to populate array omitted ...
// stuff the array into a VARIANT
CComVariant returnValue;
returnValue.vt = (VT_ARRAY | VT_UI1);
returnValue.parray = data.Detach();
return returnValue.Detach(value);
}
Thanks
Bimish
Bimish - 05 Oct 2005 14:57 GMT
Nervermind. This is a type mismatch between char and BYTE. The SAFEARRAY
contains chars, but the VARIANT says it contains BYTEs. I changed the
CComSafeArray declaration to
CComSafeArray<BYTE> data;
Thanks
Bimish
>I am working on a ATL COM project which will be used by both COM-based
>clients and .NET clients. One of the interface methods in the ATL project
[quoted text clipped - 25 lines]
>
> Bimish