COleDispatchDriver::CreateDispatch

BOOLCreateDispatch(REFCLSIDclsid**,COleException*pError=NULL);**

BOOLCreateDispatch(LPCTSTRlpszProgID**,COleException*pError=NULL);**

Return Value

Nonzero on success; otherwise 0.

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.

Remarks

Creates an object and attaches it to the COleDispatchDriver object.

Example

void CTestView::OnBatterylife()
{
   COleDispatchDriver disp;
   COleException *e = new COleException;

   try {
      // Create instance of Microsoft System Information Control
      // by using ProgID.
      if (disp.CreateDispatch("SYSINFO.Sysinfo.1", e))
      {
         //Call BatteryLifePercent.    
         short nBatteryLifePercent;

         disp.InvokeHelper(0x03, DISPATCH_PROPERTYGET, VT_I2,
            (void*)&nBatteryLifePercent, NULL);           

         if (nBatteryLifePercent == 255)
            AfxMessageBox("Battery Life % unknown");           
         else
         {
            CString cStr;
            cStr.Format("Battery Life is at %d%%",
                nBatteryLifePercent);
            AfxMessageBox(cStr);
         }
      }
      else
         throw e;

   }
  
   //Catch control-specific exceptions.
    catch (COleDispatchException * e)
   {
      CString cStr;

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

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

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

COleDispatchDriver OverviewClass MembersHierarchy Chart

See Also   COleDispatchDriver::DetachDispatch, COleDispatchDriver::ReleaseDispatch, COleDispatchDriver::AttachDispatch, COleException, COleDispatchDriver::m_lpDispatch