ATL introduction

xiaoxiao2021-03-06  41

ATL introduction

Lu Siwei Pan Aimin

Beijing University Computer Science and Technology Research Institute (100871)

Workers must be good,

1. What is ATL?

Since Microsoft has announced the first COM technology in 1993, the development model on the Windows platform has undergone tremendous changes, and the WINDOWS programming is brought into the component chemical era in the development model of the WINDOWS platform. The majority of developers are extremely inconvenient for the difficulty and cumbersome details of COM development techniques. COM programming is seen as a highly uncomfortable technology, which is expected. Developers hope to have a convenient and fast COM development tool to improve development efficiency and better utilize this technology.

In this case, Microsoft has launched the COM SDK, in order to simplify COM programming, improve development efficiency, and have adopted many programs, especially in the Microsoft Foundation Class, add support for COM and OLE. However, with the development of the Internet, distributed component technology requires COM components to be transmitted on the network, and we will save valuable network bandwidth resources. The COM components developed by MFC have not met this requirement due to various limits, so Microsoft has introduced a new COM development tool in 1995.

ATL is an abbreviation of ActiveX Template Library, which is a set of C template libraries. Using ATL quickly develops efficient, concise code (Effective and Slim Code), while providing maximum code automatic generation and visual support for the development of COM components. For convenience, starting from the Microsoft Visual C 5.0 version, Microsoft integrates ATL into the Visual C development environment. The Visual Studio 6.0 introduced in September 1998 integrates ATL 3.0. At present, ATL has become an important member of Microsoft standard development tools, increasingly attached importance to C developers.

What benefits have ATL bring to developers? This will also start from ATL to the previous COM development method.

Before ATL generated, there are two ways to develop COM components: First, use COM SDK to directly develop COM components, and another way is achieved by COM support provided by MFC.

It is the most flexible way to develop COM components directly using COM SDK. By using Microsoft, we can write COM programs directly. However, this difficulty and workload of this development method are very large. On the one hand, the developer is required to have a more in-depth understanding of the technology principles of COM (although the deep understanding of the technology itself is very beneficial to the use of any kind of tool. However, for a complete set of complex technologies such as COM, it is very difficult to master in a short time), on the other hand, directly using the COM SDK to request the developers to realize every detail of the COM application, complete a lot of repetition Sexual work. The result of this is that not only the work efficiency is reduced, but also makes developers have to put many energy into technical details that are not related to the application needs itself. Although this development method is necessary for some special applications, this programming method does not meet the reusability of the component chemical design method, so it is not an ideal development method that the COM SDK is used directly.

The COM support from the MFC is used to develop COM applications that the automation is improved on the basis of using the COM SDK, and the development time is shortened. MFC uses object-oriented ways to encapsulate COM's basic functionality in a C class of several MFCs, developers get COM support functions by inheriting these classes. In order to facilitate the various features of the COM object, there are many predefined macros in the MFC. These macros are functional to implement the definition of the COM interface and the functionality of the object's registration and the like to be used in the COM object. Developers can use these macros to customize the characteristics of COM objects. In addition, support for Automation and ActiveX Control, Visual C also provides corresponding AppWizard and ClassWizard support for these two aspects, which is more convenient for COM applications.

MFC's support for COM and OLE is indeed more advanced than manual writing COM programs. However, the support of MFC is not perfect and thorough, such as the IDL language defined by the COM interface, and the MFC does not support, and there is no flexible support for the new development of COM and ActiveX technology in recent years. This is determined by the basic starting point designed by the MFC. The MFC is designed to develop the object-oriented package developed by the Windows platform. It naturally needs to involve Windows programming, COM is also supported by the MFC as a part of the programming development of Windows platform, but the support of MFC is based on its global target The starting point, so support for COM must obey its global goal. In this respect, MFC's support for COM can meet the requirements of developers.

