610Project 5 CREATING A COM COMPONENT USING ATL
Construction
Testing
The following sections describe the tasks performed by the team in each of these stages.
Requirements Analysis
During the requirements analysis phase, the Code-Forge team analyzed the use of a checkout page on Art-Shop’s site and the features that it should include.
After this analysis, the team came up with the following list of requirements: |
|
|
|
Y |
The checkout page will accept the credit card information of the buyer, |
|
|
L |
|
including the credit card number, type, and date of expiry. |
|
|
F |
The information provided by the buyer must be validated. If the infor- |
|
mation is valid, then the order details will be stored in the database and |
|
|
M |
|
|
the order will be processed. Otherwise, the order will be cancelled. |
|
A |
|
The credit card will be validated by a service provider. However, before |
|
actually connectingEto a service provider, the card number has to be vali- |
|
dated to be a valid Visa, Master, or Amex number by using the Luhn |
|
T |
|
|
algorithm.
Design
During this phase, the Code-Forge team determined the functionality to be implemented and the technology to be used. In addition, the team designed the user interface in this phase. The team decided upon the following functionality of the checkout page and the technology to implement the same:
One function of the checkout page is to accept the credit card details. To address this, the team decided to create a credit card validation screen, shown in Figure 19-1, with a simple layout containing text boxes to accept the credit card number and the expiry date and a list box to indicate the type of credit card. The types of credit card accepted are Visa, Master Card, and American Express.
The most important function of the page is to validate the credit card details accepted. To implement the local validation of the credit card, the team decided to create an ATL-based COM component that will be accessible from Web pages. The component will have a CardType prop-
IMPLEMENTING COM USING ATL |
Chapter 19 |
611 |
|
|
|
|
erty that can store values including Visa, Master, or Amex. It will also have a method Validate that will accept a single parameter, the credit card number that is to be validated. This function will encompass the logic used to validate the number. The most widely used algorithm to validate a credit card number is the Luhn algorithm.
Credit card number |
|
4545 4903 3581 3115 |
|
|
|
Visa
Credit card type Master
Amex
FIGURE 19-1 The credit card validation screen
Construction
During this phase, the Code-Forge team will construct the application. The primary tasks that the team needs to cater to are:
Create an ATL-based COM component
Code the Validate method to implement the Luhn algorithm
Testing
The team will test the component by accessing it from a VB application and from an ASP page.
612 Project 5 CREATING A COM COMPONENT USING ATL
Creating the Credit Card Validation
Component
In this section you will build an ATL based COM component that will check to see if the credit card number passed is a valid credit card number or not. Before creating the component, you need to understand how to validate a credit card number using the Luhn algorithm.
Understanding the Luhn Algorithm
To understand the Luhn algorithm, assume that the credit card number provided by the user is 4123456789876543. Here are the steps to validate the number using the algorithm:
1.Starting from the leftmost digit, double all alternative numbers. In the sample number, double 4, 2, 4, 6, 8, 8, 6, and 4. The resultant numbers are 8, 4, 8, 12, 16, 16, 12, and 8.
2.Add all the digits of these doubled numbers: 8+4+8+1+2+1+6+1+6+1+2+8 = 48.
3.Add all the numbers that were not doubled: 1+3+5+7+9+7+5+3 = 40.
4.Add the results of the preceding two steps and divide the sum by 10. If the remainder is 0, then it is a valid Luhn number; otherwise, the number is considered invalid. In this example, you add 48 and 40, which results in 88, which when divided by 10 leaves 8 as a remainder and not 0. Hence, this number is not a valid Luhn number.
Now that you are familiar with the logic of the algorithm, you are ready to learn how to create a component to implement this logic.
Besides the Luhn algorithm, credit card numbers also have some rules associated with the type of the cards. The rules define the starting digits and the total number of digits that a credit card number of a particular type can have. Art-Shop accepts only Visa, Master Card, and American Express (Amex) cards. As a result, we will consider only the rules defined for these types of cards:
A Visa card number always begins with 4 and is 13 or 16 digits long.
A Master Card number always begins with numbers from 51 to 56 and is 16 digits long.
An Amex card always begins with 34 or 37 and is 15 digits long.
IMPLEMENTING COM USING ATL |
Chapter 19 |
|
613 |
|
|
|
|
|
|
Creating the Basic COM Component Using the ATL
Project Wizard
In this section you will create the credit card validation component. This component can be hosted in an ASP page or can be used in applications developed using Visual Basic and Visual C++.
1.Start Visual Studio .NET and create a new project by selecting File option from the menu and in the resulting menus select New and Project. Select ATL Project as the project type, as shown in Figure 19-2.
FIGURE 19-2 The Visual C++ New Project dialog box
2.Name the project LuhnChecker. Click on the OK button. The ATL Project Wizard appears, as shown in Figure 19-3.
3.Click Application Settings to view the Application Settings page, shown in Figure 19-4.
The Attributed check box is checked by default. As a result, the support for attributed programming for COM is added by default. The Server type setting should be Dynamic-link library (DLL).
4.Click on the Finish button.
You have created the shell for an in-process (dynamic link library) COM component.
614 Project 5 CREATING A COM COMPONENT USING ATL
FIGURE 19-3 The ATL Project Wizard
FIGURE 19-4 The Applications Settings page of the ATL Project Wizard
IMPLEMENTING COM USING ATL |
Chapter 19 |
615 |
|
|
|
|
Creating the CoClass and
Adding Functionality
Now that the shell of the component is ready, next you need to add a CoClass class to it. Recall that a CoClass is a class that defines the functionality of a COM object.
1.Right-click on the project name LuhnChecker in the Solution Explorer, as shown in Figure 19-5, select Add, and then select Add Class as shown in Figure 19-6.
NOTE
If the Class View and Solution Explorer views of your project are not visible, select the respective options in the View menu.
ATTRIBUTES
Attributes are one of the most predominant features of Visual C++ .NET. Attributes are an easy way to define common application attributes and the code required to support them with simple definitions. You can add attributes either by checking the appropriate check box in a wizard, as you did while using the ATL Project Wizard, or manually. Attributes have significantly simplified COM programming in Visual C++ .NET, leading to increased productivity for the COM developer. Nearly all C++ constructs, such as classes, data members, and member functions, support attributes. The C++ compiler recognizes attributes, parses them, and validates them. Then the compiler calls an attribute provider, which will replace the attribute with the required code. For example, Atlprov.dll is the attribute provider for all ATL-related attributes. (MSDN has a short example of a COM component being developed using a text editor. This example illustrates the level to which attributes simplify COM development. Try creating the same project with the Attributes option turned off and see the difference!)
FIGURE 19-5 Adding a new CoClass
616 Project 5 CREATING A COM COMPONENT USING ATL
FIGURE 19-6 The Add Class dialog box
2.The Add Class dialog box appears with various types of ATL components. Select ATL Simple Object, as shown in Figure 19-7, and click on Open. This opens the ATL Simple Object Wizard, shown in Figure 19-8.
FIGURE 19-7 The ATL Simple Object Wizard
IMPLEMENTING COM USING ATL |
Chapter 19 |
617 |
|
|
|
|
FIGURE 19-8 The Options page of the ATL Simple Object Wizard
3.Enter the Short name as CardCheck. The files and the classes will be named after the name of the class. Click on the Options link to display the Options page of the wizard, shown in Figure 19-9.
FIGURE 19-9 The Class View of the project
618Project 5 CREATING A COM COMPONENT USING ATL
4.Accept the default settings. As per these settings, the CoClass that you are creating will be a component with a dual interface and will support the Apartment threading model. The Dual Interface option must be selected to make the component accessible from scripting clients. As identified in the requirements analysis phase, this component will be hosted on a Web page, so a dual interface is a must.
5.Click on Finish to create the class.
You have added a CoClass to your project. To check the classes added, in the Solution Explorer, click the Class View to see the display shown in Figure 19-10.
As you can see, the wizard has added a class CCardCheck and an interface ICardCheck to the project. Next, you will add the methods and properties to define the interface:
1.Right-click on the interface name and select Add, as shown in Figure 19-11. This will give you an option of adding either a method or a property to the interface.
This component will have one property, CardType, which indicates the type of the credit card and can store “VISA”, “MASTER”, or “AMEX”.
FIGURE 19-10 The Add menu
IMPLEMENTING COM USING ATL |
Chapter 19 |
619 |
|
|
|
|
FIGURE 19-11 The Add Property Wizard
The component will have a single method, which will return 0 if the number is valid (according to the Luhn algorithm).
2.Select Add Property from the menu. The Add Property Wizard appears, shown in Figure 19-12.
3.Enter the property type as BSTR and property name as CardType and click Finish.
4.Repeat the preceding steps but select Add Method in Step 2 to display the Add Method Wizard, shown in Figure 19-13, and enter the method name as Validate. This function will accept two parameters. One is an input type parameter, bstrCardNo, which will contain the credit card number to be evaluated, and the other is a variable, vRetVal.
5.Select the in check box under Parameter attributes, select the parameter type as BSTR*, and assign the parameter name as bstrCardNo. Similarly, for the second parameter, select the type as Variant *, the attribute as retval, and the name as vRetVal. Click on Add to add the parameter.