CreateStdDispatch

HRESULT CreateStdDispatch( 
  IUnknown FAR*  punkOuter,        
  void FAR*  pvThis,               
  ITypeInfo FAR*  ptinfo,          
  IUnknown FAR* FAR* ppunkStdDisp  
);
 

Creates a standard implementation of the IDispatch interface through a single function call. This simplifies exposing objects through Automation.

Parameters

punkOuter
Pointer to the object's IUnknown implementation.
pvThis
Pointer to the object to expose.
ptinfo
Pointer to the type information that describes the exposed object.
ppunkStdDisp
This is the private unknown for the object that implements the IDispatch interface QueryInterface call. This pointer is Null if the function fails.

Return Value

The return value obtained from the returned HRESULT is one of the following:

Return value Meaning
S_OK Success.
E_INVALIDARG One of the first three arguments is invalid.
E_OUTOFMEMORY There was insufficient memory to complete the operation.

Comments

You can use CreateStdDispatch when creating an object instead of implementing the IDispatch member functions for the object. However, the implementation that CreateStdDispatch creates has these limitations:

LoadTypeLib, GetTypeInfoOfGuid, and CreateStdDispatch comprise the minimum set of functions that you need to call to expose an object using a type library. For more information on LoadTypeLib and GetTypeInfoOfGuid, see Chapter 9, "Type Description Interfaces."

CreateDispTypeInfo and CreateStdDispatch comprise the minimum set of dispatch components you need to call to expose an object using type information provided by the INTERFACEDATA structure.

Example

The following code implements the IDispatch interface for the CCalc class using CreateStdDispatch.

CCalc FAR*
CCalc::Create()
{
    HRESULT hresult;
    CCalc FAR* pcalc;
    CArith FAR* parith;
    ITypeInfo FAR* ptinfo;
    IUnknown FAR* punkStdDisp;
extern INTERFACEDATA NEARDATA g_idataCCalc;

    if((pcalc = new FAR CCalc()) == NULL)
        return NULL;
    pcalc->AddRef();

    parith = &(pcalc->m_arith);

    // Build type information for the functionality on this object that
    // is being exposed for external programmability.
    hresult = CreateDispTypeInfo(
        &g_idataCCalc, LOCALE_SYSTEM_DEFAULT, &ptinfo);
    if(hresult != NOERROR)
        goto LError0;

    // Create an aggregate with an instance of the default
    // implementation of IDispatch that is initialized with
    // type information.
    hresult = CreateStdDispatch(
        pcalc,                // Controlling unknown.
        parith,                // Instance to dispatch on.
        ptinfo,                // Type information describing the instance.
    &punkStdDisp);

    ptinfo->Release();

    if(hresult != NOERROR)
        goto LError0;

    pcalc->m_punkStdDisp = punkStdDisp;

    return pcalc;

LError0:;
    pcalc->Release();
    return NULL;
}

QuickInfo

  Windows NT: Use version 3.1 and later.
  Windows: Use Windows 95 and later.
  Header: Declared in oleauto.h.
  Import Library: Link with oleaut32.lib.