Click here to Skip to main content
15,868,419 members
Articles / Desktop Programming / MFC

JavaScript call from C++

Rate me:
Please Sign up or sign in to vote.
4.90/5 (62 votes)
7 Jul 2011CPOL2 min read 560.8K   20.6K   224   94
A class for easy implementation of JavaScript calls from C++ code.

Sample Image

Introduction

Sometimes when you are using the IE Browser Control inside of a C++ application, you need to get access to the HTML elements. We can do it by using standard COM objects like IWebBrowser2, IHTMLDocument2, etc. By this method, we can easily implement features like click button, click anchor, get input string, get HTML text, etc. Unfortunately, Microsoft did not provide similar objects for JavaScript. At any case, to make control for a JavaScript object inside of an HTML page is possible by using the traditional COM approach. This article describes the class CWebPage which allows to do it and a technique to call a JavaScript function from C++ code.

How to do

As a result of using the presented class, it will be easy to call any JavaScript function from C++ code. For implementing this feature, we should get a pointer to the IHTMLDocument2 interface. If we are using the CHtmlView class from MFC, we can get one by using the member function CHtmlView::GetHtmlDocument(). In the case of using IWebBrowser or IWebBrowser2 components, the function get_Document will bring us the desired interface. Here is an example:

C++
CComPtr<IDispatch> spDisp = CHtmlView::GetHtmlDocument();
m_webPage.SetDocument(spDisp);

The rest of the things will be done by the CWebPage class. Here is an example of a JavaScript call without parameters:

C++
m_webPage.CallJScript("Welcome");

An example of a JavaScript call with two parameters will looks like:

C++
m_webPage.CallJScript("Miltiply","2.34","3.32");

The class implementation

C++
class CWebPage
{
public:
  CWebPage();
  virtual ~CWebPage();

  bool SetDocument(IDispatch* pDisp);
  LPDISPATCH GetHtmlDocument() const;
  const CString GetLastError() const;
  bool GetJScript(CComPtr<IDispatch>& spDisp);
  bool GetJScripts(CComPtr<IHTMLElementCollection>& spColl);
  CString ScanJScript(CString& strAText,CStringArray& args);

  bool CallJScript(const CString strFunc);
  bool CallJScript(const CString strFunc,const CString strArg1);
  bool CallJScript(const CString strFunc,const CString strArg1,
                   const CString strArg2);
  bool CallJScript(const CString strFunc,const CString strArg1,
                   const CString strArg2,const CString strArg3);
  bool CallJScript(const CString strFunc,const CStringArray& paramArray);

protected:

  CComPtr<IHTMLDocument2> m_spDoc;

};

Calling technique

The above mentioned technique can be split into the following steps:

  • Getting a pointer to the IHTMLDocument2 interface.
  • Getting IDispatch for the JavaScript object in the HTML document.
  • Getting the DISPID for the given name of the JavaScript function.
  • Putting the parameters to the DISPPARAM structure.
  • Calling the JavaScript function by using the Invoke method of the IDispatch interface.

Here is an example of getting an IDispatch pointer to the JavaScript objects:

C++
bool CWebPage::GetJScript(CComPtr<IDispatch>& spDisp)
{
  HRESULT hr = m_spDoc->get_Script(&spDisp);
  ATLASSERT(SUCCEEDED(hr));
  return SUCCEEDED(hr);
}

And here is the final function to call JavaScript:

C++
CComVariant CWebPage::CallJScript(const CString strFunc,
                                  const CStringArray& paramArray)
{
  //Getting IDispatch for Java Script objects
  CComPtr<IDispatch> spScript;
  if(!GetJScript(spScript))
  {
    ShowError("Cannot GetScript");
    return false;
  }
  //Find dispid for given function in the object
  CComBSTR bstrMember(strFunc);
  DISPID dispid = NULL;
  HRESULT hr = spScript->GetIDsOfNames(IID_NULL,&bstrMember,1,
                            LOCALE_SYSTEM_DEFAULT,&dispid);
  if(FAILED(hr))
  {
    ShowError(GetSystemErrorMessage(hr));
    return false;
  }
  
  const int arraySize = paramArray.GetSize();
  //Putting parameters  
  DISPPARAMS dispparams;
  memset(&dispparams, 0, sizeof dispparams);
  dispparams.cArgs      = arraySize;
  dispparams.rgvarg     = new VARIANT[dispparams.cArgs];
  dispparams.cNamedArgs = 0;
  
  for( int i = 0; i < arraySize; i++)
  {
    CComBSTR bstr = paramArray.GetAt(arraySize - 1 - i); // back reading
    bstr.CopyTo(&dispparams.rgvarg[i].bstrVal);
    dispparams.rgvarg[i].vt = VT_BSTR;
  }
  EXCEPINFO excepInfo;
  memset(&excepInfo, 0, sizeof excepInfo);
  CComVariant vaResult;
  UINT nArgErr = (UINT)-1;  // initialize to invalid arg
  //Call JavaScript function         
  hr = spScript->Invoke(dispid,IID_NULL,0,
                        DISPATCH_METHOD,&dispparams,
                        &vaResult,&excepInfo,&nArgErr);
  delete [] dispparams.rgvarg;
  if(FAILED(hr))
  {
    ShowError(GetSystemErrorMessage(hr));
    return false;
  }
  return vaResult;
}

