Component technology essential COM theory knowledge

zhaozj2021-02-16  47

Talking about component technology

---- Component technology essential COM theory

Former remember: I have written the actual operation of the components. I have been writing for a long time. I always don't dare to put it up. I am afraid that everyone thinks that it is too pediatric, and some friends also remind me that things are too burnt, not It is very simple and easy to understand to reveal the core questions, and the OCX also suspected this aspect, so did not put it up. Ok, continue our discussion today, I hope everyone has its own opinion.

Maybe you have some questions about this title, is the essence of component technology Is CoM?

According to the application, the components described here are in the Windows platform, then what is the nature of the component under the Windows platform? Ofless, COM! Although DCOM, MTS, COM , and even NOT NET have been hung by the programmer's mouth, what is the essence? Just com. I don't talk about why I want to say this, maybe in the following elaboration, you will naturally know the answer.

When introducing COM (Component Object Model, Component Object Model), we will omit some details, because I have to write a lot of details, I think we have to talk about it for a long time, and then The author does not dare to say that he can fully control COM. But try to make a multi-position in this article and afterwards in this article, and strive to have a clear understanding of COM, and can complete the writing of COM components independently. let's continue.

As shown in the figure above, it is a complete COM component map, in fact, can be extended to any component, and their relationship, as shown in the figure above, is included in the included relationship. Among them, COCLASS is part of the true package interface. We take a step by step according to the above figure, in fact, each CoClass can be a COM object, and each CoClass can implement multiple interface, interface before The article has been introduced in the article, then what is the difference between the COM interface and interface we identified here? To a certain extent, the COM interface and Interface we identified can be considered a concept, just here to make every detail of their details, I hope to see the article of this article. Don't confuse. A friend said is quite well, the so-called OOP is the source of the source level, and the component is from the underlying, binary, the user's operation has been specified, so the component can be obliterated The difference between language! But whether it is a small COM component or a large COM component, it has to comply with the COM specification, and the COM component is in the form of a Win32 Dynamic Library (DLL) or in the form of a dial file (EXE). Each COM component is some binary executables. As a component, you must do the following:

It must exist in the form of a service to other clients, of course, it can also obtain other components of services.

COM components can be dynamically inserted or unloaded

COM components must be dynamic links

COM components must hide (package) their internal implementation details

The COM component must hide the language it implemented

COM components must be released in binary form

COM components must be upgraded without hindering existing users

COM components can be transparently reassigned on the network

COM components declare their existence in a standard way

......

It exists in the form of providing services, and it can be completely separated from the physical machine limit, then how is it recognized by each client? Similar to the interface, each COM object also has a 128-bit GUID to identify, called CLSID (Class Identifier, class identifier, or class ID), and it is also the only worldwide, can be understood in conjunction with the interface GUID. According to the COM component language, we can see that a COM component can contain multiple COM objects, and how does these COM objects contact? Can we access object B through object A? In theory, it should be, or even a COM component contains only a COM object, and the COM object is not related to each other, but can you actually operate? Of course, you will see the relevant instance behind it. Every COM object is a black box, what is its interior? Is the implementation of the interface! This is also the essence of COM by implementing interfaces. So, in COM, the interface is everything. We can say that the COM, which will be separated, will not exist, and COM without the interface is no meaning. For us, the COM component, the object is a set of interfaces, only through the interface and COM, no Users of interface access rights, which COM components are not used by it. This is the embodiment of the package. In the last two articles, we introduced the interface, which will not take too much pen ink. OK, after you have these knowledge about the COM component, we can now discuss the further step-by-step discussion of the COM component, discuss from its most detailed place. (For some of COM, such as: iMarshal, agent, stub DLL and other related knowledge will be specialized in future articles)

COM object

COM object? How do I understand COM objects? What is he?

(At this point, this article is only elaborated in Object Pascal). COM object is the collection of interfaces, but how does the COM object implement, control these interfaces? In fact, we can see a lot of related classes in COM in the ComoBJ unit, here we will take Tcomobject as an example, the following code is taken from Delphi6

Tcomobject = Class (Tobject, IUNKNOWN, ISUPPORTERRORINFO)

Private

