Delphi Object-oriented learning essay seven: COM

Source: Internet
Author: User
Tags getmessage

Bahamut
http://www.cnpack.org
(reproduced please specify the source and remain intact)

In a previous article, we introduced the interface. If you do not have contact with the COM object, you will feel that the interface is really troublesome, there may be: "Rather than directly define a class more convenient" idea.
Indeed, interfaces without COM encapsulation are really troublesome. In my opinion, an interface without COM encapsulation does not seem to have any meaning. So, what is a COM object? What advantages does it have? The next step is to start with a simple introduction to COM objects:

COM is a binary specification that is independent of the language being implemented. This way, even though COM objects are created in different programming languages, they can also communicate with each other, running on different process spaces and different operating system platforms. COM is both a specification and an implementation that provides a standard interface for accessing the core functionality of COM objects and a set of API functions, which are used to create and manage COM objects, in the form of a COM library. COM is still essentially a client server model. The client (typically the application) requests that the COM object be created and manipulated through the COM object's interface. The server creates and manages COM objects based on customer requests. Of course, both the client and server roles are not absolute.

Remember when I first contacted the COM object, my teacher once said to me: "com is not a DLL, although it may be prefixed with a DLL file to render in front of you, but it is definitely not in our traditional sense of the DLL (dynamic link library)."
In my opinion, the in-process COM object should be a special dynamic link library that provides some special services with DLL as the carrier. Of course, there are also out-of-process COM.

Now, we demonstrate how to build a simple COM model using wizards in Delphi.
First: Open Borland Delphi 7.1 (sorry, I usually use this version.) What the? Why is it 7.1? It's 7.0 plus a Update1 patch pack.-_-| |).
Then: Turn off Delphi By default for the application we created, and select File->new->other in the menu, then locate the ActiveX page in the popup window and double-click the ActiveX library icon.
After double-clicking the icon, we can see that Delphi has built an ActiveX library for us, with the following code:

libraryProject1;uses  ComServ;exports  DllGetClassObject,    // 返回类工厂的接口  DllCanUnloadNow,      // 是否可以释放该组件  DllRegisterServer,    // 注册函数  DllUnregisterServer;  // 反注册函数{$R *.RES}beginend.

We will see that in the project, Delphi has helped us to define four output functions (more detailed descriptions of these functions, can be consulted more information), we do not care about them first.
Next, we'll use the menu file->new->other and create the COM object in the ActiveX page, and we'll see a dialog: Where classname is our object name, Instancing is the object creation mode, Threading module is thread mode. We use Newcomserver as the object name, other default. OK after you can see a window titled "Project11.tlb", which we can add a new method to the interface in this window, for example, we add a GetMessage method. Then we open Unit1.pas to see the following code:

unitUnit1;{$WARN SYMBOL_PLATFORM OFF}interfaceuses  Windows, ActiveX, Classes, ComObj, Project1_TLB, StdVcl;    // Project1_TLB 接口所在单元type  TNewComServer = class(TTypedComObject, INewComServer)    // 实现接口的类  protected    functionGetMessage: HResult; stdcall;      // 我们刚刚添加的方法    {Declare INewComServer methods here}  end;implementationusesComServ;functionTNewComServer.GetMessage: HResult;begin  MessageBox(0, \‘测试\‘, \‘提示\‘, $40);  Result:= GetLastError; // 我添加的代码end;initialization  TTypedComObjectFactory.Create(ComServer, TNewComServer, Class_NewComServer, ciMultiInstance, tmApartment);    // 类工厂end.

After that, we compile the 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// 注意,在测试代码中也需要引用project1_tlb单元,由于我们的接口声明在该单元内  NewComObject: INewComObject; // 声明接口begin  NewComObject:= CreateComObject(CLASS_NewComObject) asINewComObject; // 创建COM对象,    //CLASS_NewComObject 的定义可以在Project1_tlb.pas里找到  ifNewComObject <> nilthen    begin      NewComObject.GetMessageInfo; // 调用接口中的方法      NewComObject:= nil; // 释放接口    end  else    ShowMessage(\‘对象创建不成功\‘);end;

Note that before we run this exe, we need to register our previous COM project with the system: Start-and-run->regsvr32.exe \ "... Project1.dll\ ". After seeing the prompt for a successful registration, we can now run the test program we just wrote to test our COM object, and see if we have a dialog box titled "Hint" and "test" after executing the test code.

As we can see, when the COM component is created, it is fairly simple when the exe is called, and when the implementation details of one of our methods change, as long as the method declaration is not changed, we can upgrade the COM components we need to upgrade only when the software is upgraded, without having to change the other places. This can effectively reduce the workload of maintenance.

Of course, this demo is just a process of COM, for more detailed instructions, you can refer to more information.

Friendly tip: ActiveX is a component specification for implementing COM under Windows. Please do not equate ActiveX with COM!

Delphi Object-oriented learning essay seven: 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.