C ++ compile ActiveX controls based on ATL Engineering

Source: Internet
Author: User

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?
  1. <Object classid = "CLSID: 4738a542-7aeb-4ce4-86a7-71081b8ce8da" id = "myaddobj"
  2. 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?
  1. Library addcomlib
  2. {
  3. Importlib ("stdole2.tlb ");
  4. [
  5. UUID (64ec1270-5626-4fdf-888f-41859fb35e15)
  6. ]
  7. Coclasscompreg
  8. {
  9. [Default] interface icomponentregistrar;
  10. };
  11. [
  12. UUID (FE912C18-0CFB-4DCA-99FA-6E4BC4B07594)
  13. ]
  14. Dispinterface_iaddevents
  15. {
  16. Properties:
  17. Methods:
  18. };
  19. [
  20. UUID (4738a542-7aeb-4ce4-86a7-71081b8ce8da)
  21. ]
  22. Coclass add
  23. {
  24. [Default] interface iadd;
  25. [Default, source] dispinterface_iaddevents;
  26. };
  27. };

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?
  1. 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?
  1. VaR result = myaddobj. Add (1, 2 );
  2. Alert (result );

VaR result = myaddobj. Add (1, 2); </P> <p> alert (result); </P> <p>8. Pack ActiveX into the cab To be continued .....

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.