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.

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.

November 4th, 2010, 03:12 PM

Marc G

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?

November 5th, 2010, 05:04 AM

OReubens

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.

November 5th, 2010, 05:49 AM

Marc G

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 :(

November 5th, 2010, 07:08 AM

OReubens

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:

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

February 11th, 2013, 04:09 AM

tumyp

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...

February 11th, 2013, 08:07 AM

OReubens

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.

February 11th, 2013, 09:14 AM

tumyp

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?