CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Apr 2003
    Location
    kathmandu, nepal
    Posts
    1,570

    COM and IDispatch

    I have the following code:

    Code:
    // Get all of the children components from this m_Component object
    _variant_t componentChildren = pComponent->GetChildren();
    The returned variant contains a SafeArray of IDispatch pointers. I have verified that the count of the SafeArray is correct, but the IDispatch pointers are all NULL.

    I extract the array elements like below:

    Code:
    if ((componentChildren.vt == VT_EMPTY) || (V_VT(&componentChildren) == VT_NULL)) // Error - array is empty or null
    	return 0;       
    
    SAFEARRAY* psa = V_ARRAY(&componentChildren);
    HRESULT hres = SafeArrayAccessData(psa, (void **)pChildren); 
    long highIndex;
    
    SafeArrayGetUBound(psa, 1, &highIndex); // Get index number of highest array element
    
    // The array range is from 0 to highIndex
    
    long childrenCount = highIndex + 1;  // Actual # of array elements is highIndex + 1
    
    if(pChildren)
    {
    	// Recursively traverse the children
    	for (int i = 0; i < childrenCount; i++) // For each child component
    	{
    		TraverseChildren(RecurseLevel, pChildren[i]);
    	}
    }
    The pChildren[i] causes an access violation error.

    What could be the probable reasons for this. Please help I am stuck in this.
    If there is no love sun won't shine

  2. #2
    Join Date
    Mar 2002
    Location
    Izhevsk, Udmurtia, Russia
    Posts
    930

    Re: COM and IDispatch

    You should use an extra level of indirection:
    Code:
    HRESULT hres = SafeArrayAccessData(psa, (void **) & pChildren);
    With best wishes,
    Vita
    -----------------------
    Russian Software Development Network -- http://www.rsdn.ru

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured