Component Object Model (COMPONENT OBJECT MODEL, hereinafter referred to as COM) is a specification between the components objects, and all objects that follow the COM interface specification can communicate and interact with each other, even if these objects are different from different vendors, with different manufacturers Language, written and build on different Windows versions or even different machines. Delphi supports the COM interface specification, the Object Pascal language increases the method of the object interface. COM objects created with Delphi can also be operated in the Microsoft Transaction Server environment.
Software Reuse is the goal of the industry, and people have always hoped that they can "assemble" applications like wood, and the component objects act as the role of building blocks. The so-called component object is actually predefined, and can complete a certain functional service or interface. The problem is how these components objects are with the application, how to communicate and interact with other component objects? This requires the development of a specification to work according to the unified standards.
CoM is a binary specification that is not related to the source code. In this way, even COM objects are created by different programming languages, these objects can communicate with each other in different process spaces and different operating system platforms. COM is both specifications and implementations, which provides standard interfaces for accessing COM object core functions as a COM library (OLE32.DLL and OLAUT32.DLL). These API functions are used to create and manage COM objects. COM is still a customer server mode. Customer (usually an application) requests to create a COM object and manipulate the COM object through the interface of the COM object. The server creates and manages the COM object according to the customer's request. The two characters of customers and servers are not absolute.
Component objects are similar to both similar to the general object. A general sense is an example of a data type of data and manipulation data, while component objects use interfaces instead of ways to describe themselves and provide services. The so-called interface, its precise definition is "the function based on the object-based semantic function", which is actually a pure virtual class, truly implementing the interface object) (Interface Object). A COM object can have only one interface, such as Wndows 95/98 shell extensions; there are many interfaces, such as AC coffee EX controls, generally have multiple interfaces, customers can manipulate ActiveX controls from many ways. The interface is the only way to communicate with the server and server. If a component object has multiple interfaces, other interfaces cannot be accessed directly through an interface. However, COM allows customers to call queryInterface () in the COM library to query other interfaces supported by component objects. In this sense, the component object is a bit like an interface object.
After calling QueryInterface (), if the component object just supports the interface to query, queryInterface () will return the pointer to the interface. If the component object does not support this interface, queryInterface () will return an error message.
So, queryinterface () is useful, it can dynamically understand the interfaces supported by the component object. The interface is an embodiment of reunion to object programming ideas, which hides the details of COM objects to achieve service. COM objects can be completely independent of access to its customers, as long as the interface itself remains unchanged. If you need to update the interface, you can redefine a new interface. For customers who use old interface, the code has been maximized. Know GUID, CLSID, IID
In a complex system, a large number of component objects may be filled. Each component object may have a large number of buildings CJ to ensure that these interfaces do not conflict with each other, Microsoft specifies the GUID to identify component objects and interfaces. GUID is an abbreviation for Globally Unique Identifier. It is intended to be the only old manner. GUID can identify the class of component objects, this time the GUID is also called CLSID (Class Identifier's abbreviation). GUID can also identify interfaces of component objects, which is also called IID (Interface Identifier's abbreviation). Quote count
The reference count is a mechanism that makes the component object "intelligence". Its work is this: When the interface object is created for the first time, the initial value of the reference count is 1. When there is a pointer to the interface object when there is a client request, add addRef () to add 1. When a customer does no longer need a service of a component object. It should call Release (). Note that Release () does not really release the interface object because there may be other customers being using the interface; Release () just reduces the reference count. Only when the number of reference juice is justified to zero. The interface object is deleted. The role of the reference count is illustrated below. Suppose the customer A requests the IMALLOC interface to the server, and the server receives the request. First see if the interface object exists. if there is not. Just create an interface object, and the apart addRef () makes the reference count to 1, and transmit the pointer of the interface object to the customer A. Suppose this time the customer B is also joined, and it is also the request to IMALLOC interface. Since the IMALLOC interface object has existed, the server simply returns a pointer, and call addRef () makes the reference count to 2. When the customer A does not need the IMALLOC interface, it calls the release () trying to release this interface. . Obviously, IMALLOC interface object cannot be deleted at this time, because the garde B is still useful. It can be seen that the reference count This mechanism enables the service area to know how to manage your own interface.
Quote The number of juices also brought? An issue is to call AddRef () and Release () cannot appear. Once confusion, it may cause the interface object water to be deleted or deleted out later.
Virtual method table
COM is a binary specification, and any development environment can produce a COM object as long as this specification can produce. COM uses a grammap called a virtual method table to solve the method call. However, the COM interface is still the class of Objetc Pascal -? Some differences: COM interface All methods to express to customers must be declared as pure, the customer is just pointing to the pointer to the virtual method table, the implementation interface is Interface object.
If multiple instances of the same COM object are established, the virtual method table is shared. But the data for each instance is private. In Delphi, use the Abstract to indicate the pure deficiency method. E.g:
TMYPUREVIRTUALCLASS = ClasspublicProcedure Mymethod; Virtual; Abstract; ... END;
IUNKNOWN interface
As TOBJETC is the ancestor of all classes, IUNKNOWN is the ancestors of all interfaces. In this way, customers who get the interface object pointer can always access the core services of the COM object, such as addRef (), Release (), and queryinterface (), which manages the survival of the interface object. AddRef () and Release () are relatively simple. There is no parameters. QueryInterface () is more complicated, it has two parameters: one is the IID parameter, used to specify the interface to query; the other is the OBJ parameter, used to return the nail of the found interface object; if the COM object does not support The OBJ parameter will return NIL. ADDREF () and Release () have an underscore prefix, which is for more eye-catching. In the past, the COM object must maintain the reference count, that is, addRef () and release () must be called to add a reference count 1 or minus 1. Another core service of COM queryInterface () is also indispensable, and customers only call QueryInterface () to apply to another interface pointer. Since the ActiveX framework is adopted, the reference count is automatically maintained by Tcomobject objects, and the application no longer needs to be deal directly with the iUnknown interface.
Here is the concept called Interface Aggregation in the COM model. Object-oriented programming idea allows software reuse by inheritance. There is no inheritance in the COM model, but the multiple interfaces are aggregated through the Interface Aggregation technology to complete a complex functionality.
The In-Process COM server is a DLL that outputs a COM object and is mapped to the customer's process address space. The advantage of the in-Process server is that customers can directly call the interface of the COM object.
To create an In-Process COM server, you must create an ActiveX library as a container for COM objects.
To do this, you can use the "new" command on the "File" menu to top it to the "ActiveX" page. Double-click the "ActiveX NBRW" icon, you will automatically create an AC6VEX library.
A COM server of an in-precess type must lead the following 4 routines:
function DllRegisterServer: HResult; stdcall; function DllUnRegisterServer: HResult; stdcall; function DllGetClaasObject (const CLSID, II: TGUID; var obj): HResult; stdcallfunction DllCanUnloadNow: HResult; stdcall;
The COMSERV unit has implemented these routines. Therefore, as long as they are drawn in the project file. DllRegisterServer () is used to register all COM objects in the server. Each COM object has its own keys under the hkey_classes_root / clsid / {xxxxxxxx-xxxxxxxxxx-xxxxxxxx} of the registry. Among them, (x ...) represents the CLSID of the COM object. For the In-Process type COM server, there is a key called InProcServer32. The default value of this key is the path to the server file on the disk. DllunRegisterServer () is used to undo dllregisterServer (), that is, cancel the COM server and COM object from the registry.
DllgetClassObjetc () is used to get a class plant for a COM object. The CLSID parameter is used to specify the CLSID of the COM object. The HD parameter specifies the interface IID of the class factory to obtain (usually IclassFactory IID). If this function call is successful, the OBJ parameter will return a pointer to the class factory. DllcanunloadNow () is used to determine if the COM server should be uninstalled from memory. As long as there is a COM object in the server being referenced, this function should return s_palse, indicating that the DLL should not be uninstalled. If there is no COM object in the server being referenced, this function should return S_TRUE.
To add a COM object in the server, you can use the "new" command on the "File" menu, turn it to the "ActiveX" page, then double-click the "COM Object" icon, Delphi 5 will start the COM Object Wizard. The COM object here is very simple. If you want to create a specific form of COM object, such as an OleAutomation object or an ActiveX piece. The special wizard provided by Delphi must be used. The specific method is as follows:
1. Enter the class name of the C0M object in the "class name" box without having to head with T.
2. Specify the instance mode of the COM object in the "instancing" box. For the IN? Process type server, you don't have to specify the instance mode.
3. In "Threading Model", select a thread mode in the cabinet, which can be set to the following values:
Single: The entire COM server is single-threaded
Apartment: An instance of each COM object has a separate thread. In this way, any data that needs to be shared (such as global variables) must be protected by thread synchronization objects;
Free: Multiple instances of a COM object can run at the same time, which means that the COM object must protect your instance data to avoid multiple instances:
Both: Supports both AARTMENT and FREE thread mode.
Enter the interface name (optional) that allows the COM object to be implemented in the "ImplementD Interfaces" box. By default, the C0M object created by the wizard only implements the iUnknown interface. If you select the "Include Type Library" checkbox, the wizard will generate a type library.
If the "Mark Interface Oleautormation" check box is selected, the interface will support the OLE Automation. However, the data type in the type library must be a type that is compatible with the OLE Automation. Click the "OK" button and the wizard will create a COM object. If you choose a central Ding "Include Type Library checkbox, the wizard will create? Type library. At the same time, the wizard will generate a unit file of a COM object. Unit of a COM object:
Unit unit2;
Interfaceuseswindows, ActiveX, Classes, Comobj, Project2_TLB, STDVCL; Typetxxh = Class (TTYPEDCOMOBJETC, IXXH) protectd {declare ixxh methods here) end; importation
Uses Comserv;
INITIALIZATION
TTYPEDCOMOBJETCFAACTORY.CREATE (CMULTIINSTANCE, TMAPART); It can be seen that the COM object created with Delphi 5 is very simple, mainly because Object Pascal language introduces the syntax of the object interface and adopting an ActiveX framework. The interface object is a class, but the word Class is the two ancestors of the mountain: the first ancestor must be the TOBJECT derive class, here is TTYPEDCOMOBJETC; the second ancestor is the interface to implement, here is IXXH. The first ancestor can be other already declared interface objects, indicating that the interface object being declared simultaneously supports multiple interfaces. The first subchube of the interface must be a CLSID. In some cases where CLSID constants are required. You can use the interface name to replace CLSID constants. Of course, there are no other members in the IXXH interface. An instance of a COM object is created by a factory factory. Each COM object has a class factory. An example of a factory itself is established in the unit's INITIZATION section. In this way, once the COM server is toned to the memory, an instance of a class factory is created, and an instance of a COM object can be created at any time.
To let Windows find COM servers, the COM server must register registration in Windows registry. This requires a command line program that serves Regsvr32.exe.
If there is no regsvr32.ex, you can create a "Registry Project" file with a text editor, and its extension is .reg. The Registry Project file should follow a certain format. Please refer to the example below:
REGEDIT4 [HKEY_CLASSES_ROOT / CLSID / {0AA1740-310E-11D0-A45E-444553540000}] @ = "MyCOMServer" [HKEY_CLASSES_ROOT / CLSID / {0AA1740-310E-11D0-A45E-444553540000} / InProcServer32] @ = "C: // DELPHI //Comserver/MYCOMSERVER.DLL "
After establishing the registry project file, as long as you double-click this file in the Explorer, Windows will add the information in the Registry Project file to the registry. After registering the COM server, you can open the Windows registry to view the registration of the COM server.