none
what do the V_VT and V_UNKNOWN macros do?

    Question

  • what does the V_VT and V_UNKNOWN macros do in the following code sample?

    Looking for a good COM programming tutorial.

    thanks,

    HRESULT CRecipePreviewHandler::DoPreview()
    {
      HRESULT hr = E_FAIL;
      
      // cannot call more than once (Unload should be called before another DoPreview)
      if (_hwndPreview == NULL && _pStream) 
      {
        IXMLDOMDocument *pDomDoc;
        
        // We will use the DOM interface from MSXML to load up our file (which is xml format)
        // and convert it to rich text format using an xml style-sheet
        hr = CoCreateInstance(
          __uuidof(DOMDocument60), NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pDomDoc));
        if (SUCCEEDED(hr))
        {
          VARIANT_BOOL vfSuccess = VARIANT_FALSE;
          VARIANT vtXmlSource = {0};
          V_VT(&vtXmlSource) = VT_UNKNOWN;
          V_UNKNOWN(&vtXmlSource) = static_cast<IUnknown *>(_pStream);
          hr = pDomDoc->load(vtXmlSource, &vfSuccess);
    
                if (vfSuccess != VARIANT_TRUE)
                {
                    hr = FAILED(hr) ? hr : E_FAIL; // keep failed hr
                }
    
                if (SUCCEEDED(hr))
                {
                    if (_pStyleSheetNode == NULL)
                    {
                        hr = _CreateStyleSheetNode();
                    }
    
                    if (SUCCEEDED(hr))
                    {
                        BSTR bstrRtf;
                        hr  = pDomDoc->transformNode(_pStyleSheetNode, &bstrRtf);
                        if (SUCCEEDED(hr))
                        {
                            hr = _CreatePreviewWindow(bstrRtf);
                            SysFreeString(bstrRtf);
                        }
                    }
                }
                pDomDoc->Release();
            }
        }
        return hr;
    }
    

    Monday, May 21, 2012 10:18 PM

Answers

  • On 5/21/2012 6:18 PM, Steve Richter wrote:

    what does the V_VT and V_UNKNOWN macros do in the following code sample?

    They provide a convenient shorthand to access VARIANT fields. E.g.

    V_VT(&vtXmlSource) = VT_UNKNOWN;

    is equivalent to

    vtXmlSource.vt = VT_UNKNOWN;

    V_UNKNOWN similarly accesses VARIANT::punkVal field. That's all there is to it.


    Igor Tandetnik

    • Marked as answer by Steve Richter Monday, May 21, 2012 11:05 PM
    Monday, May 21, 2012 10:27 PM