COleDispatchDriver Class

 

The new home for Visual Studio documentation is Visual Studio 2017 Documentation on docs.microsoft.com.

The latest version of this topic can be found at COleDispatchDriver Class.

Implements the client side of OLE automation.

class COleDispatchDriver  

Public Constructors

NameDescription
COleDispatchDriver::COleDispatchDriverConstructs a COleDispatchDriver object.

Public Methods

NameDescription
COleDispatchDriver::AttachDispatchAttaches an IDispatch connection to the COleDispatchDriver object.
COleDispatchDriver::CreateDispatchCreates an IDispatch connection and attaches it to the COleDispatchDriver object.
COleDispatchDriver::DetachDispatchDetaches an IDispatch connection, without releasing it.
COleDispatchDriver::GetPropertyGets an automation property.
COleDispatchDriver::InvokeHelperHelper for calling automation methods.
COleDispatchDriver::ReleaseDispatchReleases an IDispatch connection.
COleDispatchDriver::SetPropertySets an automation property.

Public Operators

NameDescription
COleDispatchDriver::operator =Copies the source value into the COleDispatchDriver object.
COleDispatchDriver::operator LPDISPATCHAccesses the underlying IDispatch pointer.

Public Data Members

NameDescription
COleDispatchDriver::m_bAutoReleaseSpecifies whether to release the IDispatch during ReleaseDispatch or object destruction.
COleDispatchDriver::m_lpDispatchIndicates the pointer to the IDispatch interface attached to this COleDispatchDriver.

COleDispatchDriver does not have a base class.

OLE dispatch interfaces provide access to an object's methods and properties. Member functions of COleDispatchDriver attach, detach, create, and release a dispatch connection of type IDispatch. Other member functions use variable argument lists to simplify calling IDispatch::Invoke.

This class can be used directly, but it is generally used only by classes created by the Add Class wizard. When you create new C++ classes by importing a type library, the new classes are derived from COleDispatchDriver.

For more information on using COleDispatchDriver, see the following articles:

COleDispatchDriver

Header: afxdisp.h

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.
}

Constructs a COleDispatchDriver object.

COleDispatchDriver();  
COleDispatchDriver(LPDISPATCH lpDispatch, BOOL bAutoRelease = TRUE);  
  COleDispatchDriver(const COleDispatchDriver& dispatchSrc);

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.

dispatchSrc
Reference to an existing COleDispatchDriver object.

Remarks

The form COleDispatchDriver( LPDISPATCH``lpDispatch, BOOLbAutoRelease = TRUE) connects the IDispatch interface.

The form COleDispatchDriver( constCOleDispatchDriver& dispatchSrc) copies an existing COleDispatchDriver object and increments the reference count.

The form COleDispatchDriver( ) creates a COleDispatchDriver object but does not connect the IDispatch interface. Before using COleDispatchDriver( ) without arguments, you should connect an IDispatch to it using either COleDispatchDriver::CreateDispatch or COleDispatchDriver::AttachDispatch. For more information, see Implementing the IDispatch Interface.

Example

See the example for 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();
}

Detaches the current IDispatch connection from this object.

LPDISPATCH DetachDispatch();

Return Value

A pointer to the previously attached OLE IDispatch object.

Remarks

The IDispatch is not released.

For more information about the LPDISPATCH type, see Implementing the IDispatch Interface in the Windows SDK.

Example

LPDISPATCH CreateLPDispatch(LPCTSTR lpszProgId)
{
    COleDispatchDriver disp;

    disp.CreateDispatch(lpszProgId);

    return disp.DetachDispatch();
}

Gets the object property specified by dwDispID.

void GetProperty(
    DISPID dwDispID,  
    VARTYPE vtProp,  
    void* pvProp) const;  

Parameters

dwDispID
Identifies the property to be retrieved.

vtProp
Specifies the property to be retrieved. For possible values, see the Remarks section for COleDispatchDriver::InvokeHelper.

pvProp
Address of the variable that will receive the property value. It must match the type specified by vtProp.

Example

CString IMyComObject::GetString(DISPID dispid)
{
    CString result;
    GetProperty(dispid, VT_BSTR, (void*)&result);
    return result;
}

Calls the object method or property specified by dwDispID, in the context specified by wFlags.

void AFX_CDECL InvokeHelper(
        DISPID dwDispID,  
        WORD wFlags,  
        VARTYPE vtRet,  
        void* pvRet,  
        const BYTE* pbParamInfo, ...);

Parameters

dwDispID
Identifies the method or property to be invoked.

