What Are Attributes?

team lib

Attributes provide a way to extend C++ without breaking the structure of the language. They define an add-on mechanism that is used to attach extra data to C++ constructs but which doesnt require adding new keywords to the language or altering the way C++ currently works.

Important 

Attributes are used for several separate tasks in the .NET release of Microsoft development tools: providing metadata for managed types, creating COM objects, creating Web Services and ATL Server code, implementing performance counters, and implementing OLEDB consumers. Although the C++ syntax is the same wherever attributes are used, the tasks are quite different and attributes can work in different ways. For example, COM attributes deal with code generation, whereas metadata attributes provide data that can be used at compile time, run time, or both.

In Visual C++ 6, most COM development in C++ used the ATL library. While ATL produces very small, efficient COM classes, a large proportion of COM objects dont make use of ATL functionality beyond using the wizards to create an ATL skeleton and add methods and properties. The use of ATL for these components is overkill, and the developers of these classes dont need to modifyor even seethe ATL or IDL source code.

The COM- related attributes are designed to simplify the creation of COM components, and its possible to create many components without seeing any ATL or IDL code at all. Even if you are used to using ATL, attributed programming can increase the productivity of component developers. You can still use ATL if you need to, but attributes simplify the process of component creation for the many cases where you dont need anything out of the ordinary.

To put it rather simplistically, attributes let you specify in C++ code the same things you previously had to code in IDL or ATL. COM developers dont need to use a different library or learn IDL to create components, and they can do all their development in plain C++. Heres a simple example. To mark a class as the implementation of a COM coclass, use the coclass attribute on the class definition:

 [coclass] classMyObject { ... }; 

If you also want to specify the CLSID for the resulting coclass rather than have one generated automatically for you, add the uuid attribute:

 [coclass,uuid(12EA4458-7753-11D2-098A-00C04F37BBFF)] classMyObject { ... }; 

When you compile code containing attributes such as coclass and guid , the compiler automatically generates the ATL and IDL code needed to implement the COM component.

You can apply attributes to nearly any C++ construct, such as interfaces, classes, data members , and member functions. As you read the chapter, youll find explanations of all the major attributes available to the COM programmer using Visual C++ .NET.

Using attributes replaces the large amount of IDL and registration code required by a COM component with a few concise annotations to class and interface definitions.

Note 

COM attributes dont change the way COM objects are registered and the way they work, so type libraries and registry entries are still the same as before. Attributes simply provide a simpler and more abstract layer at the C++ code level.

How Do Attributes Work?

Attributes are implemented by attribute providers , which are implemented as separate dynamic-link libraries (DLLs). When the Visual C++ compiler recognizes an attribute, the code is parsed and syntactically verified as correct. The compiler then dynamically calls an attribute-provider DLL, which will insert code or make other modifications at compile time; the compiler processes the generated code as part of building the compiled output. The provider that gets called depends on the type of attributefor example, ATL-related attributes are all implemented by the Atlprov.dll provider. The process is illustrated in Figure 6-1.

click to expand
Figure 6-1: An attribute provider works with the compiler to generate code at compile time.

ATL attributes work by inserting ATL code into the project. Attributes dont alter the contents of the source file, and you can see only the injected code in the debugger. If youd like a copy of the generated code, you can use the /Fx compiler option to generate a file containing the original file with the injected code merged. If you are using Visual Studio .NET, you can find this option on the Output Files pane of the C++ section in the Project Properties dialog. Note that the generated ATL code exists only at compile time, but information about the code needs to be added to debug builds so that its available to the debugger.

Note 

As of .NET 1.1, you cant write your own attribute providers. Two providers are used with C++: clxx.dll, which the compiler uses for basic type generation and marshaling; and atlprov.dll for ATL. In fact, it appears unlikely youll ever be able to write custom providers because they interact with the compiler at a very low level and to write them would require knowing a lot of details about how the compiler works that are unlikely to be made public knowledge.

 
team lib


COM Programming with Microsoft .NET
COM Programming with Microsoft .NET
ISBN: 0735618755
EAN: 2147483647
Year: 2006
Pages: 140

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net