Handling An HTML Button Click Event In C++ Code
Solution 1:
You will need to create a COM object as the bridge between JavaScript and C++. The JavaScript initialization code will look something like:
var myhelper = new ActiveXObject("MyCompany.MyHelper");
Note that despite the name "ActiveXObject", the object does not have to be a full ActiveX control, just a COM object. Then in your onClick handler you can just do:
myhelper.DoThis();
myhelper.DoThat();
The "fun" part is creating the COM object in C++ - IDL files, ATL classes, IDispatch, IUnknown and all that stuff I am trying to forget.
One thing though - if the C++ code exists in your application exe (not in a separate COM DLL), don't forget that when your program starts up, you must create a class factory instance for "MyCompany.MyHelper" and register it with COM so that the "new ActiveXObject" in JavaScript succeeds.
[update 1]
I should have added in my initial response - I don't actually recommend doing this if you can avoid it. Back before C# and .NET existed, we in our company actually though COM was a good thing and it was worth the time and effort to learn it. Today ... not so much.
In your case, having your business logic in C++ and again in JavaScript might seem like a lot of extra work - but at least it is extra work that you can plan, allocate resources to and have a hope of being able to finish. Once you go down the path of C++/COM/ActiveScripting, when stuff stops working - there is no limit to the amount of time you might spend trying to chase down obscure COM related issues. Anyhow good luck!
Post a Comment for "Handling An HTML Button Click Event In C++ Code"