共用方式為


COleDispatchDriver::CreateDispatch

Creates an IDispatch interface object and attaches it to the COleDispatchDriver object.

BOOL CreateDispatch(
   REFCLSID clsid,
   COleException* pError = NULL 
);
BOOL CreateDispatch(
   LPCTSTR lpszProgID,
   COleException* pError = NULL 
);

Parameters

  • clsid
    Class ID of the IDispatch connection object to be created.

  • pError
    Pointer to an OLE exception object, which will hold the status code resulting from the creation.

  • lpszProgID
    Pointer to the programmatic identifier, such as "Excel.Document.5", of the automation object for which the dispatch object is to be created.

Return Value

Nonzero on success; otherwise 0.

Example

void COleContainerView::OnCreateDispatch()
{
   COleDispatchDriver disp;
   COleException* pe = new COleException;

   try 
   {
      // Create instance of Microsoft System Information Control 
      // by using ProgID.
      if (disp.CreateDispatch(_T("WMPlayer.OCX"), pe))
      {
         //Get uiMode.     
         CString strUIMode;
         disp.InvokeHelper(23, DISPATCH_PROPERTYGET, VT_BSTR, 
            (void*)&strUIMode, NULL);            

         CString strMsg;
         strMsg.Format(_T("WMP uiMode is %s."), strUIMode);
         AfxMessageBox(strMsg);
      }
      else
      {
         throw pe;
      }
   }
   //Catch control-specific exceptions.
    catch (COleDispatchException* pe) 
   {
      CString cStr;

      if (!pe->m_strSource.IsEmpty())
         cStr = pe->m_strSource + _T(" - ");
      if (!pe->m_strDescription.IsEmpty())
         cStr += pe->m_strDescription;
      else
         cStr += _T("unknown error");

      AfxMessageBox(cStr, MB_OK, 
         (pe->m_strHelpFile.IsEmpty()) ?  0 : pe->m_dwHelpContext);

      pe->Delete();
   }
   //Catch all MFC exceptions, including COleExceptions.
   // OS exceptions will not be caught.
   catch (CException* pe) 
   {
      TRACE(_T("%s(%d): OLE Execption caught: SCODE = %x"), 
         __FILE__, __LINE__, COleException::Process(pe));
      pe->Delete();
   }

   pe->Delete();
}

Requirements

Header: afxdisp.h

See Also

Reference

COleDispatchDriver Class

Hierarchy Chart

COleDispatchDriver::DetachDispatch

COleDispatchDriver::ReleaseDispatch

COleDispatchDriver::AttachDispatch

COleException Class

COleDispatchDriver::m_lpDispatch

Other Resources

COleDispatchDriver Members