Overview

COM DCOM uses ID binding to find a method or property of a COM object. We have discussed topics like C++ static binding, C++ dynamic binding, DCOM ID binding, GetIDsOfNames etc. Please visit this page C++ static binding, C++ dynamic binding, DCOM ID binding, GetIDsofName() function in IDispatch()?

compile time binding

dynamic binding vptr vtable

idispatch id binding invoke getidsofname

IDispatch::Invoke

Invoke function can be used to get or set object properties and calling object methods. GetIDsOfNames is used to get the dispatch identity number of the property or method and same number is passed to Invoke to call the actual procedure.

HRESULT Invoke(
  [in]      DISPID     dispIdMember,
  [in]      REFIID     riid,
  [in]      LCID       lcid,
  [in]      WORD       wFlags,
  [in, out] DISPPARAMS *pDispParams,
  [out]     VARIANT    *pVarResult,
  [out]     EXCEPINFO  *pExcepInfo,
  [out]     UINT       *puArgErr
);

Parameters

  • dispIdMember [in] Unique number which identifies the member property or method returned by GetIDsOfNames.
  • riid [in] Reserved and must be IID_NULL.
  • lcid [in] The locale context in which to interpret arguments.
  • wFlags [in] Flags describing the context of the Invoke call.
    • DISPATCH_METHOD The member is invoked as a method. If a property has the same name, both this and the DISPATCH_PROPERTYGET flag can be set.
    • DISPATCH_PROPERTYGET The member is retrieved as a property or data member.
    • DISPATCH_PROPERTYPUT The member is changed as a property or data member.
    • DISPATCH_PROPERTYPUTREF The member is changed by a reference assignment, rather than a value assignment. This flag is valid only when the property accepts a reference to an object.
  • pDispParams [in, out] Pointer to a DISPPARAMS structure containing an array of arguments, an array of argument DISPIDs for named arguments, and counts for the number of elements in the arrays.
  • pVarResult [out] Pointer to the location where the result is to be stored, or NULL if the caller expects no result. This argument is ignored if DISPATCH_PROPERTYPUT or DISPATCH_PROPERTYPUTREF is specified.
  • pExcepInfo [out] Pointer to a structure that contains exception information. This structure should be filled in if DISP_E_EXCEPTION is returned. Can be NULL.
  • puArgErr [out] The index within rgvarg of the first argument that has an error. Arguments are stored in pDispParams->rgvarg in reverse order, so the first argument is the one with the highest index in the array. This parameter is returned only when the resulting return value is DISP_E_TYPEMISMATCH or DISP_E_PARAMNOTFOUND. This argument can be set to null. For details, see Returning Errors.

Return value

This method can return S_OK on Success and various error codes - DISP_E_BADPARAMCOUNT, DISP_E_BADVARTYPE, DISP_E_EXCEPTION,DISP_E_MEMBERNOTFOUND, DISP_E_NONAMEDARGS, DISP_E_OVERFLOW, DISP_E_PARAMNOTFOUND, DISP_E_TYPEMISMATCH, DISP_E_UNKNOWNINTERFACE, DISP_E_UNKNOWNLCID, DISP_E_PARAMNOTOPTIONAL etc.

Invoke demo

We are using Invoke method to set the Visible property of IWebBrowser to TRUE and then calling Invoke to call Navigate method and browsing google.com. Disp IDs are taken from our previous example using GetIDsOfNames(). This will make Internet explorer visible and then it will open google.com.

#include <stdio.h>
#include <exdispid.h>      /* IE Events */
#include <objbase.h>      /* COM initialization */
#include <mshtml.h>        /* Internet Explorer IHTML Objects */
#include <mshtmcid.h>      /* Additional Internet Explorer Constants */
#include <mshtmhst.h>      /* Additional Internet Explorer Constants */
#include <exdisp.h>

enum enmMethods{
  IePropVisible = 402,   /* Visible => id(402) */
  IeMethodNavigate = 104, /* Navigate => id(104) */
};

int main(int argc, char* argv[])
{
  IDispatch *pBrowser;
  IUnknown *pUnknown;
  CLSID clsid;
  HRESULT hr;
  UINT nErr;
  DISPPARAMS dp = {NULL, NULL, 0, 0};
  DISPID named;
  VARIANT *args;

  CoInitialize(NULL);
  hr = CLSIDFromProgID(OLESTR("InternetExplorer.Application"), &clsid);
  if(hr != S_OK)
    return hr;

  hr = CoCreateInstance(clsid, NULL, CLSCTX_SERVER, IID_IUnknown, (LPVOID *) &pUnknown);
  if(hr != S_OK)
    return hr;

  hr = pUnknown->QueryInterface(IID_IWebBrowser, (LPVOID *) &pBrowser);
  if(hr != S_OK)
    return hr;

  wprintf(L"Invoke demo with IWebBrowser interface\r\n");

  /* IWebBrowser.Visible = TRUE using Invoke */
  args = new VARIANT[1];
  named = DISPID_PROPERTYPUT;
  VariantInit(&args[0]);
  args[0].vt = VT_BOOL;
  args[0].boolVal = TRUE;
  dp.rgvarg = args;
  dp.cArgs = 1;
  dp.rgdispidNamedArgs = &named;
  dp.cNamedArgs = 1;

  pBrowser->Invoke(IePropVisible,
                   IID_NULL,
                   LOCALE_SYSTEM_DEFAULT,
                   DISPATCH_PROPERTYPUT,
                   &dp,NULL,NULL,&nErr);

  /* IWebBrowser.Navigate(URL) using Invoke */
  args[0].vt = VT_BSTR;
  args[0].bstrVal = SysAllocString(L"http://www.google.com/");
  dp.rgvarg = args;
  dp.cArgs = 1;
  dp.rgdispidNamedArgs = NULL;
  dp.cNamedArgs = 0;
  pBrowser->Invoke(IeMethodNavigate,
                   IID_NULL,
                   LOCALE_SYSTEM_DEFAULT,
                   DISPATCH_METHOD,
                   &dp,NULL,NULL,&nErr);

  pUnknown->Release();
  pBrowser->Release();
  CoUninitialize();
  SysFreeString(args[0].bstrVal);
  delete args;
  return 0;
}

Output

idispatch invoke demo

About our authors: Team EQA

You have viewed 1 page out of 67. Your COM/DCOM learning is 0.00% complete. Login to check your learning progress.

#