From Interface to COM + (Part Two)

Source: Internet
Author: User

 

 

From Interface to COM + (Part Two)

 

1. What is COM? What is COM used? How to use COM?

1. COM = Component Object Model (Component Object Model). When you use delphi to create a powerful and easy-to-use software module (or complex computing method, when you want to work with people who use C ++ or other language development tools, you need to provide the functions in your module to them for use. In this case, you have two options: either, You encapsulate this module into a DLL, call them, or You encapsulate this module into COM and install it in the Windows operating system for them to call.

 

2. From the perspective of a larger application software system, your module is an important component of a large application software. Therefore, your encapsulated module is called a software component, from the perspective of the module you developed, your module is a small software that can independently complete the specific functions of large application software, the encapsulated module is called component software (which can be assembled into larger application software systems ). the Component Object (CO) Model is called as a facial object. It means that you can install this module on the Windows operating platform to provide external functions.

 

3. How to Create a COM? File-> New-> Other-ActivX-> ActiveX Library. In this way, a DLL is actually created. However, this DLL is special and can be installed in the Windows operating platform, provide external services for all functions.

 

4. Let's talk about the COMServ class COMObject class COMObjectFactory class. These three classes are the core class of the COM Service. The function of the COMServer is to provide services to the object COMObject, when a COMServer serves many COMObject objects, ComObjectFactory (COM Object factory) is used to manage many COMObject objects. a dll must provide external services through the Windows COM mechanism, you must reference COMServer (ComServ is the abbreviation of COMServer)

 

5. Register a com dll and use the Regsvr32 DLL file name in the same directory. Cancel the registration and use the Regsvr32/u DLL file name.

 

6. COM is a mechanism provided by the Windows operating platform for external user-defined Component Object services. Therefore, compile com dll to abide by this mechanism. to help you define a COM Service, Delphi creates a class library Editor (class library = stores the class in the database and uses the functions in the class to retrieve it from the library, such as dynamic link library)

7. How to Create a COM?

One: File-> New-> Other-> ActiveX Library at this time, it will generate a dll dpr File, a ridl (Class Library designer) File, A _ TLB automatically associated with the class library designer. pas file. the DPR file is automatically associated with _ TLB. pas File Association (reference). At the same time, a Dispatch (scheduling) interface of COM is automatically created. All self-built interfaces belong to this general scheduling interface, and only one scheduling interface can be created in one COM, it manages and releases the functions of all interfaces in COM. at this time, only one empty COM is defined. Note: Do not add interfaces and implementations at this time.

Two: to add functions to this COM, we are still creating a COM Object File-> New-> Other-> ActiveX-> COM Object, at this time, the system will automatically set the name of the class for the write and put interface implementation class, remove the class library option, and remove the OLE interface creation automation. after the method is set up, you can add a method to the interface and click "refresh" to automatically implement all the code.

8. How to call a COM?

Component-> ImportComponent-> Import A Type Library select the registered DLL, and DELPHI will automatically generate A class Library based on the com dll, and then view the class Library

Var

Interface variable: Interface

Begin

Interface variable: = CreateComObject (CLASS _ interface implementation CLASS name) as interface

Then, call the method in the interface directly.

9. Let's talk about the structural relationship between the project file (DPR) and _ TLIB. Pas (class library file) and the Implementation Unit. pas file.

The DPR file is a DLL file that references the ComServ unit and indicates that the DLL is a COM service file.

The following four export methods are used to obtain the Class Object and register and deregister the object. The project file must reference the class library file and interface implementation file, the reference of a file in use is an in statement. the Code is as follows:

Library Project1;

Uses

ComServ,

Project1_TLB in 'project1 _ TLB. pa', // unit File Identifier in "file path + file full name"

Unit1 in 'unit1. pa' {Adder: CoClass };

Exports

DllGetClassObject,

DllCanUnloadNow,

DllRegisterServer,

DllUnregisterServer;

{$ R *. RES}

Begin

End.

The Class Library Unit defines the information of the interface and the initial chemical operation of the interface. The interface implementation unit must reference the class library unit to implement the interface defined in the class library unit. the Class Library Unit file does not reference the interface Implementation Unit.

 

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.