Development Environment: vs2010
Development Project: C ++ ATL Project
Objective: To create ActiveX for JS call
Steps:
1. Create an ATL Project
Select the dynamic link library (DLL) for the application type, select COM + 1.0, and check the support for Part registries.
2. Select the Class View and Add Interfaces and Classes
If the class view is not found, you can add a View to the toolbar.
Select a project and right-click to add a class. Select an ATL simple object.
Enter the object information.
Note that you can use new activexobject ("Your progid") in JS to create an object after entering the progid.
Select the three supported options. The thread model I selected is single-threaded and can be modified as needed. The Add. h and Add. c files are generated. There are some red Ripple lines in it to indicate errors.
3. In the Class View, find the corresponding interface adding method. For example, if the simple object name I created is add, right-click the iadd node to add the method.
When adding a method, note that the out and retval must be pointer types. Otherwise, do not select this option. Here, add three parameters, where result is returned (the retval parameter is checked ).
If you select retval, you will not be added to the parameter again, indicating that only one return value is allowed. Check only the out parameter. js has no effect. C # and so on can use the out parameter of this function.
4. Find the Add. c file and implement the specific method in the added function.
5. After compilation is successful, find addcom. dll in the project directory (debug or release.
Use the regsvr32 addcom. dll command to register. After the registration is successful, compile an HTML code test.
6. Create an object in two ways.
1) create an object using the object tag
[HTML]View plaincopyprint?
- <Object classid = "CLSID: 4738a542-7aeb-4ce4-86a7-71081b8ce8da" id = "myaddobj"
- Name = "myaddobj"> </Object>
<Object classid = "CLSID: 4738a542-7aeb-4ce4-86a7-71081b8ce8da" id = "myaddobj" </P> <p> name = "myaddobj"> </Object>
Similar to the object created by this label, you can directly access the myaddobj object through Js. Classid can be found in the addcom. IDL file, and the ID corresponding to coclass add in the library.
[CPP]View plaincopyprint?
- Library addcomlib
- {
- Importlib ("stdole2.tlb ");
- [
- UUID (64ec1270-5626-4fdf-888f-41859fb35e15)
- ]
- Coclasscompreg
- {
- [Default] interface icomponentregistrar;
- };
- [
- UUID (FE912C18-0CFB-4DCA-99FA-6E4BC4B07594)
- ]
- Dispinterface_iaddevents
- {
- Properties:
- Methods:
- };
- [
- UUID (4738a542-7aeb-4ce4-86a7-71081b8ce8da)
- ]
- Coclass add
- {
- [Default] interface iadd;
- [Default, source] dispinterface_iaddevents;
- };
- };
Library addcomlib </P> <p >{</P> <p> importlib ("stdole2.tlb "); </P> <p> [</P> <p> UUID (64ec1270-5626-4fdf-888f-41859fb35e15) </P> <p>] </P> <p> coclasscompreg </P> <p >{</P> <p> [Default] interface icomponentregistrar; </P> <p >}; </P> <p> [</P> <p> UUID (FE912C18-0CFB-4DCA-99FA-6E4BC4B07594) </P> <p>] </P> <p> dispinterface_iaddevents </P> <p >{</P> <p> properties: </P> <p> Methods: </P> <p >}; </P> <p> [</P> <p> UUID (4738a542-7aeb-4ce4-86a7-71081b8ce8da) </P> <p>] </P> <p> coclass add </P> <p >{</P> <p> [Default] interface iadd; </P> <p> [default, source] dispinterface_iaddevents; </P> <p> };
2) create an object through new activexobject
[JavaScript]View plaincopyprint?
- VaR myaddobj = new activexobject ("myaddobj.1 ");
VaR myaddobj = new activexobject ("myaddobj.1 ");
Myaddobj is the progid filled in when you create a simple object. This can be found in the Add. RGS resource file.
7. After the object is created, you can directly call the corresponding function. For example, the result = add (arg1, arg2) method just implemented.
[JavaScript]View plaincopyprint?
- VaR result = myaddobj. Add (1, 2 );
- Alert (result );
VaR result = myaddobj. Add (1, 2); </P> <p> alert (result); </P> <p>8. Pack ActiveX into the cab To be continued .....