COM Component Development Practice (VI)---from C + + to COM:P Art 3

Source: Internet
Author: User
Tags count

In the previous article, "COM Component Development Practice" (V)---from C + + to COM:P Art 2, we are going to use the COM library to load C + + objects, and in this article we will actually turn the C + + object into a COM object, and in the next article we will add multiple interface support to it.

C + + objects become COM objects

To turn a C + + object into a real COM object, you only need to do the following:

1 Implement the reference count of the interface. Therefore, each COM object requires two functions to manage the reference count:

ULONG AddRef();
  
ULONG Release();

These two functions do not return HRESULT because they cannot fail unless the object is no longer present and they do not need to return a value because they simply subtract the reference count.

2 object allows multiple interfaces to be implemented. If the object is to return different interfaces for different customers, the customer needs to tell the object which interface it needs. In fact, a method has been used before: the IID parameter of the DllGetClassObject function. If an object has more than one interface, the Createdb function of the class factory object (that is, the function that actually creates the object) should add an argument: the interface ID, the IID.

If our DB object has multiple interfaces, one interface is used for building tables, one for reading and writing, and one for obtaining database information. We can have the DB object provide an interface query function,

HRESULT QueryInterface (RIID riid,void** ppobj);

3 class factory objects use the standard IClassFactory interface. 4) Use the _stdcall calling convention.

5 Implement DLL dynamic uninstall. The uninstall of the DLL is owned by COM, and the COM query DLL is still in use, It calls the DLL's lead function DllCanUnloadNow to determine if the DLL can be unloaded. If the client wants COM to uninstall all unused libraries, it calls the CoFreeUnusedLibraries function.

6) Implement automatic registration of objects. Only need DLL to implement two derivation functions DllRegisterServer and DllUnregisterServer

1, modify the interface file

1 The IDB is derived from IUnknown, the release member function declaration is deleted, and the _stdcall is added for all member functions of IDB (because COM objects use standard calling conventions under Win32). 2 Delete class factory idbsrvfactory declaration, because we now want to use the Standard class factory interface IClassFactory, but also delete the Iid_idbsrvfactory declaration. 3 Declare external variable IID_IDB,

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}};
{30df3432-0266-11cf-baa6-00aa003e0eed}
extern const GUID IID_IDB;
{0x30df3432, 0x266, 0X11CF, {0xba, 0xa6, 0x0, 0xaa, 0x0, 0x3e, 0xe, 0xed}};
Class Idb:public IUnknown
{
Interfaces
Public
Interface for data access
Virtual HRESULT _stdcall Read (short ntable, short nrow, lpwstr lpszdata) = 0;
Virtual HRESULT _stdcall Write (short ntable, short nrow, lpcwstr lpszdata) = 0;
Interface for database management
Virtual HRESULT _stdcall Create (short &ntable, lpcwstr lpszname) = 0;
Virtual HRESULT _stdcall Delete (short ntable) = 0;
Interfase para obtenber informacion sobre la base de datos
Virtual HRESULT _stdcall getnumtables (short &nnumtables) = 0;
Virtual HRESULT _stdcall gettablename (short ntable, lpwstr lpszname) = 0;
Virtual HRESULT _stdcall getnumrows (short ntable, short &nrows) = 0;
Virtual ULONG release () = 0;
};

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.