With the development of Internet technology, Microsoft has vigorously promoted ActiveX technology as an important part of its network strategy, but uses MFC developed ActiveX Control, the code redundancy is large (so-called "fertilizer code fat code"), and must Depending on the MFC's runtime library to run correctly. Although the MFC's runtime library is only related to COM, the ActiveX Control must carry the heavy burden of running moments due to the essence of MFC's inheritance. If you use a static connection MFC running time library, this will make the ActiveX Control code too large, which will occupy a valuable network bandwidth resource when transferring on the network; if you use a dynamically connected MFC running time library, this will ask the browser one side. Must have MFC's runtime library support. In short, MFC is also very flexible in the environment of COM technology.

Solving the problem in the above COM development method is the basic goal of ATL.

First, the basic goal of ATL is to make COM application as much as possible, this basic goal determines that ATL is only supported for COM development. The goal clearly enables ATL to support COM technology. Any link and procedure for COM development, ATL is supported and integrated with many tools related to COM development into a unified programming environment. ATL provides comprehensive Wizard support for the various applications of COM / ActiveX. All of this greatly facilitates the use of developers, enabling developers to focus on logically related to the application itself.

Secondly, ATLs have used their specific basic implementation techniques to get rid of a large amount of redundant code, and the code of the COM application developed using ATL is concise, that is, the so-called "Slim Code". The ATL is implemented as much as possible, even in the inside of its internal C / C development, the alternative portion of the C startup code must have. At the same time, the code generated by the ATL does not need to rely on the huge code module that is required by the MFC program, which is included in the final module is the user's considered to be the most basic and most. These measures enable the COM components developed by ATL (including ActiveX Control) to implement application distributed component structures in a network environment. Third, the various versions of ATL have good support for Microsoft's Based on COM-based components such as MTS, ASP, etc., ATLs are much faster than the new technology. ATL has become a major development tool for Microsoft support COM application, so the new progress in COM technology will be reflected in ATL in a short period of time. This allows developers to use ATL for COM programming to get the same flexibility and powerful features directly using COM SDK programming.

The purpose of this paper is that it is desirable to make readers in a limited space to have a preliminary understanding of the use and basic principles of ATL. For the majority of COM developers to better use ATL development to throw jade.

Two. ATL basic technology

Although developing COM applications using ATL is a very simple thing, but there is a complex technique behind an easy-to-use interface of ATL. In the face of a large number of code generated by ATL, we can develop COM applications even if we don't understand the meaning of these codes, but if we have to fully explore the potential of ATL, develop more flexible, powerful COM applications, must The basic techniques used by ATL have aware of the basic techniques. Studying the essence of ATL is the ATL source code provided by Visual C . This part of this article is only a brief introduction to the most basic technologies used in ATL.

Simply put, the basic technologies used in the ATL include the following aspects:

l COM technology

L C template technology (Template)

L C Multi Inheritance Technology (Multi-Inheritance)

COM technology is the basis of understanding ATL, using ATL development to have a minimum understanding of the basic concept of COM technology. Since COM is a very complex and huge technical system, it is limited to the parameters of this article, and details will not be described here. For the COM basic concepts mentioned herein, do not do too much, please refer to the reference books.

The template for the most core implementation technology of ATL is an extension of standard C language, but in most C programming environments, people rarely use it, because the template's function is very strong, but its internal mechanism is more complicated. It is necessary to use more C knowledge and experience to be flexible. The function classes such as COBJECTARRAY in the MFC are defined by templates. Fully pass the template to define the overall class structure of the program, ATL is the most successful.

The so-called template class is simply related to the abstraction of the class. We know that the C language class defines the way constructor (herein the C object instead of the COM object), the object is an instance of the class, and the template class defines the construction of the class, which is generated using the template class definition. It is different. Therefore, it can be said that the template class is "class".

The definition format of the template class in the C language is as follows:

Template

Class myTemp

{

MyTemp () {};

~ MyTemp () {};

Int Myfunc (INT A);

}

..........

INT MyTemp :: MyFunc (INT A)

{

}

First use the C keyword "template" to declare a template class definition. Behind the keyword is the type parameters enclosed with a sharp bracket. It is based on this type of parameters, and the compiler can convert the specific definition of the template class into a definition of the actual class in the compilation process, which generates a new class. The next definition method is very similar to the ordinary class definition, just with a description of the type parameters in the function definition of the class. The following block illustrates the usage of the template class:

