ATL creates COM objects

Source: Internet
Author: User

I used to write the DLL creation process and use LoadLibrary to load the dynamic library directly.

But ATL presents a very important feature that introduces the concept of COM objects.

First of all. The ATL Active Template Library is the active templates gallery. ATL to ASP provides code for COM applications.

Instead, you typically use the Active Template Library to create COM components. In short, ATL is generally used as a convenient and fast COM development tool.

And the basic technology used in ATL is COM technology. C + + template technology and C + + multi-inheritance technology.

The next step is the basic process of developing a COM component using ATL. Incidental:

1. Create a new ATL project:

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvbhvvx3hpyw5taw5n/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center ">

Just take a name and make sure.


In the Setup wizard, it is simple to create a DLL file.

2. Then choose to include a new class in the project:


The new class is selected as an ATL Simple object in ATL:

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvbhvvx3hpyw5taw5n/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center ">


3. Include the class name in the wizard:

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvbhvvx3hpyw5taw5n/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center ">

Will find. The wizard will proactively generate additional information, such as the class name. The component class name. Interface name, and so on.

There are some more detailed settings in the options:

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvbhvvx3hpyw5taw5n/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center ">

The direct point is complete.

4. In Class View, we see the generated Cfirst class and the Ifirst interface.


Join the method on the interface. The following specific information will be available:

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvbhvvx3hpyw5taw5n/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center ">

Fill in the method name, and then add the number of parameters. The return value of a COM function is used to check that the DLL function is correctly loaded and executed. So we use the parameters as the return value to pass. Here a simple addition function is implemented. For this function, the number of references is a, B, and Ret. RET is a long*. Used to store the return value.

Point in. Indicates that this parameter is the number of input parameters. At the generated code, you will voluntarily join the _in_, suggesting that this is the input, but it does not have much practical effect. Look for the corresponding data type in the type of the parameter and fill in the corresponding parameter name:

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvbhvvx3hpyw5taw5n/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center ">

For functions that do not have a return value, the last parameter is usually set to the pointer type. Return a value with its record function:

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvbhvvx3hpyw5taw5n/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center ">

The final number of references is:

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvbhvvx3hpyw5taw5n/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center ">

Point is complete.


5. Change the generated code. Join the implementation.

First we double-click the interface, will enter a Myfirstdll.idl file, which has the following code:

Interface ifirst:iunknown{[] HRESULT Add ([in] long A, [in] long B, [Out,retval] long* Ret);}; [UUID (f7dafd6a-c1db-46af-9cf9-62ec0d7d589f), version (1.0),]library myfirstdlllib{importlib ("Stdole2.tlb"); [ UUID (d2f7f834-d4a2-4ab7-b5be-b08d1eb35564)]coclass First{[default] interface Ifirst;};};
is an interface for use in other languages or systems.

The IUnknown here is also noteworthy, in the internal implementation of COM. All of the classes are inherited with this IUnknown base class.

For some of the more in-depth details of ATL COM. Contains: (IUnknown three functions, two other classes CComCoClass. CComObjectRootEx. DLL and Counter-register. The destructor for the object and the memory request and release. First, the COM object uses new or CoTaskMemAlloc to request memory, the former is internal to use. Self-release. The latter may request the memory to be used by the other party, so the other party can use the corresponding method to free memory. This content will later be more specific to learn it, first dig a hole. You can fill in the blanks later.



Then look at first.h. The declarations in the header file about the Cfirst class are as follows:

Class atl_no_vtable Cfirst:p ublic ccomobjectrootex<ccomsinglethreadmodel>,public CComCoClass<Cfirst, & Clsid_first>,public Ifirst{public:cfirst () {}declare_registry_resourceid (Idr_first) DECLARE_NOT_AGGREGATABLE ( Cfirst) Begin_com_map (Cfirst) com_interface_entry (Ifirst) End_com_map () declare_protect_final_construct () HRESULT FinalConstruct () {return S_OK;} void FinalRelease () {}public:stdmethod (ADD) (Long A, long B, long* Ret);};
This embodies ATL's basic technology, multiple inheritance, and templates. The inherited content has a ifirst, which is the interface previously in the IDL file. In the compiler, the direct red underline for this Ifirst interface is an error, and the explanation is actually a dynamic process, as far as detail is concerned. I don't want to get too into it.

This class declaration has a lot of complex macros to use. This is not the point, but the emphasis is on the Add function declaration in public.

Then we went into the Cfirst.cpp. Add a simple implementation to the Add function:

STDMETHODIMP Cfirst::add (Long A, long B, long* Ret) {//TODO: Add implementation code here *ret = A+b;return S_OK;}
This enables a simple COM component to be implemented.


6. Point generation, generate DLL.

After that is the transfer of COM objects.


Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.

ATL creates COM objects

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.