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”安排对象要创建的,自动化对象。

返回值

非零在成功;否则为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