Typedef mytemp myclassfromtemp;

MyclassFromTemp M;

INT a = m.myfunc (10);

Usually, when using the template class, use a keyword "typedef" to take a name for the newly defined class. In the above block, "MyClass" is assumed by the user-defined class, by passing the name of this class as the type parameter to the template class, we can create a new class, this class's behavior will be defined by template class Based, for example, it has all member functions defined by template class, and this class is another modification of template behavior, which is implemented by the type parameters provided by the user. The template class is given by different types of parameters, and a set of classifications of behavioral frames is similar but specific behavior. With new classes, we can create a class instance, namely a new object, and call the member functions of this object.

The template class is the latest extension of the standard C language. Although its function is very powerful, you need to use the template class to require considerable language and programming experience and knowledge, and incorrectly use the template class and the structure of the program. And the operational efficiency brings great side effects, so a general programming environment and programming books take a cautious attitude towards the use of template classes. The core of ATL is made up of dozens of template classes. By studying the source code of ATL, we can make our use of template classes a more comprehensive understanding.

Multi-inheritance technology is the same as template, which is a very controversial technology in the C language. Using multi-inheritance technology can make the program's design and achieve more flexible, but due to multi-inheritance complexity and some problems on their own concept, it is very limited in the support of various object-oriented locale. For example, Small Talk does not allow multiple inheritance, and the same MFC does not support multi-inheritance technology.

Most inheritance is the so-called "diamond structure". For example, the following code:

Class A

{

.....

}

Class B: Public A

{

..

}

Class C: Public a

{

.....

}

Class D: Public C, B

{

........

}

Since class D is inherited from class C and B, ambiguity will occur in the following statement:

D * pd = new d;

(A *) PD-> func (...);

Since class D inherits class A by class C and class B, the forced conversion here will be ambiguous.

ATL uses two operational symbols added in C 's latest specifications STICAST, DYNAMIC_CAST instead of simple forced transformation, eliminating the ambiguity of multi-inheritance. With these two arithmetic symbols, we can get the type information of the object during the object operation. The above code can be modified in the following manner:

D * pd = new d;

Static_cast (static_cast (pd)) -> func (...);

Why is the template class and multi-inheritance technology become the main tools of ATL? The reason is that the template can quickly generate a class with a user-customized function during the compilation process, which is greatly improved for COM's complex technical system. By using template classes, users can focus on the basic logic of their own class, after completing their own class, by inheriting different classes, generating different template classes, can quickly implement COM functionality At the same time avoid a large number of functional redundancy caused by a single inheritance structure. In summary, it is because the template class and multi-inheritance technology is used in the design implementation, and ATL has become a small and flexible COM development tool, which can adapt to the various needs of developers to develop developers.

3. ATL basics

This section will focus on the basic use of the ATL. Since ATL has been integrated in the Visual C development environment of Microsoft Visulal Studio, you must install Visual C first. Please refer to the relevant documentation in the discussion below, please refer to the relevant documentation, which is no longer detailed. The given figure is a schematic diagram of the use of Visual Studio 6.0 under the Microsoft Windows 98 platform.

Developing a COM application using ATL can basically be divided into the following steps:

1. Create a new ATL project and make the appropriate configuration for the project.

2. Add a new ATL class to the newly created project and work some initial configuration for this class.

3. Add a new interface definition to the new ATL class according to the basic requirements of the COM application and implement the corresponding interface member functions.

4. Compile the connection engineering, register your COM application.

The basic use of the ATL will be described in order according to these steps.

Create project

First start the Visual C integration development environment, select the "New ..." command under the "File" menu, select the "Project" page in the "New" dialog, as shown in Figure 1.

Figure 1 Create a new engineering interface schematic

Select the "ATL COM AppWizard" item, which is an AppWizard to the import port for the ATL project. Then enter the name of the project in the "Project Name" edit box, click the "OK" button to enter the AppWizard dialog. as shown in picture 2.

