Problems implimenting a feature

Hi guys,

I've taken this code, and made some changes to it to suite my needs (added a few functions, and changed a few existing functions):

http://www.codeproject.com/internet/CustomBrowser.asp


Works fine. I now want to add the features found here:

http://www.codeproject.com/com/jscalls.asp

Into the same project, but not having much luck, could somebody kindly guide me into implimenting it into my code, and letting me know what I need to call to make JavaScript calls? Is it just:

m_webPage.CallJScript("MyFunction");

Or do I need to make pre-declarations first?


All help is appreciated!

Cheers guys 'n gals!
LVL 4
Cyber-DrugsAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mahesh1402IT ProfessionalCommented:
>>could somebody kindly guide me into implimenting it into my code, and letting me know what I need to call to make JavaScript calls?

Have you looked at how function CallJScript() implemented and tried to include same in your project ?

CComVariant CWebPage::CallJScript(const CString strFunc,const CStringArray& paramArray)
{
  //Getting IDispatch for Java Script objects
  CComPtr<IDispatch> spScript;
  if(!GetJScript(spScript))
  {
    ShowError("Cannot GetScript");
    return false;
  }
  //Find dispid for given function in the object
  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;
}

Have a look at class implementations CWebPage and CWebPage::CallJScript() srccode.

-MAHESH
mahesh1402IT ProfessionalCommented:
However following is a simple KB article for you to refer regarding this :

How To Call a Script Function from a VC WebBrowser Application
http://support.microsoft.com/kb/q185127/ <=====

-MAHESH
Cyber-DrugsAuthor Commented:
mahesh1402,

I checked out that link you sent me, not sure which to choose. Which one of the two would you recommend? I'll try to impliment it, and then tell you what issues I get (if any).


Cheers!
Why Diversity in Tech Matters

Kesha Williams, certified professional and software developer, explores the imbalance of diversity in the world of technology -- especially when it comes to hiring women. She showcases ways she's making a difference through the Colors of STEM program.

mahesh1402IT ProfessionalCommented:
As I said KB article will be easy to implement in your existing workspace..

How To Call a Script Function from a VC WebBrowser Application
http://support.microsoft.com/kb/q185127/ <=====

-MAHESH
Cyber-DrugsAuthor Commented:
I'm not quite sure what this is, is it required?

#import "C:\winnt\system32\mshtml.tlb" // location of mshtml.tlb


If I wanted it to call functions dynamically, would I change it to look like this?


   void CMyClass::ExecuteScriptFunction(LPARAM strFunction)
   {
      // m_WebBrowser is an instance of IWebBrowser2
      MSHTML::IHTMLDocument2Ptr spDoc(m_WebBrowser.GetDocument());

      if (spDoc)
      {
         IDispatchPtr spDisp(spDoc->GetScript());
         if (spDisp)
         {
            // Evaluate is the name of the script function.
            OLECHAR FAR* szMember = strFunction;
            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)");
            }
         }
      }
   }


and make use of it like this?

CMyClass::ExecuteScriptFunction(myFunctionNameHere);


Cheers!
mahesh1402IT ProfessionalCommented:
NO its not necessary to #import "C:\winnt\system32\mshtml.tlb" ....

use it like :

CMyClass objMyClass;

objMyClass.ExecuteScriptFunction(myFunctionNameHere);


-MAHESH
Cyber-DrugsAuthor Commented:
mahesh1402,

OK, I've only got one last problem... I think it's possibly from me missing something out, but I'm getting this error when I try compile:

fatal error C1010: unexpected end of file while looking for precompiled header directive

Which points to the last "}" in the function.



Cheers!
mahesh1402IT ProfessionalCommented:
I think you need to set 'Not using the precompiled headers' under Project->Settings->C/C++,
choose Category  'Precompiled Headers' and check Not using the precompiled headers..

Otherwise
you may need to add

#include "StdAfx.h"

as the 1st line of your source file(s).

-MAHESH
Cyber-DrugsAuthor Commented:
The first one left me with these 7:

