共用方式為


COleDispatchDriver::AttachDispatch

Call the AttachDispatch member function to attach an IDispatch pointer to the COleDispatchDriver object. For more information, see Implementing the IDispatch Interface.

void AttachDispatch(
   LPDISPATCH lpDispatch,
   BOOL bAutoRelease = TRUE 
);

Parameters

  • lpDispatch
    Pointer to an OLE IDispatch object to be attached to the COleDispatchDriver object.

  • bAutoRelease
    Specifies whether the dispatch is to be released when this object goes out of scope.

Remarks

This function releases any IDispatch pointer that is already attached to the COleDispatchDriver object.

Example

void COleContainerView::OnAttachDispatch()
{
   CLSID clsidWMP;
   LPDISPATCH pWMPDispatch = NULL;
   COleDispatchDriver oddWMP;

   try
   {
      AfxCheckError(::CLSIDFromProgID(_T("WMPlayer.OCX"), &clsidWMP));

      AfxCheckError(::CoCreateInstance(clsidWMP, NULL, CLSCTX_INPROC_SERVER, 
         IID_IDispatch, (LPVOID*)&pWMPDispatch));

      oddWMP.AttachDispatch(pWMPDispatch, TRUE);
      pWMPDispatch = NULL; // our COleDispatchDriver now owns the interface

      CString strUIMode;
      oddWMP.GetProperty(23, VT_BSTR, (void*)&strUIMode);
      TRACE(_T("WMP uiMode is %s.\n"), strUIMode);
   }
   catch (COleException* pe)
   {
      pe->ReportError();
      pe->Delete();
   }
   catch (CMemoryException* pe)
   {
      pe->ReportError();
      pe->Delete();
   }

   // cleanup
   if (NULL != pWMPDispatch)
   {
      pWMPDispatch->Release();   
   }

   // COleDispatchDriver automatically releases the dispatch interface when
   // it goes out of scope if m_bAutoRelease is TRUE.
}

Requirements

Header: afxdisp.h

See Also

Concepts

COleDispatchDriver Class

COleDispatchDriver Members

Hierarchy Chart

COleDispatchDriver::DetachDispatch

COleDispatchDriver::ReleaseDispatch

COleDispatchDriver::CreateDispatch

COleDispatchDriver::m_lpDispatch

COleDispatchDriver::m_bAutoRelease