Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Modifying a function

Posted on 2006-11-15
4
Medium Priority
?
625 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 2000 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

719 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