CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    CHtmlView - javascript accessing your c++ variables/functions ?

    I have a project that has a CHtmlView. This html view displays user customizable html files.

    However from my program I want to supply variables (strings mainly) and/or provide functions that can be called from the javascript of the hosted pages.

    The idea is that the javascript can then use those variables to dynamically generate the content the users want to be seeing in the form they want to see it.

    Essentially... Start a new project in MFC.
    Template: MFC application
    In Generated classes: chance 'CView' to 'CHtmlView'.
    Open the view class and change the Navigate2() call to point to a local html file.
    Code:
    Navigate2(_T("c:\\test.htm"),NULL,NULL);
    create c:\test.html with following contents
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head>
        <title>Untitled Page</title>
    </head>
    <script type="text/jscript" language="jscript">
    	function ConstructPage() {
    		var mydiv = document.getElementById("MyDiv");
    		mydiv.innerHTML = "MyVariableHere ?";
    	}
    </script>
    <body onload="ConstructPage();">
    	<div>Header</div>
    	<div id="MyDiv"></div>
    	<div>Footer</div>
    </body>
    </html>
    Now, when running, I would like the "MyVariableHere?" be replaced by a variable (CString) that's being calculated from the C++ code. How do I set script variables from my C++ code" ?

    The page doesn't need to autoupdate as the CString changes it just needs to be able to use the variables at load. If it could get updated values so you can set a timer in the script to keep a portion updated that would be awesome, but not required.

  2. #2
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: CHtmlView - javascript accessing your c++ variables/functions ?

    I'm not sure, but maybe you can use this method and call a JavaScript function once the page is loaded?
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  3. #3
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: CHtmlView - javascript accessing your c++ variables/functions ?

    I don't really want to call a jscript function at all. I want to define and set a bunch of variables. If I can make some methods that the script can call as well that would be even better.

    And while I could achieve this more or less by requiring that users make a function with a specific name, then call it repeatedly in an "OnSetVariable()" type approach. This would be quite annoying for the users.

    The link you provided allows the C++ to call into the script. I'm actually trying to do the reverse of this... I want the script to be able to access/read the C++ variables/code of the running instance of the exe. Multiple exe's might be running concurrently.

  4. #4
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: CHtmlView - javascript accessing your c++ variables/functions ?

    I know it's the other way around, but I was indeed thinking of doing something like OnSetVariable. But you are right, it's not the cleanest solution
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  5. #5
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: CHtmlView - javascript accessing your c++ variables/functions ?

    Made some progress in this.

    Apparently what needs to be done in CHtmlView is override the OnGetExternal() function and return a pointer to an IDispatch enabled object.
    So I assume i can do something along the lines of:
    Code:
    CMyClass mydata;  // global static for now easier to test)
    /*virtual*/ HRESULT CMyHtmlView::OnGetExternal(LPDISPATCH *lppDispatch)
    {
    	*lppDispatch = &mydata;
                    return S_OK;
    }
    That would make my current problem:
    Suppose I have
    Code:
    class CMyClass
    {
    public:
         CMyClass() {}
    
         int m_iMyInt;
         CString m_strMyString;
    };
    How do I IDispatch-enable the above class so that the int and CString members are accessible?
    (can't say I have a lot of experience with COM).
    The solution would need to work without registering a com object in the registry, and preferably without needing an IDL thinghy. (the data provided is semi dynamic).

    I looked around but can't seem to find a simple explanation of what you need to do to get this working. :s

  6. #6
    Join Date
    Feb 2013
    Posts
    2

    Re: CHtmlView - javascript accessing your c++ variables/functions ?

    Hi,

    I encountered the same problem.. when I override the OnGetExternal() function of ChtmlView, I have a problem with AJAX calls...

  7. #7
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: CHtmlView - javascript accessing your c++ variables/functions ?

    I got this to work by creating an IDispatch interface around my own class. Things work well enough although I'm sure some parts of the code could be done cleaner. I'm a bit more aware of how to do COM in c++ by now.

    I can't really help with AJAX, I'm not even sure how that's related to this, AJAX is about making webservice calls and returning data to be used in your html script.

  8. #8
    Join Date
    Feb 2013
    Posts
    2

    Re: CHtmlView - javascript accessing your c++ variables/functions ?

    I tried to do same but not succeed...

    Can you put here an example how you implemented IDispach interface please?

    thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured