共用方式為


COleDispatchDriver::CreateDispatch

建立 IDispatch 介面物件並將其附加至 COleDispatchDriver 物件。

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

參數

  • clsid
    將會建立的 IDispatch 連接物件的分類 ID。

  • pError
    為 OLE 例外狀況物件的指標,會保留狀態碼由建立。

  • lpszProgID
    若要以程式設計方式識別項的指標,例如「Excel.Document.5」分派,定義要建立的 Automation 物件。

傳回值

在不成功,則為 0。

範例

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();
}

需求

Header: afxdisp.h

請參閱

參考

COleDispatchDriver 類別

階層架構圖

COleDispatchDriver::DetachDispatch

COleDispatchDriver::ReleaseDispatch

COleDispatchDriver::AttachDispatch

COleException 類別

COleDispatchDriver::m_lpDispatch