I need to create a COM object which is callable from Javascript. I've been told that to do this, you need a COM object which also implements a class factory and it was suggested here that I try ATL. Someone pointed me to a Code Project ATL sample, however that doesn't cover the class factory.
I'm able to build a small ATL project with a simple object, but am unable to instantiate it, presumable because I haven't provided the class factory.
So, does anyone have experience building a simple COM object in VS2010 via ATL which can be called from Javascript?
EDIT: well, in reading the documentation for the DECLARE_CLASSFACTORY_EX it sounds like a default class factory IS provided. So now I'm not sure what's missing. What is needed to instantiate and invoke the class from Javascript?
EDIT: Also, to clarify, the Javascript where I'm calling the object from is in an HTML page being shown in the IE ActiveX control in an MFC dialog app. So that's the context in which the Javascript will run.
I need to create a COM object which is callable from Javascript. I've been told that to do this, you need a COM object which also implements a class factory and it was suggested here that I try ATL. Someone pointed me to a Code Project ATL sample, however that doesn't cover the class factory.
I'm able to build a small ATL project with a simple object, but am unable to instantiate it, presumable because I haven't provided the class factory.
So, does anyone have experience building a simple COM object in VS2010 via ATL which can be called from Javascript?
EDIT: well, in reading the documentation for the DECLARE_CLASSFACTORY_EX it sounds like a default class factory IS provided. So now I'm not sure what's missing. What is needed to instantiate and invoke the class from Javascript?
EDIT: Also, to clarify, the Javascript where I'm calling the object from is in an HTML page being shown in the IE ActiveX control in an MFC dialog app. So that's the context in which the Javascript will run.
Share Improve this question edited Nov 6, 2012 at 4:25 Nerdtron asked Nov 6, 2012 at 4:12 NerdtronNerdtron 1,5261 gold badge21 silver badges33 bronze badges2 Answers
Reset to default 4First of all, I believe you mean MS-specific JScript for Internet Explorer, because other browsers might not support COM/ActiveX.
You don't have to implement class-factory explicitly, because ATL framework already provides it. Just run ATL wizard: View --> Class view; in Class view right-click project root --> Add --> Class --> ATL Simple Object; set class name, and leave the defaults for all the rest. The wizard will define the following main things:
- Interface and co-class that implements it, in IDL file.
- C++ class, which would be the actual implementation of the above co-class. Pay attention to
OBJECT_ENTRY_AUTO
macro in the header file: it "connects" between your class and the ATL framework class-factory. - *.rgs file, which is used to register your co-class in Windows registry.
Now you can add some methods to this newly created class - by means of wizard (right-click on class --> Add --> Add Function...), or manually.
Besides, in order to avoid security issues with your ponent, you have to implement IObjectSafety
in your class: just inherit the c++ class from public IObjectSafetyImpl<CYourClass, INTERFACESAFE_FOR_UNTRUSTED_CALLER | INTERFACESAFE_FOR_UNTRUSTED_DATA>
, and add COM_INTERFACE_ENTRY(IObjectSafety)
to the interface map.
Now your class is ready for use from within JScript. In the script you can either create it statically, using object
tag, or dynamically, using new ActiveX()
statement. If your object has connection-points (i.e. fires events) and you don't want to be too tricky, you have to use the former approach.
I published sample code project earlier in response to another question: Exposing COM events to VBScript (ATL). It uses VBS and I assume JS conversion is an easy thing.
Code: SVN, Browser Friendly.
Another related sample: SVN, Browser Friendly + blog post explain things going on: Three ways to implement VBScript (VB6, VBA) callback from C++/ATL class.