CComPtr and CDHtmlDialog
-
I have created a CDHtmlDialog base application and I want to create IHTMLOptionElement from an external source into an IHTMLSelectElement. I have managed to do this. However, I am not sure if my code will leak: // SelectID is already empty CComPtr pSelect; HRESULT hr = GetElementInterface(_T("SelectID"), &pSelect); if (FAILED(hr)) return; CComPtr pWindow; hr = m_spHtmlDoc->get_parentWindow(&pWindow); if (FAILED(hr)) return; CComPtr pOptionFactory; hr = pWindow->get_Option(&pOptionFactory); if (FAILED(hr)) return; while (Source.MoveNext()) { CComPtr pOption; hr = pOptionFactory->create(CComVariant(Source.Col(0), Source.Col(1), CComVariant(VARIANT_FALSE), CComVariant(VARIANT_FALSE), &pOption); if (FAILED(hr)) return; CComPtr pElement; pElement = pOption; // This works but what is it doing? // IHTMLSelectElement::add(IHTMLElement*, VARIANT); hr = pSelect->add(pElement, CComVariant(-1)); // This works! if (FAILED(hr)) return; } Is the code "pElement = pOption;" used correctly above? Geo
-
I have created a CDHtmlDialog base application and I want to create IHTMLOptionElement from an external source into an IHTMLSelectElement. I have managed to do this. However, I am not sure if my code will leak: // SelectID is already empty CComPtr pSelect; HRESULT hr = GetElementInterface(_T("SelectID"), &pSelect); if (FAILED(hr)) return; CComPtr pWindow; hr = m_spHtmlDoc->get_parentWindow(&pWindow); if (FAILED(hr)) return; CComPtr pOptionFactory; hr = pWindow->get_Option(&pOptionFactory); if (FAILED(hr)) return; while (Source.MoveNext()) { CComPtr pOption; hr = pOptionFactory->create(CComVariant(Source.Col(0), Source.Col(1), CComVariant(VARIANT_FALSE), CComVariant(VARIANT_FALSE), &pOption); if (FAILED(hr)) return; CComPtr pElement; pElement = pOption; // This works but what is it doing? // IHTMLSelectElement::add(IHTMLElement*, VARIANT); hr = pSelect->add(pElement, CComVariant(-1)); // This works! if (FAILED(hr)) return; } Is the code "pElement = pOption;" used correctly above? Geo
Thats fine. The assignment operator for CComPtr will call AddRef for you, or in this case take an assigment from the underyling IUnknown of pOption and do a QueryInterface on it for you - which in tern does the AddRef. When using the assingment operators you should always check the assinged pointer for NULL as this is the only way CCom(QI)Ptr has to report any failure code for you - unlike when you call QI manually and you get your HRESULT back