Last Updated:

Attributes in C++ | With Examples

Visual C++ .NET supports the use of attributes that enable you to write plain unmanaged code, such as Component Object Model (COM) components, even with less effort than before. In addition, Visual C++ .NET supports new .NET features, such as the Unified Event Model.

Initially, attributes related to the Microsoft Component Object Model (COM) were used in a separate IDL (Interface Definition Language) file to describe information about the type of components. Microsoft Component Object Models (COM). Now these attributes can be used directly in the C++ source code, so there is no need for a separate IDL file. The compiler uses these attributes to generate executable code and type information.

One of the advantages of using C++ attributes to program using the Microsoft Component Object Model (COM) is that you only have to mess with C++ source files, not IDL or Registry Script (IDL) files. This makes projects with components based on the Microsoft Component Object Model (COM) simpler and more intuitive.

The use of attributes significantly changes the C++ language and expands the modularity of programs. Attribute sources, which can be independent dynamic-link libraries (DLLs), implement a mechanism for dynamically extending the capabilities of the C++ compiler. Attributes are designed to increase programmer productivity, especially when developing components based on the Microsoft Component Object Model (COM). The code generated by using attributes is automatically inserted into the destination file. Attributes are used to generate code for the following elements:

  • interfaces and components based on the Microsoft Component Object Model (COM)
  • Microsoft Component Object Model (COM) events (docking points)
  • Unified Event Model events (managed events)
  • ATL Server code
  • OLE user code for databases
  • Code with performance counters
  • module entry points.

Although this chapter is called "Programming in Managed C++" and attributes are used in managed code generation, in this section we will only look at using attributes to generate unmanaged code based on the Microsoft Component Object Model (COM). For information about using attributes when working with managed events, see the Events section of this chapter. Information on using attributes for other purposes can be found in the documentation for the .NET Software Development Toolkit (NET SDK). Developer-defined attributes are discussed in Chapter 8, ".NET Framework Wireframe Classes."

Let's demonstrate the need to use attributes in C++ using the example of the description of the AddEmUp function in the following line of code. Note that within ANSI C++, this feature cannot be fully described. For example, it is not possible to specify which arguments are input and which are output. Typically, this additional information, which is important when generating Microsoft Component Object Model (COM)-based code that uses marshaling, is described by using Interface Description Language (IDL) attributes in a separate IDL file. In this case, the attributes of the Interface Description Language (IDL) are enclosed in square brackets and can be used to describe many features of components based on the Microsoft Component Object Model (COM), including interfaces, soclasses, and type libraries.

// no important marshaling information
HRESULT AddEmUp(int i, int j, int.*psum);

The above C++ function can be described in more detail in the IDL file, as shown below. It uses the in(input), out(output), and retval attributes.

.HRESULT AddEmUp(
[in]int i, [in]int 3, [out,retval]int *psum);

Midi .exe is used to parse an IDL file, create type libraries and source files that use marshaling (for a proxy and stub), for microsoft component object model (COM) interface methods (remote procedure call (RPC) interfaces are also supported).

You can manually add attributes to your source code. However, it is useful to see how attributes are inserted into a project generated by the AtL COM Application Wizard, which is a high-performance tool for creating components based on the Microsoft Component Object Model (COM) model. In Fig. Figure 3.4 shows how to enable the use of attributes in the Template Class Library (ATL) Application Wizard based on the Microsoft Component Object Model (COM) Application Wizard.

Attributes in C++1


Fig. 3.4. ATL Project Wizard (Template Class Library (ATL)) with Attributed check box selected

The following code snippet is taken from the MyATLProject file created by the ATL Project Wizard with the Attributed check box selected. Note that the module attribute is applied to the project as a whole, which automatically generates the DllMainDllRegisterServer, and DllUnregisterServer functions in the project.

// MyATLProject.cpp: Implementation of the export dynamically
// link library (DLL).
#include "stdafx.h"
#include "resource.h"
// Attribute module (module) causes automatic generation
// DllMain, DllRegisterServer and DllUnregisterServer
[ module(dll, uuid = "{50434D6D-AAEA-405C-AC49-B9CA769E5D6D}",
// module
name = "MyATLProject", // name
helpstring = "MyATLProject 1.0 Type Library",
// Type Libraries
resource_name = "IDR_MYATLPROJECT") ];

Figure 3 5 shows how to use the ATL Simple Object Wizard to add a simple class based on the Microsoft Component Object Model (COM) called MyATLClass to your project.

Attributes in C++ 2


Fig. 3.5. ATL Simple Object Wizard (Templated Class Library (ATL))

The ATL Simple Object Wizard will add the following attribute source code to the MyAtlClass.h file instead of to the IDL file. Note that the objectuuid (universally unique identifier), and dual attributes are used to describe interfaces, and the coclassprogid, and version attributes are used to describe classes based on the Microsoft Component Object Model (COM). These attributes are located directly in the C++ source code, so the IDL file is not needed.

