Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Check if a function works

Posted on 2006-11-29
8
Medium Priority
?
340 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
[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
  • 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

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: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

721 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