COM application technology (BCD articles)

zhaozj2021-02-08  283

COM application development technology

Cai Qian

Key words: COM ActiveX C Builder

1.Com Technical Overview

COM represents Component Object Model (component object model), which is software development technology that Microsoft has vigorously promoted. Application software developed with COM standards has a powerful feature, mainly as follows:

◆ COM is a binary programming specification that can be written by multiple languages.

◆ Used to create an ActiveX control.

◆ Controls other programs via Ole Automation.

◆ Dialogue with objects or programs on other machines, constitute a distributed application.

After Microsoft launches Windows 98 and Windows NT 5.0, the core of the entire operating system is built around COM. We can regard the Windows system as a series of COM interfaces, which can call them. If DirectX is a series of COM interface servers, which can be designed with high performance Windows graphics.

Applications developed with COM technology are theoretically a program of client / server mode. Programmakers can use a series of COM service programs to construct their own applications, which can be embedded in the main program at any time as needed. These servers can be accessed through the distributed system. In the future, the operating system and the entire network may be considered as a set of services provided in the form of COM objects. Part of the programmer is responsible for establishing these services, while another programmer is only responsible for how to call them. Its purpose is to implement software installment.

Developing a COM application is more complicated, usually using the ActiveX Template (ATL) programming. Here we recommend using C Builder to develop COM programs. Object-oriented technology of Inprise (Borland) has been in the world's leading level.

The following programs are preparatory using C Builder 4.0, running in a Chinese Windows98 environment.

2. Establish a COM service

There are three forms in the COM service, the first is to reside on the local machine in the form of a DLL. When the service program is called, it is embedded in the call to the calling program; the second is to reside on the local machine with EXE Form provided, the service program will occupy a separate thread operation when it is called; the third resident is provided in an exe in the remote machine, and the service program is called over the network, it runs on the remote machine, and the result is returned through the network Caller.

In this first form, the first form is used, which is also the most common form. DirectX is provided in this form.

The way C Builder to establish a COM service program is as follows:

2.1 Create a dynamic connection library file that supports the COM interface object:

◆ Open the File / New / ActiveX project page, select ActiveX Library;

◆ Select Save all to save the item in a PCOMSERVER file name; at this time, C Builder automatically generates the following files:

PCOMSERVER.BPR: Project documents of the project;

PCOMSERVER.H, PCOMSERVER.CPP: Support for the dynamic connection source file for COM objects, where many functions are used for automatic assembly of COM interface objects, you don't have to edit them;

PCOMSERVER_ATL.H, PCOMSERVER_ATL.CPP: ATL File File for C Builder compiler call, you don't want to edit them.

◆ Open the Project / Options / Linker Property page Unchecked Use Dynamic RTL option, Open the Project / Options / Packages property page Unchecked Builder With Runtime Packages option, these two steps can make the developed COM dynamic connection library does not rely on C Builder VCL Dynamic connection library, which is conducive to independent release, but in general, these two items are recommended. 2.2 Creating a COM interface object

Open the file / new / activeX property page, select Automation Object to insert an automatic type COM object to the service program, and we choose this type of COM object to be automatically registered, and automatic support can be called by other languages. The following dialog box appears, enter the name mycom of the COM class, and other options in the dialog box are used to specify the nature of the COM object to view the help information.

2.3 Editing the properties and methods of the corresponding interface objects in the COM object through the Type Library Editor

Automatically enter the Type Library Editor, the type library is used to store the COM object, which is a header package that can be called by multiple languages. In the Type library, you can define the interface of the COM object, define the properties and methods of the interface object. The Type Library Editor is as follows:

It can be seen that an interface class Imycom that automatically generates the MyCOM class at this time is actually dealing with the interface object in COM applications. The following defines methods and properties to IMYCOM interfaces through the Type Library Editor.

◆ Click the Method button at the top of the editor;

◆ Enter the name of the method in the Name field of the Arributes page, this example is AddINT for integer addition;

◆ In the Parameters page, click the parameters in the Add button to edit the method;

X and Y are the two integers input, and RET is used to return the result of the calculation, and must be defined as a pointer type.

◆ Switch to the FLAGS page, you can adjust the properties of the interface;

◆ You can check the generated IDL code in the Text page:

[ID (0x00000001)]]]

HRESULT _STDCALL ADDINT ([In] int X, [in] int y, [out, return] int * ret);

◆ Click the Refresh button, at which point the type library editor can be turned off. When you need to add new properties and methods to interfaces, you can reopen the editor via View / Type Library. Select Save all the default file names provided by C Builder Save the related files as follows:

PCOMSERVER.TLB: Type library file;

PCOMSERVER_TLB.CPP: Include the COM interface and object, its main purpose is to access, in the client program, you need to contain this file to the client's project;

PCOMSERVER_TLB.H: The header file of PCOMSERVER_TLB.CPP is introduced into the client by #include.

MyCompl.cpp: This file is where we need to write program code, implement the method and attributes of interface objects defined by the type library;

MyCompl.h: header file of mycompl.cpp.

2.4 Implementing the method in a COM interface

