EQuestionAnswers.com Computer/Electronics Questions and Answers
C, C++, VC++, COM/DCOM, DLL and more
#Login #Sign up  Facebook Twitter TGoogle+
 

#Previous question #Index of COM/DCOM Questions #Next question

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

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

 Vote 0

Similar topics related to this section

# COM DCOM Interview Questions and Answers :: ALL Interview .com
# COM DCOM - COM DCOM Interview Questions
# COM interview questions | TechInterviews
# COM Interview Questions - CodeProject
# Com Dcom Interview Questions - Com and Dcom in Asp Net
# ATL Interview Questions and Answers :: ALL Interview .com
# ActiveX Interview Questions and Answers :: ALL Interview .com

* #1 webmaster Tue 29 Apr/2014 05:29:56(GMT)  Like 0 Unlike 0

Dear Users,

We are pleased to inform you that a forum/blog has been incorporated with www.mybestnotes.co.in. You are welcomed to add your comments, requests, codes, solutions and feedback to it. Please login(if already a member) or signup(for free) to avail to this facility.

Regards,
Webmaster

Your message goes here:

Name:Guest
Email:anonymous@unknown.com
My Post:*
Secutiry Code: ******  *
Preview this compose before posting this in discussion forum.
 
Note:
  1. Use [Sxx] code to show smiles. Example [S02]
  2. Use [URL ], example [URL http://www.google.com/]
  3. To display code or un formatted text use [CODE] [/CODE]. Example: [CODE] printf("Hello world"); [/CODE]
 [S01]   [S02]   [S03]   [S04]   [S05]   [S06]   [S07]   [S08] 
 [S09]   [S10]   [S11]   [S12]   [S13]   [S14]   [S15]   [S16] 
 [S17]   [S18]   [S19]   [S20]   [S21]   [S22]   [S23]   [S24] 

Note: Only members are allowed, * fields are mandatory.