Last time we saw build ADO components to access records in a data source. This time we would build a client which can access the various methods in the component.
The steps to create a COM Client using MFC are as under:
-
Create a dialog-based project using AppWizard (EXE).
-
Add controls to the dialog box as shown in Figure 1.

Figure 1.
In addition to the controls shown in Figure 1, add a list view control at the bottom. This control is filled up with data programmatically whenever the user clicks on the ‘List’ button.
-
Using ClassWizard add three variables m_id, m_name and m_bal of type integer, string and integer respectively for the edit controls in the dialog box.
-
Similarly using ClassWizard add member variables m_list, m_rad, of type CListCtrl and CButton for the list view control and radio button in the dialog box respectively.
-
Similarly using ClassWizard add member variables m_hide, m_commit, and m_search, all of type CButton for the ‘Hide’, ‘Commit’ and ‘Search’ buttons respectively.
-
From the Class view tab add the following member variables to ‘AdoClientDlg.h’.
int m_oper ;
CEdit *name, *acc, *bal ;
-
Import the server’s Type Library into the client. This is done by adding the following statements to the file ‘StdAfx.h’.
# include
# include "atlbase.h"
# import "C:\Program Files\Common Files\System\ado\msado15.dll" no_namespace rename ( "EOF", "adoEOF" )
# import "..\AdoServer\AdoServer.tlb"
using namespace ADOSERVERLib ;
-
Add an interface pointer cust of the type ICustomer * to ‘CAdoClientDlg.h’ from the class view tab.
-
Add the regulation code in the OnInitDialog( ) function as shown below:
BOOL CAdoClientDlg::OnInitDialog( )
{
// AppWizard generated code
// TODO: Add extra initialization here
CoInitialize ( NULL ) ;
CLSID clsid ;
HRESULT hr ;
hr = CLSIDFromProgID ( OLESTR ( "AdoServer.Customer" ), &clsid ) ;
hr = CoCreateInstance ( clsid, NULL, CLSCTX_ALL, __uuidof ( ICustomer ), ( void ** ) &cust ) ;
acc = ( CEdit * ) GetDlgItem ( IDC_EDIT1 ) ;
name = ( CEdit * ) GetDlgItem ( IDC_EDIT2 ) ;
bal = ( CEdit* ) GetDlgItem ( IDC_EDIT3 ) ;
// insert columns in the list view control
m_list.InsertColumn ( 0, "ID", LVCFMT_LEFT, 100 ) ;
m_list.InsertColumn ( 1, "Name", LVCFMT_LEFT, 100 ) ;
m_list.InsertColumn ( 2, "Balance", LVCFMT_LEFT, 100 ) ;
// Don’t show the list view control to begin with
SetWindowPos ( &wndTop, 50,50,400,235,SWP_SHOWWINDOW ) ;
}
-
Add handlers for the six buttons add, delete, update, commit, list and hide and add the code shown below to them.
void CAdoClientDlg::OnAdd( )
{
acc -> EnableWindow ( TRUE ) ;
name -> EnableWindow ( TRUE ) ;
bal -> EnableWindow ( TRUE ) ;
m_commit.EnableWindow ( TRUE ) ;
m_oper = 1 ;
}
void CAdoClientDlg::OnDelete( )
{
acc -> EnableWindow ( TRUE ) ;
m_search.EnableWindow ( TRUE ) ;
m_oper = 3 ;
}
void CAdoClientDlg::OnUpdate( )
{
acc -> EnableWindow ( TRUE ) ;
m_search.EnableWindow ( TRUE ) ;
m_oper = 2 ;
}
void CAdoClientDlg::OnList( )
{
SetWindowPos ( &wndTop, 50, 50, 400, 390,SWP_SHOWWINDOW ) ;
m_hide.ShowWindow ( SW_SHOW ) ;
m_list.DeleteAllItems( ) ;
UpdateData ( TRUE ) ;
CComVariant custaccno ;
CComVariant custname ;
CComVariant custbal ;
CString name,acc,bal ;
int i = 0 ;
_RecordsetPtr recset ;
recset = ( _RecordsetPtr ) cust -> Getrsetbysort ( m_rad1 ) ;
while ( !recset -> adoEOF )
{
custaccno = recset -> GetCollect ( L"id" ) ;
acc.Format ( "%d", custaccno.iVal ) ;
m_list.InsertItem ( i, acc, i ) ;
custname = recset -> GetCollect ( L"Name" ) ;
name = custname.bstrVal ;
m_list.SetItemText ( i, 1, name ) ;
custbal = recset -> GetCollect ( L"Balance" ) ;
bal.Format ( "%d", custbal.iVal ) ;
m_list.SetItemText ( i, 2, bal ) ;
recset -> MoveNext( ) ;
i++ ;
}
recset->Close( ) ;
}
void CAdoClientDlg::OnCommit( )
{
UpdateData ( TRUE ) ;
switch ( m_oper )
{
case 1:
cust -> AddRecord ( m_id, _bstr_t ( m_name ), m_bal ) ;
break ;
case 2:
cust -> UpdateRecord ( m_id, _bstr_t ( m_name ), m_bal ) ;
break ;
case 3:
cust -> DeleteRecord ( m_id ) ;
break ;
}
m_id = 0 ;
m_name = "" ;
m_bal = 0 ;
acc -> EnableWindow ( FALSE ) ;
name -> EnableWindow ( FALSE ) ;
bal -> EnableWindow ( FALSE ) ;
m_commit.EnableWindow ( FALSE ) ;
m_search.EnableWindow ( FALSE ) ;
UpdateData ( FALSE ) ;
}
void CAdoClientDlg::OnSearch( )
{
UpdateData(TRUE) ;
CComVariant custaccno ;
CComVariant custname ;
CComVariant custbal ;
_RecordsetPtr recset ;
recset = ( _RecordsetPtr ) cust -> Getrsetbyid ( m_id ) ;
if ( ! recset -> adoEOF )
{
custaccno = recset -> GetCollect ( L"id" ) ;
m_id = custaccno.iVal ;
custname = recset -> GetCollect ( L"Name" ) ;
m_name = custname.bstrVal ;
custbal = recset -> GetCollect ( L"Balance" ) ;
m_bal = custbal.iVal ;
recset -> Close( ) ;
UpdateData ( FALSE ) ;
name -> EnableWindow ( TRUE ) ;
bal -> EnableWindow ( TRUE ) ;
m_commit.EnableWindow ( TRUE ) ;
}
else
MessageBox ( "Record not found" ) ;
}
void CAdoClientDlg::Onhide( )
{
SetWindowPos ( &wndTop, 50, 50, 400, 230, SWP_SHOWWINDOW ) ;
m_hide.ShowWindow ( SW_HIDE ) ;
}
-
Uninitialize the COM library by the function CoUninitialize( ) at the end of InitInstance( ) function as shown below:
BOOL CAdoClientApp::InitInstance( )
{
// wizard generate code
CoUninitialize( ) ;
return FALSE ;
}
With that we are through with the creation of the client. Now you can compile and execute the client and check out whether it is able to interact with the methods in ADO component that we developed last week.
0 comments:
Post a Comment