Solved

Modifying a function

Posted on 2006-11-15
4
599 Views
Last Modified: 2013-11-20
Hi guys,

I have a function which calls JavaScript functions via C++ code, but now need to modify it so that:

1/ I can define which JavaScript function I want to call, via a Parameter when calling this function.
2/ I can send up to 5 Parameters to the JavaScript Function via Paramters sent when calling this function.

Anybody able to help me out please?

Here is the code at the moment:

#import "C:\\windows\\system32\mshtml.tlb"   // **you need this !

void CCustomModalDialog::ExecuteScriptFunction()
{
  // m_WebBrowser is an instance of IWebBrowser2
  MSHTML::IHTMLDocument2Ptr spDoc(m_browser.GetDocument());

  if (spDoc)
  {
     IDispatchPtr spDisp(spDoc->GetScript());
     if (spDisp)
     {
        // Evaluate is the name of the script function.
         
        OLECHAR FAR* szMember = L"evaluate";
        DISPID dispid;

        HRESULT hr = spDisp->GetIDsOfNames(IID_NULL, &szMember, 1,
                                       LOCALE_SYSTEM_DEFAULT, &dispid);

        if (SUCCEEDED(hr))
        {
           COleVariant vtResult;
           static BYTE parms[] = VTS_BSTR;

           COleDispatchDriver dispDriver(spDisp, FALSE);

           dispDriver.InvokeHelper(dispid, DISPATCH_METHOD, VT_VARIANT,
                                   (void*)&vtResult, parms,
                                   "5+Math.sin(9)");
        }
     }
  }
}
0
Comment
Question by:Cyber-Drugs
  • 2
  • 2
4 Comments
 
LVL 22

Accepted Solution

by:
mahesh1402 earned 500 total points
ID: 17953678
Cyber-Drugs,

 You need to take reference of  this article : http://www.codeproject.com/com/jscalls.asp

Refer function CallJScript(const CString strFunc, const CStringArray& paramArray) <== here you can pass paramArray which is array of parameters as string.. you can pass any no. of parameters to this function.

Either you may think to use above function as it is ...or you can modify your existing function with reference of this funciton.

Modifie function will look like this :

#import "C:\\windows\\system32\mshtml.tlb"  

// function name and string array as parameters
void CCustomModalDialog::ExecuteScriptFunction(const CString strFunc,  const CStringArray& paramArray)
{

// m_WebBrowser is an instance of IWebBrowser2
  MSHTML::IHTMLDocument2Ptr spDoc(m_browser.GetDocument());

  IDispatchPtr spScript(spDoc->GetScript());

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

-MAHESH
0
 
LVL 4

Author Comment

by:Cyber-Drugs
ID: 17954314
mahesh1402,

Thanks for the fast reply, just to check though, would I also be able to send an array over to a JavaScript function as a parameter?


Cheers!
0
 
LVL 22

Expert Comment

by:mahesh1402
ID: 17954437
In above function its a provision implementged to accept paramteres using CStringArray.. you are suppose to pass parameters in this array as strings.

-MAHESH
0
 
LVL 4

Author Comment

by:Cyber-Drugs
ID: 17954463
Thanks mahesh1402, that's great! :)
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now