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.
First 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:
OBJECT_ENTRY_AUTOmacro in the header file: it “connects” between your class and the ATL framework class-factory.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 component, you have to implement
IObjectSafetyin your class: just inherit the c++ class frompublic IObjectSafetyImpl<CYourClass, INTERFACESAFE_FOR_UNTRUSTED_CALLER | INTERFACESAFE_FOR_UNTRUSTED_DATA>, and addCOM_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
objecttag, or dynamically, usingnew 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.