Re: SAFEARRAY issue.

Tech-Archive recommends: Speed Up your PC by fixing your registry



<subashjayan@xxxxxxxxx> wrote in message
news:1129283636.659307.202470@xxxxxxxxxxxxxxxxxxxxxxxxxxxx
> int nCount = 3;
>
> SAFEARRAY *pSA;
> SAFEARRAYBOUND rgsaBound[1];
> rgsaBound[0].lLbound = 0;
> rgsaBound[0].cElements = nCount;
> long aLong = 1;
>
> pSA = SafeArrayCreate(VT_VARIANT, 1, rgsaBound);

This creates an array with valid indexes of 0, 1 and 2. Note that aLong
is initialized to 1, not to 0.

> for ( int n = 0; n < 3; n++)
> {
> VARIANT vOut;
> VariantInit(&vOut);
> vOut.vt= VT_BSTR; // set type
> vOut.bstrVal= ::SysAllocString(L"Test");
> if (FAILED( SafeArrayPutElement(pSA, &aLong, &vOut)))
> {
> VariantClear(&vOut); // release BSTR from memory on error
> SafeArrayDestroy(pSA); // does a deep destroy on error
> }
> VariantClear(&vOut); // does a deep destroy of source VARIANT
> aLong++;
> nCount++;

This loop accesses elements 1, 2 and 3. The last access fails, at which
point you destroy the array. But you don't set pSA to NULL, so it now
contains a dangling pointer.

> VariantInit(&p_udt->Special);
> p_udt->Special.vt= VT_SAFEARRAY|VT_BSTR; // set type
> p_udt->Special.parray = pSA ; // BSTR array

And here you pack the dangling pointer into a VARIANT.

> p_udt->lStatus =1;
> p_udt->lChannelNumber = 10;
> VariantClear(&p_udt->Special);

And here you cause double destruction. By the way, what's the point of
setting up a VARIANT and then clearing it immediately?

> }
> hr = ::SafeArrayUnaccessData( *SequenceArr );
>
> i could create all arrays. But while i am accessing the elements , the
> result is getting NULL, the erro code is "0x80070057"
> "The parameter is incorrect".

Now you know why.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925


.



Relevant Pages

  • Re: SAFEARRAY issue.
    ... Igor Tandetnik wrote: ... > point you destroy the array. ... > And here you pack the dangling pointer into a VARIANT. ...
    (microsoft.public.vc.atl)
  • COM VARIANT to C#
    ... so this method will return a VARIANT. ... The SAFEARRAY contains a vectorarr; ... int nSize = arrNames.size; ... SAFEARRAY *psa; ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: safearray and invoke
    ... Because of previous SafeArrayCreate(VT_VARIANT, 1, aDim) the author ... creates the array of VARIANT. ... pSA =SafeArrayCreate; ...
    (microsoft.public.vc.atl)
  • Re: ReDim not working as expected. Array expert needed.
    ... MsgBox TypeName) ' Type Variant() ... examples of how array dimensions work in VBA. ... Sub ArrayStudies14() ... Dim MyArray As Variant ' Declare nonarray variant. ...
    (microsoft.public.excel.programming)
  • Re: C++ Conversion functions for pointers
    ... I know I can create an array of Variant pointers and assign to these ... >> But the problem I am having is converting an int pointer to a Variant ...
    (alt.comp.lang.learn.c-cpp)