Solved

Check if a function works

Posted on 2006-11-29
8
321 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
 
LVL 4

Author Comment

by:Cyber-Drugs
ID: 18039149
hr is:


-2147352570
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 86

Accepted Solution

by:
jkr earned 500 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

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 …
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
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 video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

705 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

18 Experts available now in Live!

Get 1:1 Help Now