Calling a Method with No Arguments

The simplest use of Invoke is to call a method that does not have any arguments. You only need to pass the DISPID of the method, a LCID, the DISPATCH_METHOD flag, and an empty DISPPARAMS structure.

The following example demonstrates the simplest use of Invoke. Error handling has been omitted for brevity.

HRESULT hresult;
IUnknown * punk;
IDispatch * pdisp = (IDispatch *)NULL;
OLECHAR * szMember = L"Simple";
DISPID dispid;
DISPPARAMS dispparamsNoArgs = {NULL, NULL, 0, 0};

hresult = CoCreateInstance(
            (void **)&punk);

hresult = punk->QueryInterface(IID_IDispatch, (void **)&pdisp);

hresult = pdisp->GetIDsOfNames(IID_NULL, &szMember, 1, LOCALE_USER_DEFAULT, &dispid);

hresult = pdisp->Invoke(
            &dispparamsNoArgs, NULL, NULL, NULL);

The example invokes a method named "Simple" on an object of the class CMyObject. First, it calls CoCreateInstance, which instantiates the object and returns a pointer to the object's IUnknown interface (punk). Next, it calls QueryInterface, receiving a pointer to the object's IDispatch interface (pdisp). It then uses pdisp to call the object's GetIDsOfNames function, passing the string "Simple" in szMember to get the DISPID for the Simple method. With the DISPID for Simple in dispid, it calls Invoke to invoke the method, specifying DISPATCH_METHOD for the wFlags parameter and using the system default locale.

To further simplify the code, the example declares a DISPPARAMS structure named dispparamsNoArgs that is appropriate to an Invoke call with no arguments.

Because the Simple method does not take any arguments and does not return a result, the puArgErr and pVarResult parameters are Null. In addition, the example passes Null for pExcepInfo, indicating that it is not prepared to handle exceptions and will handle only HRESULT errors.

Most methods, however, take one or more arguments. To invoke these methods, the DISPPARAMS structure should be filled in, as described in Passing Parameters .

Automation defines special DISPIDs for invoking an object's Value property (the default), and the members _NewEnum, and Evaluate. For details, see DISPID Data Type and Data Types, Structures, and Enumerations .