Calling all of you COM masters

Discussion in 'OT Technology' started by panzerfaust, Nov 15, 2004.

  1. panzerfaust

    panzerfaust New Member

    Joined:
    Oct 13, 2003
    Messages:
    1,604
    Likes Received:
    0
    Location:
    Far far away
    I need help dealing with a (VT_VARIANT | VT_ARRAY)

    let's say you have the following:

    IDocument* pDoc;
    VARIANT varRet;
    IItem *pItem;

    pDoc->get_Items(&varRet);

    Now, knowing that varRet returns an array of variants of type IItem how would you walk the array assigning each element to an IItem?

    Thanks for the help.
     
  2. panzerfaust

    panzerfaust New Member

    Joined:
    Oct 13, 2003
    Messages:
    1,604
    Likes Received:
    0
    Location:
    Far far away
    OK, this is what you need to do:

    Code:
    void ProcessSingleItem(VARIANT varItem)
    {
    	 IItem* pItem = NULL;
    	 VARIANT varItemValues;
    	 VARIANT varValue;
    	 LONG lValIdx = 0;
    	 HRESULT hr;
    	 CComBSTR bstrTemp;
    	 
    	 hr = varItem.pdispVal->QueryInterface(IID_IItem, (void**)&pItem);
    	 pItem->get_Values(&varItemValues);
    	 hr = SafeArrayGetElement(varItemValues.parray, &lValIdx, &varValue);
    	 while(SUCCEEDED(hr))
    	 {
    		   /*
    				  now you have to check vt variant type and process each
    				  accordingly
    		   */
    		   hr = SafeArrayGetElement(varItemValues.parray, &(++lValIdx), &varValue);
    	 }
    	 if(pItem) pItem->Release();
    }
    void ProcessCollection(IDocumentCollection* pColl)
    {
    	 IDocument* pDoc = NULL; 
    	 VARIANT varIItems;
    	 VARIANT varIItem;
    	 LONG lItemIdx = 0;
    	 HRESULT hr = NULL;
     
    	 coll->GetFirstDocument(&pDoc);
    	 while(pDoc != NULL)
    	 {
    		  pDoc->get_Items(&varIItems);
    		  lItemIdx = 0;
    		  hr = SafeArrayGetElement(varIItems.parray, &lItemIdx, &varIItem);
    		  while(SUCCEEDED(hr))
    		  {
    				ProcessSingleItem(varItem);
    				hr = SafeArrayGetElement(varIItems.parray, &(++lItemIdx), &varIItem);
    		  }
    	 }
    }
    
     

Share This Page