Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Check if a function works

Posted on 2006-11-29
8
Medium Priority
?
343 Views
Last Modified: 2013-11-20
Hi guys,

I have this function:

void CCustomBrowserDlg::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;
}


It compiles, and doesn't throw any errors when run, but doesn't call the javascript function I ask it to... It doesn't call anything, could somebody check I'm using it correctly please?


myString.Add("test");
ExecuteScriptFunction("Evaluate", myString);


with this javascript:

                  function evaluate(x)
                  {
                        alert("hello - "+x);
                        return eval(x);
                  }



Cheers guys!
0
Comment
Question by:Cyber-Drugs
  • 4
  • 4
8 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 18038583
>>and doesn't throw any errors when run

Since you commented out the calls to the functions that display the errors, that's not really surprising. Have you tried debuggin that function and stepping through it to find out what's going wrong?
0
 
LVL 4

Author Comment

by:Cyber-Drugs
ID: 18038685
I updated the code to this:

void CCustomBrowserDlg::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))
  {
       AfxMessageBox("FAILED(hr) - 1");
    //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))
  {
       AfxMessageBox("FAILED(hr) - 2");
    //ShowError(GetSystemErrorMessage(hr));
    return;// false;
  }

  return;// vaResult;
}



and get the messagebox:


FAILED(hr) - 1



Any thoughts on what the issue could be? My original function call worked fine, I just wanted to make it dynamic so I could call any JavaScript function and parse variables to it...

void CCustomBrowserDlg::ExecuteScript()
{
  // 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
 
LVL 86

Expert Comment

by:jkr
ID: 18039128
What is 'hr' when it fails? That's the really important information that is missing.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 4

Author Comment

by:Cyber-Drugs
ID: 18039149
hr is:


-2147352570
0
 
LVL 86

Accepted Solution

by:
jkr earned 2000 total points
ID: 18039186
That is

//
// MessageId: DISP_E_UNKNOWNNAME
//
// MessageText:
//
//  Unknown name.
//
#define DISP_E_UNKNOWNNAME               _HRESULT_TYPEDEF_(0x80020006L)

So that function does not seem to be available for some reason.
0
 
LVL 4

Author Comment

by:Cyber-Drugs
ID: 18039225
Could you possibly see if there is a better way of turning my working static function into a dynamic? That would solve my issue...
0
 
LVL 4

Author Comment

by:Cyber-Drugs
ID: 18039945
I found the problem, and have moved the problem to a new question as to be more specific on my problem.

Cheers!
0
 
LVL 86

Expert Comment

by:jkr
ID: 18042718
Great that you got it working. Sorry for being late, but there are meetings I just have to attend...
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
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.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Suggested Courses

972 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