Open myComPL.cpp file will find the method we defined in the Type Library Editor to write code for this method as follows:

Stdmethodimp Tmycomme :: Addint (int X, int y, int * ret)

{

* RET = x y;

Return S_OK;

}

2.5 Generate a DLL file and register a COM object

◆ Select Project / Builder PCOMSERVER to generate a PCOMSERVER.DLL file.

◆ Open the Type Library Editor, click the Register button to complete the registration of the COM object. Run the regedit program through the Run menu in the Windows Task Bar, find the pcomserver.mycom sub-key, PCOMSERVER is the name of the DLL file in the HKEY_CLASS_ROOT button, and mycom is the name of the COM object, and you can see the COM object below. The global unique descriptor CLSID is as follows:

{59834f03-49f1-11d3-b85b-00e09804a418}

Note: Different machine generated descriptors are different.

Find the CLSID subkey under the hkey_classes_root key, find {59834f03-49f1-11d3-b85b-00 (009804a418} subkey below it, below with the following entry:

InprocServer32: Store the path directory of PCOMSERVER.DLL;

PROGID: Storage CoM object: pcomserver.mycom;

TYPELIB: CLSID value of the COM object {59834F03-49F1-11D3-B85B-00E09804A418}.

The COM object is the automatic connection of the DLL and the customer program by implementing the record in the registry.

3. Establish a COM client

The client will access the MyCom object in the PCOMSERVER.DLL service, and the instructions of these objects are saved in the TLB files described earlier. We can directly add PCOMSERVER_TLB.CPP to the client's project file and reference the pcomserver_tlb.h file in the client program; you can also refer to the PCOMSERVER_TLB.TLB file by Project / Import Type Library, regenerate .cpp and .h files, Automatically complete the above process.

The programming focus of the client program is to implement the call to the method of the COM object in the service program. There are many ways to call, all through the so-called proxy interface, these proxy interfaces have detailed definitions in pcomserver_tlb.h, from These proxy interfaces can be seen in these definitions.

PCOMSERVER_TLB.H file is important, including various interface information of calling myCom object, the main contents of this file are as follows:

// Type Lib: D: /cai/com/pcomserver.tlb

// IID / LCID: {5BD378E5-4B57-11D3-B85B-00E09804A418} / 0

// HelpFile:

// DEPNDLST:

// (1) v2.0 stdole, (c: /windows/system/stdole2.tlb)

// (2) V4.0 STDVCL, (C: /Windows/system/stdvcl40.dll)

// ******************************************************** ***********************

#ifndef __PCOMSERVER_TLB_H____

#define __PCOMSERVER_TLB_H__

#pragma option push -b -w-inl

#include

#if! defined (__ utilcls_h_version) || (__utilcls_h_version <0x0101)

#Error "this file requires an newer version of the header file utilcls.h"

#ENDIF

#include

#include

#if Defined (use_atlvcl) || defined (use_atl)

#if! defined (__ TLB_NO_EVENT_WRAPPERS) #include

#ENDIF

#ENDIF

Namespace STDVCL {class istrings; class istringsdisp;}

Using Namespace STDVCL;

Namespace PCOMSERVER_TLB

{

Define_guid (libid_pcomserver, 0x5bd378e5, 0x4b57, 0x11d3, 0xb8, 0x5b, 0x00, 0xe0, 0x98, 0x04, 0xa4, 0x18);

Define_guid (IID_IMYCOM, 0X5BD378E6, 0x4B57, 0x11D3, 0xB8, 0x5B, 0x00, 0xE0, 0x98, 0x04, 0xA4, 0x18);

Define_guid (CLSID_MYCOM, 0X5BD378E8, 0x4B57, 0x11D3, 0xB8, 0x5B, 0x00, 0xE0, 0x98, 0x04, 0xA4, 0x18);

Interface Declspec_UUID ("{5BD378E6-4B57-11D3-B85B-00E09804A418}") IMYCOM;

Typedef iMycom mycom;

#define libid_of_mycom (& libid_pcomserver)

Interface Imycom: Public IDISPATCH

{

PUBLIC:

Virtual HRESULT stdmethodCallType AddINT (int x / * [in] * /, int * RET / * [out, retval] * /) = 0; // [1]

#if! defined (__ TLB_NO_ITERFACE_WRAPPERS)

INT __FASTCALL AddINT (int X / * [in] * /, int y / * [in] * /)

{

int R;

OLECHECK (this-> addint (x, y, & return);

Return Ret;

}

#ENDIF / / __TLB_NO_INTERFACE_WRAPPERS

}

#if! defined (__ TLB_NO_ITERFACE_WRAPPERS)

Template

Class TcomimyCOMT: PUBLIC TCOMINTERFACE , Public TcominterfaceBase

{

PUBLIC:

Tcomimycomt () {}

TcomimyCOMT (iMycom * intf, bool addref = false): tcominterface (intf, addref) {}

TcomimyCOMT (Const TcomimyCOMT & SRC): Tcominterface (src) {}

TcomimyCOMT & OPERATOR = (Const TcomimyCOMT & SRC) {Bind (src, true); return * this;}

HRESULT __FASTCALL ADDINT (int X / * [in] * /, int y / * [in] * /, int * RET / * [OUT, RETVAL] * /);

INT __FASTCALL AddINT (int X / * [in] * /, int y / * [in] * /);

}

TypedEf TcomimyComt TCOMIMYCOM;

Template Class ImycomDispt: Public Tautodriver

{

PUBLIC:

IMycomDispt () {}

IMycomDispt (Imycom * PINTF)

{

Tautodriver :: Bind (PINTF);

}

ImycomDispt & Operator = (iMycom * PINTF)

{

Tautodriver :: Bind (PINTF);

RETURN * THIS;

}

HRESULT BINDDEFAULT (/ * binds to new instance of coclass mycom * /)

{

Return Olecheck (Bind (CLSID_MYCOM));

}

HRESULT BINDRUNNING (/ * binds to a running instance of coclass mycom * /)

{

Return BindtoActive (CLSID_MYCOM);

}

HRESULT __FASTCALL ADDINT (int X / * [in] * /, int y / * [in] * /, int * RET / * [OUT, RETVAL] * /);

INT __FASTCALL AddINT (int X / * [in] * /, int y / * [in] * /);

}

TypedEf ImyComDispt iMycomDisp;

Template HRESULT __FASTCALL

TcomimyCOMT :: Addint (int x / * [in] * /, int y / * [in] * /, int * RET / * [out, retval] * /

{

Return (* this) -> Addint (x, y, return);

}

Template INT __FASTCALL

TcomimyCOMT :: Addint (int x / * [in] * /, int y / * [in] * /)

{

int R;

OLECHECK (this-> addint (x, y, & return);

Return Ret;

}

Template HRESULT __FASTCALL

IMycomDispt :: addint (int x / * [in] * /, int y / * [in] * /, int * Ret / * [out, return] * /

{

Static _TDispid _dispid (* this, Oletext ("Addint"), Dispid (1));

Tautoargs <2 >_Args;

_Args [1] = x / * [vt_int: 0] * /;

_Args [2] = y / * [vt_int: 0] * /;

Return OutretValSetterptr (RET / * [VT_INT: 1] * /, _Args, OLEFUNCTION (_Dispid, _args));

}

Template INT __FASTCALL

IMycomDispt :: addint (int x / * [in] * /, int y / * [in] * /)

{

int R;

This-> Addint (x, y, & return);

Return Ret;

}

Typedef TcoclassCreatort Comycom;

#ENDIF / / __TLB_NO_INTERFACE_WRAPPERS}; // Namespace PCOMSERVER_TLB

#if! defined (no_implicit_namespace_use)

Using Namespace PCOMSERVER_TLB;

#ENDIF

#pragma Option POP

#ENDIF / / __PCOMSERVER_TLB_H__

Below is the main object described in the file and its definition:

Interface Imycom: Public IDISPATCH

Class TcomimyComt: Public Tcominterface

Class ImycomDispt: Public Tautodriver

Class Comycom: Public CoclassCreator

◆ iMycom: Call the object by the IDispatch interface, which allows the object to be called in a language that does not support virtual function tables (such as Visual Basic). This is a very slow and very benzene interface call mode.

◆ TComiMyCOMT: The method of calling the object by a so-called intelligent interface, can implement IDispatch calls, or use vTable to call, to achieve the fastest call speed.

◆ ImycomDispt: The method of calling the object via the DISP interface can improve the access speed of the IDispatch interface, but it is not more than the VTABLE interface.

◆ Comycom: An instance of the TcomimYCom agent can be automatically generated by using CoClassCreator.

Here is the client program that implements the intelligent interface and the DISP interface call. This customer program is simple, there are two buttons to complete the two interface calls, one edit box displays the result.

◆ The VTABLE call method of the smart interface is as follows:

INT x = 6, y = 6;

Tcomimycom o;

O = Comycom :: Create (); // Finish by CoClassCreator

O-> addint (x, y, & y); // vTable form call

Edit1-> Text = INTOSTR (Y);

◆ The calling method of the DISP interface is as follows:

INT x = 6, y = 6;

IMycomDisp app;

App.BindDefault (); // Finish initialization via bind

App.Addint (x, y, & y); // DISP form call

Edit1-> Text = INTOSTR (Y);

4. Small knot

The above program example is very simple, but the development process of the COM application is described in detail. COM technology is not a programming language, but a programming specification and method. With COM technology can develop powerful software, which is conducive to distributed application technology, which is conducive to multiplayer development, and can help us understand the Windows system itself. The interface technology of COM is more complicated. To learn more about COM technology, please refer to the "COM Technology insider" of Tsinghua University Press.

C Builder is a good tool for developing COM applications. It implies the details of COM implementation. We only need to deal with it to develop a perfect and powerful COM application. I hope that more people go to the development of COM applications, COM technology is the development direction of software technology, and is an effective way to implement software plug-and-play in software engineering.

转载请注明原文地址:https://www.9cbs.com/read-1567.html

New Post(0)