Notes about the demo

To call a JavaScript function from the demo, you should select a function in the tree in the left window. After this, press the "!" button on the menu bar.

History

  • July 07, 2011: Updated download files.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralRe: Memory leak while dealing parameters. Pin
thucnh4-May-10 20:55
thucnh4-May-10 20:55 
GeneralRe: Memory leak while dealing parameters. Pin
liujun32124-Sep-13 23:30
liujun32124-Sep-13 23:30 
GeneralGetting the url of an object Pin
Craig D.27-Jul-05 9:37
Craig D.27-Jul-05 9:37 
QuestionHow to change the UserAgent of embeded browser Pin
akhilesh.kumar13-Jul-05 7:20
akhilesh.kumar13-Jul-05 7:20 
AnswerRe: How to change the UserAgent of embeded browser Pin
yu_cg4-Sep-07 7:48
yu_cg4-Sep-07 7:48 
Generalaccess to the HTML elements Pin
Jomateix15-Mar-05 4:01
Jomateix15-Mar-05 4:01 
Generalmemory overflow Pin
spring24-Jan-05 19:30
spring24-Jan-05 19:30 
GeneralPass Object to JavaScript Pin
bleleer28-Dec-04 19:15
bleleer28-Dec-04 19:15 
GeneralRe: Pass Object to JavaScript Pin
Amit22031-Jul-07 6:41
Amit22031-Jul-07 6:41 
GeneralCalling Action script (flash) function Pin
KmAshif9-Nov-04 23:22
KmAshif9-Nov-04 23:22 
GeneralRe: Calling Action script (flash) function Pin
Deepak Demiwal8-Dec-08 22:35
Deepak Demiwal8-Dec-08 22:35 
Generalc# Pin
Ikarus769-Sep-04 22:11
Ikarus769-Sep-04 22:11 
GeneralVery nice !! Pin
WREY20-May-04 4:27
WREY20-May-04 4:27 
Generalhelp - going both ways ... Pin
Benos Glatkos12-May-04 16:24
Benos Glatkos12-May-04 16:24 
GeneralRe: help - going both ways ... Pin
Mohan Bhargava16-Apr-10 12:25
Mohan Bhargava16-Apr-10 12:25 
GeneralRe: help - going both ways ... Pin
ehsiung3-Feb-11 4:55
ehsiung3-Feb-11 4:55 
QuestionHow can we set eventhandler to call a Javascript function Pin
H2K31-Mar-04 18:46
H2K31-Mar-04 18:46 
Questionhow to run encoded script? Pin
wangniu8-Dec-03 3:19
wangniu8-Dec-03 3:19 
Questionhow to call in &lt;frame&gt; Pin
budwiser3-Sep-03 20:45
budwiser3-Sep-03 20:45 
GeneralRe: how to call in <frame> Pin
SevenWow28-Dec-14 0:27
professionalSevenWow28-Dec-14 0:27 
QuestionServer-side??? Pin
Flyguy30-Jun-03 20:24
Flyguy30-Jun-03 20:24 
Questionhow to bypass intermediate response pages when using WebBrowser controls Pin
shivsun12-Jun-03 23:01
shivsun12-Jun-03 23:01 
GeneralProblem in getting the html content Pin
shivsun11-Jun-03 0:18
shivsun11-Jun-03 0:18 
GeneralRe: Problem in getting the html content Pin
Eugene Khodakovsky11-Jun-03 3:00
Eugene Khodakovsky11-Jun-03 3:00 
GeneralRe: Problem in getting the html content Pin
shivsun12-Jun-03 0:23
shivsun12-Jun-03 0:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.