Figure 2 ATL COM AppWizard dialog

The primary setting options in the AppWizard dialog are:

l COM Server Type: - Dynamic Linking Library finally generates a Dynamic Library (DLL) form of COM service; - Application (Executable Application) finally generates a COM of executable program type (Exe) Server; - NT Service: Generates a COM service running in NT service.

l Allow embedding proxy / stub code. After the MIDL compiled IDL file provided by Microsoft, the code for the proxy / stub of object scheduling is produced. Traditionally, this part of the code is separated from the COM service program, but since the new COM standard supports the COM object service in multi-threaded environment, there is also an Proxy / Stub support in the COM service program of the dynamic connection library. . In order to support transmission on the network, ATL allows the user to select the code of the Proxy / Stub included in the generated DLL code. This option is not optional under the COM application conditions of the EX and NT service types.

l Allows MFC. Since the ATL supports the basic Windows programming in addition to COM is extremely limited, many programmers are very familiar with the MFC, so the ATL project is allowed to support the use of MFC in the ATL project, which can use the MFC defined class. . This is very convenient for one hand, especially for developers who are accustomed to using MFC, can use the powerful classes of the MFCs to provide support, without having to use Windows SDK directly. From another aspect, the use of MFC in the ATL project will lose the characteristics of the ATL code light level. l Supports MTS. MTS is an abbreviation of Microsoft Transaction Server, which is a new branch in Microsoft in COM technology, which is not described herein.

After completing the above settings, you can choose Finish to complete the project settings, and ATL will create the corresponding project.

2. Join the ATL class

After completing the creation and setting of the project, the next step is to add a new ATL class to the project. The Visual Studio integration environment provides a wizard tool "ATL Object Wizard" for joining a new ATL class. The operation process is not complicated, just a set of dialog operations.

First enter the "New ATL Object ..." command under the "Insert" menu of the integrated environment, as shown in Figure 3.

Figure 3 ATL Object Wizard Dialog Schematic

This dialog box is the beginning interface for creating an ATL object. The left part of the dialog describes the basic type of the object to be created, and there are some types of types here:

l Object (Object) Basic COM object type;

l Control ActiveX Control type ATL object;

l Other (Miscellaneous) auxiliary functions, such as the generation of dialogs, etc .;

l Data Access (Data Access) Data Access, supports MTS, etc.

The right part illustrates the details of each type, for a general COM service program, using the simple object in the object table (Simple Object).

After selecting the basic type of the object to be created, click the "Next>" button to enter the next step, enter the Object Properties Settings dialog, as shown in Figure 4 and Figure 5.

Object property settings are divided into two processes: first, the object name identification is set, and then set up the basic properties of the object. The first is the name identification setting of the object, as shown in Figure 4.

Figure 4 Object Name Identifier Settings Dialog Box Schematic

Enter the name of the object to be created in the Object Identification Edit box, the ATL Object Wizard will synchronously set the C ID and COM ID of the object according to the object identified by the user input. The C identifier of the object includes the class name, CPP file name, and head file name. The COM identifies the name of the primary interface of the CoClass segment and implemented objects in the type library, as well as the type name and progID in the system registry.

After the object name identification setting is complete, select the object property page (Attribute) to enter the object setting page, as shown in Figure 5.

Figure 5 Object Property Settings Dialog

Object property settings are the most complex parts of the ATL object creation process, including the following main parts:

l The thread model of the object of the object is a COM object to control the access method when accessible in a multi-threaded environment. By default, the set of suite model APARTMENT is used in the ATL, and the system passes the message. Concurrency control. l Objects The interface of the interface COM object can be a dual interface. The dual interface is different from the common interface (Custom Interface) is inherited from the Automation Basic Interface Idispatch, and the ordinary interface is directly inherited from the iUnknown interface. The default interface model is a dual interface.

l The aggregate model of the object does not allow the implementation of the object to be inherited, but other COM objects can be reused by aggregation. The aggregation model in the ATL object property setting can specify whether the COM object to be created supports the aggregation model. The default option is to support the aggregation of objects.