wFlags
Flags describing the context of the call to IDispatch::Invoke. . For a list of possible values, see the wFlags parameter in IDispatch::Invoke in the Windows SDK.

vtRet
Specifies the type of the return value. For possible values, see the Remarks section.

pvRet
Address of the variable that will receive the property value or return value. It must match the type specified by vtRet.

pbParamInfo
Pointer to a null-terminated string of bytes specifying the types of the parameters following pbParamInfo.

...
Variable list of parameters, of types specified in pbParamInfo.

Remarks

The pbParamInfo parameter specifies the types of the parameters passed to the method or property. The variable list of arguments is represented by ... in the syntax declaration.

Possible values for the vtRet argument are taken from the VARENUM enumeration. Possible values are as follows:

SymbolReturn Type
VT_EMPTYvoid
VT_I2short
VT_I4long
VT_R4float
VT_R8double
VT_CYCY
VT_DATEDATE
VT_BSTRBSTR
VT_DISPATCHLPDISPATCH
VT_ERRORSCODE
VT_BOOLBOOL
VT_VARIANTVARIANT
VT_UNKNOWNLPUNKNOWN

The pbParamInfo argument is a space-separated list of VTS_ constants. One or more of these values, separated by spaces (not commas), specifies the function's parameter list. Possible values are listed with the [EVENT_CUSTOM]--brokenlink--(../Topic/not%20found.md#event_custom) macro.

This function converts the parameters to VARIANTARG values, then invokes the IDispatch::Invoke method. If the call to Invoke fails, this function will throw an exception. If the SCODE (status code) returned by IDispatch::Invoke is DISP_E_EXCEPTION, this function throws a COleException object; otherwise it throws a COleDispatchException.

For more information, see VARIANTARG, Implementing the IDispatch Interface, IDispatch::Invoke, and Structure of COM Error Codes in the Windows SDK.

Example

See the example for COleDispatchDriver::CreateDispatch.

If TRUE, the COM object accessed by m_lpDispatch will be automatically released when ReleaseDispatch is called or when this COleDispatchDriver object is destroyed.

BOOL m_bAutoRelease;  

Remarks

By default, m_bAutoRelease is set to TRUE in the constructor.

For more information on releasing COM objects, see Implementing Reference Counting and IUnknown::Release in the Windows SDK.

Example

   // Clean up by forcing Release to be called
   // on COleDispatchDriver object and delete
   if (bError)
   {
      pDisp->m_bAutoRelease = TRUE;
      delete pDisp;
      pDisp = NULL;
   }

The pointer to the IDispatch interface attached to this COleDispatchDriver.

LPDISPATCH m_lpDispatch;  

Remarks

The m_lpDispatch data member is a public variable of type LPDISPATCH.

For more information, see IDispatch in the Windows SDK.

Example

See the example for COleDispatchDriver::AttachDispatch.

Copies the source value into the COleDispatchDriver object.

const COleDispatchDriver& operator=(const COleDispatchDriver& dispatchSrc);

Parameters

dispatchSrc
A pointer to an existing COleDispatchDriver object.

Accesses the underlying IDispatch pointer of the COleDispatchDriver object.

operator LPDISPATCH();

Example

   COleDispatchDriver disp;
   if (disp.CreateDispatch(_T("WMPlayer.OCX")))
   {
      IDispatch* pDispatch = disp; //COleDispatchDriver::operator
                                   //LPDISPATCH is called here
      IUnknown* pUnkn = NULL;
      HRESULT hr = pDispatch->QueryInterface(IID_IUnknown ,(void**)&pUnkn);
      if (SUCCEEDED(hr))
      {
         //Do something...
         pUnkn->Release();
      }
   }

Releases the IDispatch connection. For more information, see Implementing the IDispatch Interface

void ReleaseDispatch();

Remarks

If auto release has been set for this connection, this function calls IDispatch::Release before releasing the interface.

Example

See the example for COleDispatchDriver::AttachDispatch.

Sets the OLE object property specified by dwDispID.

void AFX_CDECL SetProperty(
    DISPID dwDispID,  
    VARTYPE vtProp, ...);

Parameters

dwDispID
Identifies the property to be set.

vtProp
Specifies the type of the property to be set. For possible values, see the Remarks section for COleDispatchDriver::InvokeHelper.

...
A single parameter of the type specified by vtProp.

Example

void IMyComObject::SetString(DISPID dispid, LPCTSTR propVal)
{
    SetProperty(dispid, VT_BSTR, propVal);
}

MFC Sample CALCDRIV
MFC Sample ACDUAL
Hierarchy Chart
CCmdTarget Class

Show: