Solved

Modifying a function

Posted on 2006-11-15
4
611 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
notReplace  challenge 53 153
lucky13 challenge 11 167
tripleUp challenge 7 97
child constructor and parent constructor, overriding and overloading 6 95
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

739 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