l Objects Support for Error Handling (Support isupporterrorInfo) Select this option to support error handling during the operation of the object. By default, this option is not selected.

l Object The Support Connection Points connection point is an event mechanism for a COM object. Select this option to make the COM object to be created with the ability to issue an event. By default this option is not selected.

l The free thread Marshaller, the free thread Marshaller, the FTM. The free thread scheduling of the object is an optimization policy that is used in the free threaded model state, in order to simplify the access process of the object. By default this option is not selected.

The detailed description of any of the above options involves some core content of COM technology, and has exceeded the scope of this article, so this article only explains the default options given by ATL, and readers interested in these contents can be referenced The documentation provided by Microsoft.

After completing the above settings, you can press the "OK" button to complete the creation process of the object. The next step is to join the definition of member functions to the generated ATL class, and the implementation process of interface member functions.

3. Add interface definition to implement interface functions

Once the ATL class definition is added, we can open the Class View in the Visual C Integration Environment to check the case where the generated class definition (see Figure 6). We can see a new class has been generated, while also generating a corresponding interface definition. ATL Object Wizard generates class definition .H and .cpp files, there are also IDL files for interface definitions. After these files, we can join the member function for the interface, complete the definition of the class.

Figure 6 Schematic diagram of ClassView of ATL project

First, the corresponding interface is selected in the Class View, and the interface IATLTEST is displayed in Figure 6, click the right mouse button to open the menu, as shown in Figure 7. This pop-up menu defines the operation of adding properties and methods for interfaces. Select the "Add Method ..." item, you can join the method member for the interface; select "Add Property ..." to add new attribute members to the interface.

Figure 7 Interface Edit menu shows

The dialog box for adding properties and methods can refer to Figure 8 and Figure 9. If we want to join a method in an interface, select the "Add Method ..." menu command. Suppose the method is called ABC, the return type of method is the HRESULT type for COM. We can also define a function of non-HRESULT returns type, but this requires manually modifying the IDL file defined by the interface. We define a parameter of the ABC method as a, type is an integer type. After completing the definition of the method, click the "OK" button to add this method to the interface.

The addition of the property is similar. The property adds the dialog request to specify the type, name, and attribute access method of the attribute. There is a "Attributes" button in the Edit dialog box of the attributes and methods, after giving a basic definition of a property or method, click this button to set some of the advanced features of the properties and methods. Figure 8 interface of the interface method

Figure 9 Add to interface properties

Method members After joining, we can check the ATL for our work, as shown in Figure 10. First we see that the ATL joins the definition of the method in the definition of the interface; at the same time, in the corresponding ATL class definition, a corresponding method is also defined; this method is added in the .cpp file, add this method Implement a framework. Thereafter, we only have the code logic of this method in this function framework, the definition and implementation of an interface function is basically completed. In this way, we can complete the definition and implementation of the entire COM object.

Figure 10 Class View after the addition of the interface method

After completing the above steps, we can compile the connection application.

4. Compile the connection application, register the COM service

The compilation connection process for the ATL project includes the following steps:

1. Use the IDL files of the MIDL compile engineering to form the header file defined by the interface and the code used to scheduling (Marshalling);

2. Compile the engineering .CPP file forming a target file;

3. Connect the target file to form an application module;

4. Register your COM service.

About the other parts of the engineering compilation connection Similar to the compilation connection process of the MFC project in Visual C , here you only have to introduce the registration process of the COM service program.

In ATL, the registration of the COM service is the final phase of the engineering compilation connection, which is completed by ATL assist. In manual COM programming, the server's registration is a troublesome work. In ATL, the system completes the registration work by reading the registration script file formed during the establishment of the project. Registering script (RGS) is a registered auxiliary file for the text of ATL. Below is an instance of the registered script file.

HKCR - Indicates the registration item of the COM object in the registry, is the abbreviation of HKEY_CLASS_ROOT

