COM Component Development Practice (V)---from C + + to COM:P Art 2 __c++

Source: Internet
Author: User
COM Component Development Practice (V)---from C + + to COM:P Art 2

one, reusing C + + objects using abstract base classes

In the previous article, COM Component Development Practice (IV)---from C + + to COM:P Art 1, we've encapsulated the C + + object that we're reusing into a DLL, and the Declaration and implementation of the object has been stripped down, But there is a problem: the private members of the object (such as the array variable m_arrtables of the CDB class in our example) are clearly visible to customers, even if the client is not able to access them; If the object changes the size of its data member, all client programs must be recompiled.

In fact, the customer needs only the address of the member function of the object, so using the abstract base class can satisfy the above requirements, the customer will not include the object's private data members, even if the object changes the size of the data member, the client program does not have to recompile.

1. Modify interface file

First, you change the interface to an abstract base class, which is the only code required by the client program. Including the following steps: 1 The functions of CDB and cdbsrvfactory are changed into pure virtual functions. 2 Delete data members. 3 Delete all the member functions of the derivation of the flag. 4 Convert CDB to IDB (interface for DB), Cdbsrvfactory to Idbsrvfactory (representing DB class factory interface) typedef long HRESULT;
#define Def_export __declspec (dllexport)
Class IDB
{
Interfaces
Public:
Interface for data access
Virtual HRESULT Read (short ntable, short nrow, lpwstr lpszdata) = 0;
Virtual HRESULT Write (short ntable, short nrow, lpcwstr lpszdata) = 0;
Interface for database management
Virtual HRESULT Create (short & ntable, lpcwstr lpszname) = 0;
Virtual HRESULT Delete (short ntable) = 0;
Interfase para obtenber informacion sobre la base de datos
Virtual HRESULT getnumtables (short & nnumtables) = 0;
Virtual HRESULT gettablename (short ntable, lpwstr lpszname) = 0;
Virtual HRESULT getnumrows (short ntable, short & nrows) = 0;
Virtual ULONG release () = 0;
};
Class Idbsrvfactory
{
Interface
Public:
Virtual HRESULT createdb (IDB * * ppobject) = 0;
Virtual ULONG release () = 0;
};

HRESULT def_export dllgetclassfactoryobject (idbsrvfactory * * ppobject);

2. Modify Object Program

In the DLL project, we implement the Declaration and implementation of the specific subclass for this abstract interface, let CDB derive from IDB, cdbsrvfactory derive from Idbsrvfactory, and the parameters of the Createdb method of the class factory cdbsrvfactory by cdb** Changed into idb**. #include ". /interface/dbsrv.h "
typedef long HRESULT;
Class Cdb:public IDB
{
Interfaces
Public:
Interface for data access
HRESULT Read (short ntable, short nrow, LPWStr lpszdata);
HRESULT Write (short ntable, short nrow, lpcwstr lpszdata);
Interface for database management
HRESULT Create (Short & ntable, LPCWSTR lpszname);
HRESULT Delete (short ntable);
Interfase para obtenber informacion sobre la base de datos
HRESULT getnumtables (short & Nnumtables);
HRESULT Gettablename (short ntable, LPWStr lpszname);
HRESULT getnumrows (short ntable, Short & nrows);
ULONG release (); Cpptocom:need to free an object in the DLL, since it is allocated here
Implementation
Private:
CPtrArray M_arrtables; Array of pointers to CStringArray (the "database")
CStringArray M_arrnames; Array of table names
Public:
~ CDB ();
};
Class Cdbsrvfactory:public Idbsrvfactory
{
Interface
Public:
HRESULT createdb (IDB * * ppobject);
ULONG release ();
};

The implementation code for these two specific subclasses is omitted from this list, referring to the previous article.

3. Modify client program

Finally, according to the above modification, the client program is also modified accordingly: 1 The data member type of the Cdbdoc class is changed from cdb* to idb*.  IDB * M_PDB; Pointer to database object

2) in Cdbdoc::onnewdocument function, convert cdbsrvfactory* to idbsrvfactory* BOOL cdbdoc::onnewdocument ()
{
...
New Database Object
M_pdb=new CDB;
Idbsrvfactory * pdbfactory = NULL;
Dllgetclassfactoryobject (& Pdbfactory);
Pdbfactory-> createdb (& m_pdb);
Pdbfactory-> release (); Do not need the factory anymore
Initializing data member variables
...
return TRUE;
}

OK, and then recompile the DLL. It can be seen that by using virtual functions and abstract base classes, this is true for the implementation of interface-oriented programming.

Two, preliminary approximation com--using COM libraries to load C + + objects

The above code declares a DLL's entry point Dllgetclassfactoryobject, and the client program gets the corresponding class factory object by calling the function, and then uses the class factory object to create the real object. In this step we try to get the client program not to call the object's entry function directly, but to have the COM library serve us, and to have the object use standard entry functions.

1, modify the interface definition file

First, the declaration of the class ID and the interface ID is added to the interface definition file, which is defined in both the object program and the client program, where only the two external variables are described. The extraction function is then dllgetclassfactoryobject removed because we will then use the standard entry function Dllgetobject, so we do not have to define the entry function ourselves. 2, modify Object program typedef long HRESULT;
#define Def_export __declspec (dllexport)
{30df3430-0266-11cf-baa6-00aa003e0eed}
extern const GUID Clsid_dbsample;
{0x30df3430, 0x266, 0X11CF, {0xba, 0xa6, 0x0, 0xaa, 0x0, 0x3e, 0xe, 0xed}};
{30df3431-0266-11cf-baa6-00aa003e0eed}
extern const GUID Iid_idbsrvfactory;
{0x30df3431, 0x266, 0X11CF, {0xba, 0xa6, 0x0, 0xaa, 0x0, 0x3e, 0xe, 0xed}};
Class IDB
{
Interfaces
Public:
Interface for data access
Virtual HRESULT Read (short ntable, short nrow, lpwstr lpszdata) = 0;
Virtual HRESULT Write (short ntable, short nrow, lpcwstr lpszdata) = 0;
Interface for database management
Virtual HRESULT Create (short & ntable, lpcwstr lpszname) = 0;
Virtual HRESULT Delete (short ntable) = 0;
Interfase para obtenber informacion sobre la base de datos
Virtual HRESULT getnumtables (short & nnumtables) = 0;
Virtual HRESULT gettablename (short ntable, lpwstr lpszname) = 0;
Virtual HRESULT getnumrows (short ntable, short & nrows) = 0;
Virtual

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.