// IMyAtlClass
[
object, // object
uuid("lF9401D8-58BF-469D-845B-A2069CBAFC84"),
dual, helpstring("IMyAtlClass Interface"), // dual, Interface
pointer_default(unique) // unique
]
_interface IMyAtlClass: IDispatch // interface
{
};
// CMyAtlClass
[
coclass,
threading("apartment"),
vi_progid("MyATLProject.MyAtlClass"),
progid("MyATLProject.MyAtlClass.1"), // program identifier
version(1.0), // version
uuid("B3321AD5-3ACE-4820-B134-35FD67120A48"),
helpstring("MyAtIClass Class") // Class
{
class ATL_NO_VTA5LE CKyAtlClass: // class
public IMyAtlClass
{
public:
CMyAtlClass()
{
}
DECLARE_PROTECT_FINAL_CONSTRUCT()
HRESULT FinalCor.struct()
{
return S_OK;
}
void FinalReease()
{
}
public:
};

The above code uses the co-lass attribute, but omits some information present in the code generated by previous versions of AT I_. For example, feature maps, interface maps, and registry scripts are missing. The coclass attribute provides all of these seemingly missed opportunities. The skipped code will be automatically added by the compiler when processing the coclass attribute.

In Fig. Figure 3.6 shows the Add Method dialog box used to add the AadEmUp method. Note the incut, and retval attributes.

They cause the following code to be added to the KyAriciass.h file. Once again, we draw your attention to the fact that attributes are also used in the source CPP file, and not in a separate IDL file.

Attributes in C++ 3

Fig. 3.6. The Add Method wizard dialog box allows you to specify parameter attributes

 

To create a simple project from the Simple ATL Project

 

Create an ATL Server Project:

  1. Select The menu item Fiie › New. The New Project dialog box opens.
  2. Select Visual C++ Projects in the Project Types window.
  3. Select ATL Project from the Templates window.
  4. Type MyATLProject in the Name field
  5. In the Location field, specify the folder where the project will be saved.
  6. Click OK to access the ATL Project Wizard.
  7. Select the Application Settings tab.
  8. Make sure that the Attributed check box is selected.
  9. Click finish. Create a simple object based on the Templated Class Library (ATL):
  10. Select The menu item Project › Add Class. The Add Class dialog box opens.
  11. Select ATL Simple Object as the Template.
  12. Click the Open button to access the ATL Simple Object Wizard.
  13. Type MyAtlClass in the Short name field.
  14. Click Finish to create the project.
    Add a method to a simple object based on the Templated Class Library (ATL):
  15. Right-click the IMyAtlClass interface in the Class View window.
  16. Select the menu item Add › Add Method. The Add Method wizard opens.
  17. For the name of the method, type AddEmUp in the Method name field.
  18. Select LONG from the Parameter type list.
  19. For the parameter name, type i in the Parameter name field.
  20. Select the in check box.
  21. Click the Add button.
  22. Select LONG from the Parameter type list.
  23. For the parameter name, type j in the Parameter name field.
  24. Select the in check box.
  25. Click the Add button.
  26. Select LONG from the Parameter type list.
  27. For the parameter name, type psum in the Parameter name field.
  28. Select the out and retval check boxes.
  29. Click the Add button.
  30. Click finish.
// IMyAtlClass
[
object, // object
uuid("lF9401D8-58BF-469D-845B-A2069CBAFC84"),
dual, helpstring("IMyAtlClass Interface"), // double,
// interface
pointer_default(unique) // unique
]
_interface IMyAtlClass: IDispatch // interface
[id(l), helpstring("method AddEmUp")] HRESULT AddEmUp([in]
LONG i, [in] LONG j, [out,retval] LONG* psum);
};

The only thing left to do in this example is to manually implement the AddEmUp method in the MyAtlClass file.cpp and compile the application. The implementation of this method is shown below:

STDMETHODIMP CMyAtlClass::AddEmUp(LONG i, LONG ], LONG* psum)
{
// TODO: Add your implementation code here
// TODO: Add your implementation code here
*psum=i+j; // added manually
return S_OK;
}

After you create an application, you can test it by using a client program based on the Microsoft Component Object Model (COM). We will not consider here how this can be done because the creation of such a program involves the use of the usual programming style in the outdated Visual Basic or Visual C ++. For more information, we recommend that you refer to the Visual Studio documentation or to any of the many excellent books in the field (for example, Understanding and Programming COM+, written by Robert J. Oberg).

Conclusion

 

This chapter describes how to use C++ extensions when creating programs and components for the NET platform. The basic concepts of creating managed code in C++ are illustrated with many examples, such as HelloWorld, ConvertTemp, etc. The String and Array classes, which are actively used in almost all types of NET applications, were discussed. The C++ Manageability Extension keywords have been studied in detail. Delegates, events, and exception handling management are also covered. Finally, we analyzed the use of C++ attributes to create components based on the Miciosott Component Object Model (COM)