\\hq-server\user docs\justinn\my documents\custombrowser\custombrowserjs.cpp(3) : error C2653: 'CMyClass' : is not a class or namespace name
\\hq-server\user docs\justinn\my documents\custombrowser\custombrowserjs.cpp(3) : error C2065: 'LPARAM' : undeclared identifier
\\hq-server\user docs\justinn\my documents\custombrowser\custombrowserjs.cpp(3) : error C2146: syntax error : missing ')' before identifier 'strFunction'
\\hq-server\user docs\justinn\my documents\custombrowser\custombrowserjs.cpp(3) : error C2182: 'ExecuteScriptFunction' : illegal use of type 'void'
\\hq-server\user docs\justinn\my documents\custombrowser\custombrowserjs.cpp(3) : error C2059: syntax error : ')'
\\hq-server\user docs\justinn\my documents\custombrowser\custombrowserjs.cpp(4) : error C2143: syntax error : missing ';' before '{'
\\hq-server\user docs\justinn\my documents\custombrowser\custombrowserjs.cpp(4) : error C2447: missing function header (old-style formal list?)


The second one left me with these 13:

\\Hq-server\User Docs\JustinN\My Documents\CustomBrowser\CustomBrowserJS.cpp(3) : error C2653: 'CMyClass' : is not a class or namespace name
\\Hq-server\User Docs\JustinN\My Documents\CustomBrowser\CustomBrowserJS.cpp(6) : error C2653: 'MSHTML' : is not a class or namespace name
\\Hq-server\User Docs\JustinN\My Documents\CustomBrowser\CustomBrowserJS.cpp(6) : error C2065: 'IHTMLDocument2Ptr' : undeclared identifier
\\Hq-server\User Docs\JustinN\My Documents\CustomBrowser\CustomBrowserJS.cpp(6) : error C2146: syntax error : missing ';' before identifier 'spDoc'
\\Hq-server\User Docs\JustinN\My Documents\CustomBrowser\CustomBrowserJS.cpp(6) : error C2065: 'spDoc' : undeclared identifier
\\Hq-server\User Docs\JustinN\My Documents\CustomBrowser\CustomBrowserJS.cpp(6) : error C2065: 'm_WebBrowser' : undeclared identifier
\\Hq-server\User Docs\JustinN\My Documents\CustomBrowser\CustomBrowserJS.cpp(6) : error C2228: left of '.GetDocument' must have class/struct/union type
\\Hq-server\User Docs\JustinN\My Documents\CustomBrowser\CustomBrowserJS.cpp(10) : error C2065: 'IDispatchPtr' : undeclared identifier
\\Hq-server\User Docs\JustinN\My Documents\CustomBrowser\CustomBrowserJS.cpp(10) : error C2146: syntax error : missing ';' before identifier 'spDisp'
\\Hq-server\User Docs\JustinN\My Documents\CustomBrowser\CustomBrowserJS.cpp(10) : error C2065: 'spDisp' : undeclared identifier
\\Hq-server\User Docs\JustinN\My Documents\CustomBrowser\CustomBrowserJS.cpp(10) : error C2227: left of '->GetScript' must point to class/struct/union
\\Hq-server\User Docs\JustinN\My Documents\CustomBrowser\CustomBrowserJS.cpp(14) : error C2440: 'initializing' : cannot convert from 'long' to 'unsigned short *'
        Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
\\Hq-server\User Docs\JustinN\My Documents\CustomBrowser\CustomBrowserJS.cpp(17) : error C2227: left of '->GetIDsOfNames' must point to class/struct/union
mahesh1402IT ProfessionalCommented:
probably you need

#include "stdafx.h"

-MAHESH
Cyber-DrugsAuthor Commented:
mahesh1402,
The 13 errors above are what occur when I include the stdafx.h file mentioned.
Cyber-DrugsAuthor Commented:
mahesh1402,
Would it help at all if I uploaded the project to www.ee-stuff.com and pasted a link here?
mahesh1402IT ProfessionalCommented:
ok !
mahesh1402IT ProfessionalCommented:
cyber-drugs
you missed \res folder in your project 's zip file.
Cyber-DrugsAuthor Commented:
mahesh1402IT ProfessionalCommented:
cyber-drugs

instead of creating a new file like CustomBrowserJS.CPP just add this function as member of existing class...

for example open CustomModalDialog.cpp and just define it like

#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)");
        }
     }
  }
}

you also need to add entry in CCustomModalDialog.h like

class CCustomModalDialog : public CDialog
{
// Construction
public:
      CString cszURL;
      HICON m_hIcon;
      CCustomModalDialog(CWnd* pParent = NULL);   // standard constructor
      void ExecuteScriptFunction();

....
....
}

just delete file custombrowserjs.cpp from workspace..now you may call and rebuild

-MAHESH

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cyber-DrugsAuthor Commented:
That worked beautifully!

Thank you for all your time and help!! :)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.