Delphi object-oriented COM [reprinted] Delphi object-oriented Learning Article 7: COM

Source: Internet
Author: User
Delphi object-oriented Learning Article 7: COM
Author: banhamt
Http://www.cnpack.org
(Please indicate the source and keep it complete)

In the previous article, we introduced interfaces. If you have never touched the COM object, you will find the interface really troublesome. You may have the idea of "It is better to define a class directly for convenience.
Indeed, interfaces without com encapsulation are really troublesome. In my opinion, interfaces without com encapsulation seem meaningless. So what is a COM object? What are its advantages? Next we will give a brief introduction to the COM Object:

Com is a binary specification and has nothing to do with the implemented language. In this way, even if the COM object is created by different programming languages and runs in different process spaces and different operating system platforms, these objects can communicate with each other. Com is both a standard and an implementation. It provides standard interfaces and a set of API functions for accessing the core functions of COM objects in the form of a COM library. These API functions are used to create and manage COM objects. In essence, COM is still the client server mode. A 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 COM Objects Based on Customer requests. Of course, the roles of customers and servers are not absolute.

I remember when I first came into contact with the COM Object, my master once said to me: "com is not a DLL, although it may be displayed in front of you with the extension name DLL, but it is definitely not a DLL (Dynamic Link Library) in the traditional sense )".
In fact, in my opinion, the COM object in the process should be a special dynamic link library that provides some special services with DLL as the carrier. Of course, there is also a com outside the process.

Now, we will demonstrate how to use the wizard in Delphi to create a simple com model.
First, open Borland Delphi 7.1 (sorry, I usually use this version. What? Why 7.1? That is, add an update1 patch package in 7.0-_-| ).
Then, disable the application created by default in Delphi, select File-& gtnew-& gtother in the menu, find the ActiveX page in the pop-up window, and double-click the ActiveX library icon.
After double-clicking the icon, we can see that Delphi has helped us establish an ActiveX library. The Code is as follows:

Library project1;

Uses
Comserv;

Exports
Dllgetclassobject, // return the interface of the class factory
Dllcanunloadnow, // whether the component can be released
Dllregisterserver, // registers a function
Dllunregisterserver; // The Unregistered function.

{$ R *. Res}

Begin
End.

We can see that in the project, Delphi has already defined four output functions for US (for more details about these functions, refer to more information.
Next, use the menu file-& gtnew-& gtother and create a COM object on the ActiveX page. Then, we can see a dialog box: classname is our object name, instancing is the object creation mode, and threading module is the thread mode. Newcomserver is used as the object name. After OK, you can see a window titled "project11.tlb". In this window, we can add a new method for the interface. For example, we can add a getmessage method. Open unit1.pas and you will see the following code:

Unit unit1;

{$ Warn symbol_platform off}

Interface

Uses
Windows, ActiveX, classes, comobj, project1_tlb, stdvcl;
// The unit of the project1_tlb Interface

Type
Tnewcomserver = Class (ttypedcomobject, inewcomserver)
// Class that implements the interface
Protected
Function getmessage: hresult; stdcall;
// The method we just added
{Declare inewcomserver methods here}
End;

Implementation

Uses comserv;

Function tnewcomserver. getmessage: hresult;
Begin
MessageBox (0,/'test/',/' prompt/', $40 );
Result: = getlasterror; // the code I added
End;

Initialization
Ttypedcomobjectfactory. Create (comserver, tnewcomserver, class_newcomserver, cimultiinstance, tmapartment );
// Class factory
End.

After that, we will compile this project (CTRL + F9) to generate a project1.dll file. Save and close the project.
Next, we write a piece of code to test this COM project: create a common application project and reference the project1_tlb unit:

VaR // note that the project1_tlb unit must also be referenced in the test code, because our interface declaration is within this unit
Newcomobject: inewcomobject; // declaration Interface
Begin
Newcomobject: = createcomobject (class_newcomobject) as inewcomobject; // create a COM object,
// The definition of class_newcomobject can be found in project1_tlb.pas.

If newcomobject <> nil then
Begin
Newcomobject. getmessageinfo; // call the method in the interface
Newcomobject: = nil; // Release Interface
End
Else
Showmessage (/'object creation failed /');
End;

Note that before running this EXE, We need to register our previous com project to the system: Start-> Run-&gtregsvr32.exe/"... project1.dll /". After we see the message indicating successful registration, we can now run the test program we just compiled to test our COM Object. After we have executed the test code, is a dialog box titled "prompt" and "test" displayed?

We can see that after the COM component is created, it is quite simple to call the EXE, and when the Implementation Details of a method change, as long as the method declaration remains unchanged, when upgrading the software, we can only upgrade the COM components that we need to upgrade without changing other places. This effectively reduces the maintenance workload.

Of course, this demo is only a COM in the process. For more details, refer to more information.

Tip: ActiveX is a component specification for implementing COM in windows. Do not place an equal sign between ActiveX and COM!

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.