Flylib.com

Books Software

 
 
 

COM Programming with Microsoft .NET - page 43

team lib

Summary

This chapter has helped you understand three essential elements of COM+ development: COM, DCOM, and Queued Components and how they relate to the .NET components . In most cases, effectively mixing the managed environment of .NET with the unmanaged environment of COM+ comes down to communication. Learning to marshal data as needed and to interpret what you receive from COM+ is important.

Now that you have a better understanding of how COM+ works from the .NET perspective, youll want to look at your .NET component development plans and determine whether any areas exist that require additional work. For example, you might determine that you need to spend additional time defining the data flow from your managed component to an unmanaged application that calls on COM+ for services. Likewise, your managed application might require additional work to ensure it will interact properly with COM+.

Chapter 6 begins the next section of the book. Youll begin writing applications that put into practice everything youve learned in the book so far. In Chapter 6, youll learn attributed programming techniques. Attributes are an essential element of the .NET framework, and theyve been added to unmanaged areas of Visual C++ as well. The next chapter puts the various attributed programming techniques in focus and helps you create your first COM applications using .NET code.

 

team lib

team lib

Part II: Writing COM Code

Chapter List

Chapter 6: Attributed Programming
Chapter 7: ATL and ATL Server

 

team lib

team lib

Chapter 6: Attributed Programming

Overview

The second part of this book switches away from interoperability issues and focuses instead on creating and using COM components by exploiting the new features of Visual C++ .NET. The Active Template Library (ATL) was introduced in Visual C++ 4, and was designed to let C++ programmers create the smallest, fastest , most efficient COM components possible. The library has matured with successive releases of Visual C++ and now provides a very powerful tool for the C++ COM programmer.

Microsoft has introduced many new features into the latest version of ATL, version 7. Most important, though, Microsoft has introduced a new way to write ATL COM components, using attributed programming . This innovation makes it possible to write ATL COM components directly in C++, and for many components, developers will no longer have to interact with the Interface Definition Language (IDL) or work with the ATL source code. In fact, the compiler generates the IDL and ATL source code at compile time, so you dont need to look at them at all unless you want to understand what is happening.

This chapter provides an introduction to attributed programming, while Chapter 7 looks at the additions to the latest version of ATLin particular, the ATL Server classes, which are designed for writing server-side applications for use with Internet Information Server (IIS).

Important 

Attributed programming is used to create COM components in C++ using the ATL library. That means this chapter has nothing to do with .NET and also assumes knowledge of C++ and ATL programming on the part of the reader. No information in this chapter applies to Visual C# or Visual Basic .NET.

 

team lib

team lib

What Are Attributes?

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