Fabulous Adventures In Coding

Eric Lippert's Blog

invoke.cpp

#include "headers.h"

HRESULT InvokeDispatch(IDispatch * pdisp, DISPID dispid, REFIID riid,
    LCID lcid, WORD flags, DISPPARAMS * pDispParams, VARIANT * pvarResult,
    EXCEPINFO * pExcepInfo, UINT * pError)
{
    AssertReadPtr(pdisp);

    HRESULT hr;

    // We must addref the pointer before the invocation.  Why?  Consider
    // this scenario:  the invocation calls a method which calls back
    // into the script engine, which triggers a garbage collection, which
    // does the final release on the dispatch object.  Then control returns
    // back to the call to Invoke, which dereferences its "this" pointer
    // and promptly crashes.

    pdisp->AddRef();

    hr = pdisp->Invoke(DISPID_VALUE, riid, lcid, flags, pDispParams,
        pvarResult, pExcepInfo, pError);

    pdisp->Release();

    return hr;
}

BOOL IsValidDispatch(VARIANT * pvar)
{
    AssertReadPtr(pvar);
    return VT_DISPATCH == pvar->vt && NULL != pvar->pdispVal;
}


 

Published Tuesday, May 04, 2004 10:04 AM by Eric Lippert
Filed under:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

No Comments

Leave a Comment

(required) 
(optional)
(required) 

  
Enter Code Here: Required
Submit

About Eric Lippert

Eric Lippert is a senior developer on the Microsoft C# compiler team. Before that he worked on the framework of Visual Studio Tools For Office. Before that, he worked on the compilers, runtimes and tools for VBScript, JScript, Windows Script Host and other Microsoft Scripting technologies. He lives in Seattle and spends his free time editing books about programming languages, playing the piano, and trying to keep his tiny sailboat upright in Puget Sound.

This Blog

Syndication


© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Microsoft
Page view tracker