GUI2 Homework #5
In this homework, you are to create a COM server. The COM server will be a simple server that opens and examines a single table from an ODBC data source (an Access table which will be provided). You will also need to create a test VC++ program to make sure the COM object is working. Before the end of the assignment, we will also attempt to create an ASP page that uses the same COM object to display information.
Creating the COM Server
Using AppWizard, create a project using the ATL COM Wizard option. Name the project MGSales (do not substitute your initials for mine). The COM Server should be a DLL project, and it should support MFC. When you add the COM object to your server, make sure to select the Insert menu item, and then New ATL Object. Select the Simple Object when adding the COM object.
Your project will need to support database access, using the CRecordset classes. So add the following line to your stdafx.h file:
#include <afxdb.h>
Requirements
A COM object with a program ID of MGSales.Client must be created. This class is really a wrapper class for a CRecordset named CClientSet which is discussed in more detail below. The project must be called MGSales, and the COM object must be named Client (its 'Short Name' when prompted).
The COM class must provide the following methods and properties:
Methods
AddNew(BSTR LastName, BSTR FirstName, double AmtPaid, double AmtOrder );
This method adds a new person to the Clients table. The ClientID must be the highest current ID +1. The Lastname, firstname, AmtPaid and AmtOrder values are gotten from the parameters.
SelectAll();
This method selects everyone.
SelectDeadbeat();
This method selects anyone who has not completely paid for what they have ordered (AmtPaid<AmtOrder).
SelectTop();
This method select anyone who is a top-standing client. This means anyone that has paid completely what they ordered, and has order more than 50.
MoveNext()
This method moves to the next selected person. If No selection is active, then all records are selected, and the first record is retrieved.
NOTE: Except for the AddNew function in the COM object, there is no other means to edit or delete records.
Properties
The following properties are to be implemented:
Name | Type | Description |
LastName | Read only BSTR | Lastname of client |
FirstName | Read only BSTR | Firstname of client |
ID | Read only Integer | Unique ID of client |
AmtSpent | Read only Double | Amount spent by client |
AmtOrdered | Read only Double | Amount order by client |
LastClient | Read only BOOL | True if on the last client, False if not. |
Note: All properties are Read-only.
The CClientSet Class
The class that you should create for your access to the Client table in the MGSales should be named CClientSet. Follow these steps to add it to your COM project:
You should create a data member in the COM object class, named m_Client, of CClientSet type. The following examples all assume that you have done this.
Adding a Record
m_Client.m_strSort.Format( "ClientID DESC" ); m_Client.m_strFilter.Empty(); if( m_Client.IsOpen() ) m_Client.Close(); m_Client.Open(); int NewID; if( m_Client.IsEOF() ) NewID = 1; else NewID = m_Client.m_ClientID+1; m_Client.AddNew(); m_Client.m_ClientID = NewID; m_Client.m_LastName = LastName; m_Client.m_FirstName = FirstName; m_Client.m_AmtPaid = AmtPaid; m_Client.m_AmtOrdered = AmtOrder; m_Client.Update();
Selection Based on a Query
if( m_Client.IsOpen() ) m_Client.Close(); m_Client.m_strFilter = "AmtOrdered > AmtPaid"; m_Client.Open();
Your Test Program
You will need to create a test program. This test program should look similar to the following when you are done:
The program should be a simple Dialog-based application. In the OnInitDialog function, you should call CoInitialize to initialize the COM system.
The All button lists all the clients.
The Top Clients button lists all the top clients, as determined by the COM objects SelectTop method.
The Bottom Clients button lists all the worst clients, as determined by the COM objects SelectDeadbeat method.
The Add Client button takes information from the 4 edit boxes, and passes it to the AddNew method of the COM object.
You will need to add a COM wrapper class to your test program. This is done by going to the Class Wizards Add Class button, and then selecting the From a Type Library menu item. When the Import from a Type Library dialog appears, browse to the folder for your COM project, and use its Type Library file (it should be MGSales.tlb).
It is recommended that once you have created the COM wrapper class (using ClassWizard), that you create an instance of the class as a data member in your dialog class, named m_Client. The following code examples will help you create this test program, and are all based on the assumption that you have created the m_Client data member.
Initializing the m_Client member (an IClient object)
CoInitialize(0); if( !m_Client.CreateDispatch( "MGSales.Client" ) ) { MessageBox("CreateDispatch failed" ); return( FALSE ); }
Listing COM properties to the Listbox
m_ClientList.ResetContent(); // m_ClientList is data member for listbox. CString Tmp; while( !m_Client.GetLastClient() ) { Tmp.Format( "%s, %s, %10.2f, %10.2f", (LPCSTR)m_Client.GetLastName(), (LPCSTR)m_Client.GetFirstName(), m_Client.GetAmtPaid(), m_Client.GetAmtOrder() ); m_ClientList.AddString( Tmp ); m_Client.MoveNext(); }
Adding a new Client with COM object
CString Last, First, Tmp; double Paid, Ordered; GetDlgItemText( IDC_ELAST, Last ); GetDlgItemText( IDC_EFIRST, First ); GetDlgItemText( IDC_EPAID, Tmp ); Paid = atof( Tmp ); GetDlgItemText( IDC_EORDERED, Tmp ); Ordered = atof( Tmp ); m_Client.AddNew( Last, First, Paid, Ordered );
Remember