FController: Pointer;

Ffactory: TcombjectFactory;

FnoncountedObject: boolean;

FREFCOUNT: Integer;

FServerexceptionHandler: iServerexceptionHandler;

Function GetController: iunknown;

protected

{IUnknown}

Function iunknown.queryinterface = Objquryinterface;

Function IUNKNOWN._ADDREF = Objaddref;

Function iUnknown._release = objrelease;

{IUnknown methods for other interfacs}

Function QueryInterface (Const IID: Tguid; Out Obj): hResult; stdcall;

Function _addref: integer; stdcall;

Function _release: integer; stdcall; {isupporterrorinfo}

Function InterfaceSupportSerrorInfo (Const IID: TIID): HRESULT; stdcall;

public

Constructor crete;

Constructor CreateAggregated (Const Controller: IUNKNOWN);

Constructor CREATEFROMFACTORY (Factory: TcombjectFactory;

Const Controller: IUNKNOWN;

DESTRUCTOR DESTROY; OVERRIDE;

PROCEDURE INTIALIZE; Virtual;

Function objaddref: integer; sirt; stdcall;

Function ObjqueryInterface (Const IID: Tguid; Out Obj): hResult; Virtual; stdcall;

Function Objrelease: Integer; virtual; stdcall;

{$ IFDEF MSWINDOWS}

Function SafeCallexception (ExceptObject: TOBJECT)

Exceptaddr: Pointer: HRESULT; OVERRIDE;

{$ ENDIF}

Property Controller: IUNKNOWN Read GetController;

Property Factory: TcomobjectFactory Read Ffactory;

Property Refcount: Integer Read Free;

Property ServeRexceptionHandler: iServerexceptionHandler

Read FServerexceptionHandler Write FServerexceptionHandler;

END;

TcomObject is inherited from TOBJECT, and implements the default interface IUNKNOWN and ISUPPORTERRORINFO, which is different from TinterfaceObjected between us, which implements the necessary functions of the COM object, such as function IUnknown._addRef = objaddref; Quote from _ADDREF. HRESULT is a special return value, which is the product of iSupp PorterrorInfo, which is used to identify the function of the function call or fail. as follows:

Isupp PorterrorInfo = Interface (IUNKNOWN)

['{DF0B3D60-548F-101B-8E65-08002B2BD119}']

Function InterfaceSupportSerrorInfo (Const IID: TIID): HRESULT; stdcall;

END;

It implements the process:

Function TcomObject.InterfaceSupportSerrorinfo (Const IID: TIID): HRESULT

Begin

IF GetInterfaceEntry (IID) <> nil dam

Result: = S_OK ELSE

Result: = s_false;

END;

Here, another process for processing a processing method for processing a detection call failure: OleCheck performs an explanation, seeing its code as follows:

Procedure Olecheck (Result: HRESULT);

Begin

IF not succeeded (result); end;

It can be obvious, it is specifically used to deal with a function that returns HRESULT, so we can use it in the program ä to call the return of HRESULT.

But what is the COM object? Said so much, it seems that it has not yet entered the body, and it will be given to the COM object. The COM object is the CoClass on the upper side! Why do you want to say this? In fact, COM is a vector that implements a set of interfaces to provide services to users. If the COM object shown by the schematic is to get the service by accessing its å, what is the necessary factor? It is interfaces (which should be included, at least two interfaces), the CoClass of the Implement Interface, and the COM Interface is actually made, he is to be CoClass Implence, and finally we have access to Interface. That is to access the CoClass implementation, here we discuss with OO! A Coclass has multiple interface, and there can be multiple CoClass to Implentation an interface, this is not like polymorphism? Each Interface method is just a definition! It has left a lot of space to let the Implentation Coclass go, each Client has no way to know the exact implementation of Interface's Methods, combined with each Interface of the on-side, can be different CoClass IMPLENTATION, which is a polymorphic Reflected, but also the best description of the package, we should know if each Interface is the same if the IMPLENTATION Coclass is the same, it is not necessary to be multiple CoClass IMplentation. This means that the same interface is different in different CoClass! OK, say this, can you tell the necessary factors for a COM object? Interface / coclass, these two! With these two factors, you can form a COM object. Interface is labeled with GUID, and the COM object is indicated by a Class ID;

Do we know enough? Of course, it is a big distance from the entry into the COM door, please continue to look down.

1: IMPLENTATION INTERFACE.

How to make CoClass to implement Interface? This question is very simple, but is it really simple? You will know when you read it.

CoClass is to implement all Virtual Method, such as: Tmyclass = Class (Object, ImyInterface), this is, but we need to know what we do? Why do you do this? Can you do it in another way? What do you do when Object? Can we use Tcomponent? Or is otherwise? The answer is yes, we can write to TMYCLASS = (TcomboBox, ImyInterface), which has an efficiency issue, saying Tcomobj provides the most basic feature of the COM object implementation, you should know other ways achieve!

2: COM object is EXE? Is it a DLL?

Yes, the COM object / COM component is ultimately Exe or DLL, but if we say that a COM object is that the exe or dll is too reluctant, it can be compared to the necessary functions of the COM component before: COM Components can be dynamically inserted or unload applications, but can an EXE OR DLL can do this? Dynamic insert or unload? No! Do he want to compile, but is the COM object? No, the COM object is published in binary, so he does not need to re-compile! We can say that the COM object actually implements the reuse of the binary code rather than the source code! And his contrast to DLL is more obvious, although the DLL can be language-independent, but do you really or do this? Will you try to call someone to write a DLL written with C with Delphi? First of all, I will not do that, I will try to fail to fail because the call is not matched. The most important thing is how you update them? If you add any virtual functions to it when you update your object, you will not move like a patient who is full of hoses. You may think that adding a new function at the end of the object does not have problems, but it is not true: this will translate all the virtual functions of the object derived from your object. And, because the virtual function needs to use the fixed offset in the virtual function table to call the correct function, you cannot make any changes to the virtual function table - at least not recompile each relevant program (these programs use you Objects or any object derived from your object), you cannot make changes. Obviously, each time you update your object, it is recompiled the world, not a good idea. Final (also the most important), updating the DLL is simply a night, because you are in two difficulties, both options are very appetizing: either update the DLL by overwriting the DLL "place", either renamed New version. Update the DLL in the ground is very bad: even if you keep the interface, some user programs of the DLL will be destroyed. This kind of chance is high! Ok, we should say how to use COM objects, this is what we really If you want, the reason why COM is hope that it can serve us, so how to achieve this idea?

Here I will not spend too much ink to describe, if you are interested, you can refer to the next article.

We know that you can communicate with the COM component in a way in the LPC (Local Procedcdure Call) or RPC (Remote Procedure Call), but in any way, how do they go to access? This has to be said that the stub DLL and the agent DLL, as shown below:

As can be seen from the above figure, in the way in the LPC, use the RPC's way, as an external access mechanism, in fact it is not a CLIENT direct call server! In fact, the client is accessible through the proxy dll of itself, and the Server terminal is not directly supplied to the client in the form of a service, which is communicating through the Stub DLL and Client's DLL, so As soon as the Stub DLL feeds the client's DLL request to the true server, Server provides the interface to the Stub DLL. At this time, the Stub DLL will pass this interface to the proxy dll, then the client can be true. See the server interface, service through the Proxy DLL! But there is not the same in the process, because they are still in one process space, they are communications with information through a vTable, and services within the process can even share the same global variables in the Client! However, whether in the way LPC is also, use RPC's way, but you can't know how Remote Server has those services? I can't imagine a Tcomponents Server, I'm going to register, this is to register if the registration information is registered, and the client accesses its Remote Server, just know that there is a registry. What services, I directly call the services shown in the registration table, and how to implement the details of the Proxy DLL and Stub DLLs on the top of the top! This article is temporarily written here, the next article will explain the Proxy DLL and Stub DLL and how to create Componets Server, how to operate. But it must be clear that you must understand the interface, otherwise you will not be able to do it!

Post: Yes, there is no explanation for why COM is the essential questions of component technology, because there is no time, the subsequent article will give you a satisfactory explanation.

Repost, please indicate: DPROGRAM

Contact: dprogram@163.com

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

New Post(0)