{

Authtest.ActiveXObject.1 = s 'ActiveXObject Class'

{

CLSID = S '{97A5CB10-AF82-11D2-B9BC-00104B04B867}'

} - Object's Progid

Authtest.ActiveXObject = s 'ActiveXObject Class'

{

CLSID = S '{97A5CB10-AF82-11D2-B9BC-00104B04B867}'

} - Objects with version unrelated progid

Noremove CLSID - Object CLSID Registration

{

Forceremove {97A5CB10-AF82-11D2-B9BC-00104B04B867} = s 'ActiveXObject Class'

{

PROGID = s 'authtest.activeXObject.1'

VersionIndependentProgid = s 'authtest.activeXObject'

InprocServer32 = S '% module% - server type, indicating DLL server

{

Val threadingmodel = s 'Both' - thread model, here is Both type}

}

}

}

The RGS file contains the contents of the registration COM service, usually we don't have to modify this RGS file, and we can also manually modify the RGS file to customize the registration process of the module.

IV. An example of applying ATL

The basic process of creating a COM service using ATL is described above. During the introduction, we have actually generated a basic framework for a COM service, just not filling in the actual content. In the following section, we actually develop a very simple COM service program and prepare a customer code to test, so that everyone has a comprehensive overall understanding of the process of developing the COM service using the ATL.

The function of the service procedures we have to develop is very simple. It only implements an interface. This interface name is ISIMPLEINTERFACE. The interface is only one member function called Welcome. The functionality of this function is just a string of "Hello World!".

The steps to create a COM service in accordance with the previous section, we do the following:

1 Open the Visual C integrated development environment;

2 Create a ATL project called SimpleTest;

3 Insert a new object in this project, the name of the object is SimpleInterface;

4 Set the relevant properties of the interface ISIMPLEINTERFACE to make it a double interface;

5 Join the method in the interface ISIMPLEINTERFACE to join the method Welcome;

6 Open the framework of the Welcome method joined by the ATL, you can see the following code segment:

STDMETHODIMP CACTIVEXOBJECT:: GET_TESTPROP (long * pval)

{

AFX_MANAGE_STATE (AFXGETSTATICModuleState ())

// Todo: Add Your Implementation Code Here

Return S_OK;

}

7 Replace the comment part in the program frame to the following code:

:: Messagebox (Null, _T ("Hello World!"), _ T ("Welcome"), MB_OK;

A message box will pop up when the Welcome method is called.

8 Compilation connection project.

After the above steps are completed, we have a simple COM service, and have been registered in the current system.

Below we have to complete a simple COM client. A COM client is simply like a program that uses the COM component object. The basic procedure for the customer program call COM object is:

1 Create an instance of a COM object. This can be done by calling the API function cocreateInstance of the Windows system.

2 Call the function through the interface.

3 call IUNKNOWN :: Release Release COM object instance

Our client programs are a dialog-based simple application written in MFC. The specific process is as follows:

1 Open the Visual C integrated development environment;

2 Create a dialog-based MFC project called SimpleClient;

3 Add a button in the dialog box, name is TEST;

4 Add the following code in the SimpleClient.cpp file:

(1) Add the following code after the CPP file #include "simpleentdlg.h":

#include "d: /simpletest/simpletest_i.h" // Modify the path to the header file as needed

The joined header file is automatically generated during the compilation COM service program, which contains the definition of the interface itself, the definition of the interface IID, and the definition of the CLSID of the COM object. Contains the header file to enable the customer to use the COM service program. (2) Add the following code in the message control function of the button TEST:

HRESULT hr; ISimpleInterface * pIntf = NULL; hr = CoCreateInstance (CLSID_SimpleInterface, NULL, CLSCTX_SERVER, IID_ISimpleInterface, (void **) & pIntf); if (SUCCEEDED (hr)) {pIntf-> Welcome (); pIntf-> Release ( );

}

The above code first creates a COM object through the system API CocreateInstance, get the pointer of the interface, then call the interface member function Welcome, and finally release the COM object instance through the iUnknown :: release () function.

5 Compile the client.

Finally, we can test if the client is running normally. Start the customer program, when you click the Test button, we can see the pop-up message box, which is the function provided by our COM service.


New Post(0)