|
|
This chapter has helped you understand three essential elements of COM+ development: COM, DCOM, and Queued Components and how they relate to the .NET
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
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
|
|
|
|
|
|
|
|
The second part of this book switches away from interoperability issues and focuses instead on creating and using COM
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. |
|
|
|
|
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
|
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
The COM-
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
Using attributes
| 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. |
Attributes are implemented by
attribute providers
, which are implemented as separate
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
|
|
|