COM implementation Original: Wu Jianming (FoxNT)
Preface COM has become a required thing. Around us, you can say that you are full of com.
- If you are using Windows and write a program below it. However, no matter
How do you use VC, or use Delphi for COM programming, in most cases, programming workers
The IDE has hidden from most of COM, making the programmer at all
To understand COM, only focus on writing the logical code they care. This means that we
Few opportunities, can uncover the mystery of COM, come see what it is below.
. This is not a regret for a Windows programmer. Therefore, this paper is to thrown the various wizard tools provided by the existing IDE, guide the big
Home starts from the most basic place, completely from a blank program, build a COM
The procedure, thus achieving a COM, how to generate it more clearly.
Implemented in the program. This article assumes that you are a Delphi / VC programmer with COM programming experience, and hope to understand
The basic implementation of COM. Limited to space and time, we only discuss COM within the process (DLL
Implementation, and guide everyone to establish a simplest COM program.
What COM is COM? COM has a variety of expressions, which can be within the process, or outside the process;
It can also be called remotely. I remember that there is an organization studying COM abroad, and his theme is called
: COM is love! This is of course a humor of foreigners, he just wants to explain, how COM is?
Important. So what CoM is? I have long, when I first started learning COM, some programmers told me:
COM is not a DLL, although it is usually used as an extension, he is completely with DLL
completely different. So, is this statement correct? Let's take a look, to achieve one
CoM in Cheng, what steps need to pass, then, we can know the answer very clearly.
Case. Complete the COM within a process, usually take the following steps: 1. Create a DLL project and export the following four functions: DllgetClassObject, DllcanunloadNow, DllunRegisterServer;
2. Define a custom interface while you must implement the iUnknown interface. 3. Establish GUID to identify this component and custom interface. 4. Register in the registry to mark this DLL. Everyone saw it. In the first step, I need to build a DLL project, then
It means that COM is a DLL? Here, I can tell you clearly, from
Technically, COM in a process can be considered to be a normal DLL-moving
Link to the library! If you abandon common COM API, such as common use of Delphi: createcomobject () or createoleObject (), then you can use this COM component directly to load normal DLL, ratio
If you say, you can load this DLL directly with the loadlibrary () function, then use
GetProcAddress to call the interface output from this DLL to complete each action.
This is completely feasible. However, I have to tell you, just see a COM just as a DLL, it is a very beautiful view - DLL is just a form of expression. more important
What COM implements a rule. So we can say: L COM is a logic that contains many processing, which meets a certain interface specification (such as IUNKNOWN)
DLL components of the specification). (Note: If there is no specification, I will refer to the process in this article.
The COM) L COM in the form of a DLL implements the iUnknown interface. Therefore, anything else is in compliance with the IUNKNOWN specification.
The DLL component of the IunkNown interface, we can see him as a COM. So what is the IUNKNOWN interface? How to implement an IUNKNOWN interface? we
Look, how to define an iUnknown interface in Delphi: iINterface = interface ['{00000000000046}'] Function Queryinterface (Const IID: Tguid; Out Obj): hResult;
STDCALL; FUNCTION _ADDREF: Integer; stdcall; function _release: integer; stdcall; end; icnknown = interface
Simply see, we can understand it directly: iunknown = interface ['{00000000-0000-0000-C000-000000000046}'] Function Queryinterface (Const IID: Tguid; Out Obj): hResult;
Stdcall; Function _addref: integer; stdcall; function _release: integer; stdcall; end; in Delphi, Interface is a type known by the compiler. If it is in VC
IUNKNOWN will be defined as a structure. If you want to implement one
IUNKNOWN interface, we must use a class to implement it, such as in Delphi,
Now IUNKNOWN interface can be written: tmycomobject = Class (TOBJECT, IUNKNOWN) ... END; Sentenceful readers may ask immediately: This iUnknown interface is implemented by TOBJECT, then
Can you be implemented by other classes? For example, use the Tcomponent class to implement? answer
The case is: complete! ! For example, we have to implement a custom interface iMycom, which can be written: iMycomtest = interface (iUnknown); TMYCOMTEST = Class (Tcomponent, iMycomtet) ...... End; This is complete! Because COM pays only how to implement an interface, as for the program
What class is used to use, COM is no matter. Behind We have an example of a COM, and I am planning to use this iMycomtest interface
Come. So we declare this interface into Example 1 for later use.
The production of COM, if we have completed a COM, and it has been registered in the system. So, one
The client needs to call this COM. What happened in the system? In general, when Delphi uses CreateComObject or CreateoleObject to call COM components: 1. CreateComObject or CreateoleObject action. Let's take a look at what these two functions do:
Function CreateComobject (const classid: tguid): iunknown; begin Olecheck (Channel, NIL, Clsctx_inProc_server
Or clsctx_local_server, icnknown, result);
CreateoleObject is slightly more complex:
function CreateOleObject (const ClassName: string): IDispatch; var ClassID: TCLSID; begin ClassID: = ProgIDToClassID (ClassName); OleCheck (CoCreateInstance (ClassID, nil, CLSCTX_INPROC_SERVER
Or clsctx_local_server, idispatch, result); end; have you seen? CreateoleObject has a PROGIDTOCLASSID function. This means
If we want to call our COM components with CreateoleObject, we will
More steps to write our COM. I will explain later. Now we want
Focus on the CoCreateInstance API function. 2. CocreateInstance API function will call the CogetClassObject API, this adjustment
Using the process we can't see the relevant code, because Microsoft has already packaged him. and
What is the role of the COGETCLASSOBJECT function? It will call LoadLibrary to find
Our designated COM component (DLL), then use GetProcadDress to find components
Entrance function - Remember the four exported functions we have told above? It is right,
The DllgetClassObject function will be called here. The original shape of this function is
Delphi is: Function DllgetClassObject (Const CLSID, IID: Tguid; Var Obj):
HRESULT; wherein the third parameter: OBJ will return to us to the defined interface in COM. However,
Note that this interface is not our custom interface, but returns to us.
Become an interface for iClassFactory, a "class factory" interface. When we get a factory
After the mouth, you can get what we need, the interface we have customized. Look at
Interface declaration of iClassFactory: iClassFactory = Interface (IUNKNOWN) ['{00000001-0000-0000-C000-000000000046}'] Function CreateInstance (const unkouter: IUnknown; const same)
Tiid; out obj): hResult; stdcall; function lockserver (flock: bool): hResult; stdcall; end; Was the CreateInstance method? Yes, its third parameter OBJ
Will return to us to the interface we define, such as our Imycomtest interface (Example 1). This way, we can call our custom interface method. Many steps above look a bit confused. Then let's use a simple process
To depict the steps we have just happened. Don't be intimidated by those steps, in fact they are right
It is often simple. l CreateComObject --à CocreateInstance. CoCreateInstance
Find the registration information of COM in the registry. l CocreateInstance -à CogetherClassObject. The registration information is handed over
CogetherclassObject. At this time, COGETCLASSOBJECT will know the COM component on disk.
s position. l CogetherClassObject -à loadingLibrary. LoadLibrary will look for COM DLL
The entrance, then getProcAddress calls its output function DllgetClassObject L dllgetClassObject's output parameter will return to us to "class factory" interface
IclassFactory. l iClassFactory --à CreateInstance. CREATEINSTANCE method
We implements the class of interfaces. This class will call its own QueryInterface method, view
Has a designated interface is implemented by himself, if implemented, return a custom interface
. l After the call is completed, the COM customer will call the COM's DLL output function DllcanunloadNow.
This component is released if the function returns S_OK.
The actual COM example will be an actual example. Includes how to build a COM Server and one
COM Client. For Com Server, we will implement the following features: L Single thread, single customer support. l Implementing custom interface L can register and reverse registration in the system using REGSVR32. l can be called by Delphi or VC program. We only pay attention to achieving the most basic features. When you know the whole process, you will naturally write
Advanced features, such as multi-threaded support, etc. Below, let's start the COM implementation.
COM Server Program l In Delphi, create a new DLL project. Note that it is a DLL, not ActiveX
Library. And save the project as Mycom. Then establish two unit files sequentially: MyCOMSERVER unit: This unit describes the logical implementation of COM: This unit describes the output function definition of COM. l In myCom Unit, we define the output function of the DLL, the entire code:
Library mycom;
Uses sysutils, classes, comdef, mycomserver in 'mycomserver.pas';
/ / Export four functions here. Exports DllgetClassObject, DllcanunloadNow, DllRegisterServer, DllunregisterServer;
{$ R * .res}
Begin end.
Do it well first, don't consider how they are implemented. I will do more about this later.
Say. Here I will explain the role of these four functions: DllgetClassObject: Returns the class factory interface. DllcanunloadNow: Tell customers
The implementation of the L factory is as long as I said, a class plant must build our custom interface. above
Face, we define custom interfaces and implemented by class TMYCOMSERVER. Then
Now we have to do it, implement the factory, and then build a TMYCOMSERVER interface instance by the class factory. The class factory interface is defined as follows: iClassFactory = Interface (IUNKNOWN) ['{00000001-0000-0000-C000-000000000046}'] Function CreateInstance (const unkouter: iUnknown; const same)
Tguid; out obj): hResult; stdcall; function lockserver (flock: boolean): hResult; stdcall; end; note, iClassFactory is the system pre-defined, in the ActiveX unit, so
You don't need to define yourself again. As long as we implement it is: tclassfactory = Class (Tobject, IclassFactory) protected flock: integer; function queryinterface (Const IID: Tguid; Out Obj): hResult
STDCALL; FUNCTION _ADDREF: INTEGER; stdcall; function _release: integer; stdcall; public constructor create; function createInstance (const unkouter: iUnknown; const iid:
Tguid; out obj): hResult; stdcall; function lockserver (flock: boolean): hResult; stdcall; end; We only follow the CreateInstance method. LockServer is used to call COM in multiple customers
When COM is locked, the COM is destroyed when a customer exits, then other customers call
An error will occur. But we only realize single customers here, so we don't consider this function.
That is empty. Function TclassFactory.createInstance (Const unkouter:
Iinterface; Const IID: Tguid; Out Obj): HRESULT; Begin
// Our custom interface is created here. MC: = TMYCOMSERVER.CREATE; POINTER (OBJ): = Pointer (MC);
Function TclassFactory.lockServer (FLOCK: BOOLEAN): HRESULT; Begin
END;
Similarly, TclassFactory must also implement a reference count because it is also realized.
IUNKNOWN interface. Function TCLASSFAACTORY._ADDREF: Integer; Begin INC (FLOCK);
END;
Function TclassFactory._release: Integer; Begin Dec (FLOCK); if Flock = 0 THEN Free;
Function TclassFactory.QueryInterface (Const IID: Tguid; OUT
Obj): hResult; begin
End; where Queryinterface I set it out, because in this example, you don't need to go to it.
What interface is queried. If the reader needs to query the interface, you can realize the relevant generation.
code. Similarly, in its constructor, it is also pre-counting 1 constructor TclassFactory.create; recomcance; end; so far, we have basically implemented most of a COM requires most of the features. right now,
We need to register it into the system so that they are called by other programs. l COM registration and anti-registration We turn back to see how to implement the four DLL output functions. These four functions
The original shape is as follows: Function DllgetClassObject (Const CLSID, IID: Tguid; Var Obj):
HRESULT; STDCALL; Function DllcanunloadNow: HRESULT; stdcall; function dllregisterServer: hResult; stdcall; function dllunregisterserver: hResult; stdcall;
The instance of the class factory we mentioned above is created in DllgetClassObject.
. The code is as follows: Function DllgetClassObject (Const CLSID, IID: Tguid; Var Obj):
HRESULT; Begin Cf: = TclassFactory.create; Pointer (OBJ): = Pointer (CF); Result: = S_OK; End; Similarly, we only have a class factory, so you can ignore the two parameters in front. no
Then, depending on the GUID to create different types of factory objects. Here, we straight
The plant is returned. The function dllcanunloadNow is used to log out of a COM. In normal use,
Count, to determine if the user is allowed to log out. Here we go directly to S_OK, let users
Directly log out. Function DllcanunloadNow: HRESULT; Begin Result: = S_OK; END; function DLLREGISTERSERVER is used to register a COM component information to the registry. To register
A COM, the user must know how the COM is organized in the registry. Structure
Next: hkey_classes_root ---- CLSID ---- GUID ----- Inprocserver32 indicates the path to the disk where the COM is located and the thread model ----- ProgID indicates the interface implemented by COM ----- Typelib indicates the type of COM The library of Guid ----- Version indicates the version number of the COM. When CreateComObject () is called, enter the Class type of the parameter COM.
The system will search for information in the registry, then you can find the position of the COM.
You can start calling. Note that if you want the COM component to support the call of the client's CreateoleObject () function
Use, you must also register a message: hkey_classes_root ---- Interface declaration ----- CLSID indicates the correspondence of the COM interface and the Class type GUID. So, the system will be based on the input parameters when CreateoleObject call occurs.
A COM interface declaration, such as A.B), look for the Class GUID corresponding to the interface, then
You can read the relevant information of COM. All code is as follows: Function DllregisterServer: hResult; var lp: pchar; ns: dword; begin result: = s_false; reg: = tregistry.create; getMem (LP, 255); tryem (lp, 255); tryems_rootkey: = hkey_classes_root; if reg.openkey ('/Mycom.mycomtest' ,true) THEN BEGIN REG.CREATEKEY (' CLSID '); if Reg.OpenKey (' CLSID ', TRUE) THEN REG.WRITESTRING (' ', guidtotring (class_mycom)); End; if REG .OpenKey ( '/ CLSID /' GUIDToString (Class_MyCOM), true) then begin if Reg.CreateKey ( 'InprocServer32') = false or Reg.CreateKey ( 'ProgID') = false or Reg.CreateKey ( 'TypeLib') = False or reg.createKey ('Version') = false kilit; reg.writeString ('', 'mycom'); if reg.openkey ('/ clsid /' guidtotring (class_mycom) '/ InprocServer32', false Then Begin Windows.getModuleFileName (Hinstance, LP, 255); reg.writeString (', lp); reg.writeString (' ThreadingModel ',' SINGLE '); END; if reg.openkey (' / CLSID / ' GuidTostring (Class_mycom)
'/ Progid', false Then reg.writeString ('', 'mycom.mycomtest'); if reg.openkey ('/ clsid /' guidtostring (class_mycom)
'/ Version', false Then reg.writeString ('', '1.0'); if reg.openkey ('/ CLSID /' GuidTostring (Class_Mycom)
'/ Typelib', false The reb.writeString ('', guidtostring (libid_mycom));
Reg.closekey; result: = s_ok; end; finally begin freemem (lp); reg.free; end; end; end;
The function DllunregisterServer logs up the registration information of a COM to the system. It compares
Simple, related directly to the COM registry keys to delete is: function DllUnRegisterServer: Hresult; begin Result: = S_False; Reg: = TRegistry.Create; try Reg.RootKey: = HKEY_CLASSES_ROOT; Reg.DeleteKey ( '/ CLSID /' GUIDToString (Class_mycom)); reg.closekey; finally reg.free; end; end; l. Now, we compile the program, then generate a DLL file, under the command line, use: regsvr32 mycom.dll Register COM to the system.
The COM Client program calls a new project in Delphi, then defines interface information in unit: iMycomtest = Interface (IUNKNOWN) ['{D1C4A022-7F6F-42F0-A9B0-4A91703EB124}'] Function MSG: integer; stdcall; Define variables: class_wjm: tguid = '{CE38847E-A386-4753-89F1-34BE80042107}'; A: Imycomte; then add the following code in the oncreate event: Procedure TFORM1.FORMCREATE (Sender: TOBJECT); begin // Just use which one can be A: = CreateComobject (class_wjm) as iMycomtest; // or use A: = CreateoleObject ('mycom.mycomtest') AS
IMycomtest; End; then put a button and add code in its event: procedure tform1.button1click (sender: TOBJECT); Begin ShowMessage (INTTOSTR (A.MSG));
Plus: Procedure TFORM1.FORMCLOSE (Sender: TOBJECT; VAR ACTION)
Tclosection; begin A: = NIL; END; Note Be sure to release the interface, otherwise it may be a ulcened AV error. Run our program, click the button, you will see the output information "1978". If you can't see the correct information, please take a closer look at whether your code is consistent with the text. you
You can also ask me source code directly.
Tune slightly complex points in VC6. First translation GUID: // {CE38847E-A386-4753-89F1-34BE80042107}; static const CLSID CLSID_MyCOM = {0xCE38847E, 0xA386,0x4753, {0x89,0xF1,0x34,0xBE, 0x80,0x04,0x21,0x07}} ; // {D1C4A022-7F6F-42F0-A9B0-4A91703EB124} Static const IID_mycom = {0xD1C4A022, 0X7F6F, 0x42F0, {0xA9, 0X70, 0X4A, 0x91, 0X70, 0X3E, 0X91, 0X24}}; then declare an interface Definition: struct iMycomtet: public iunknown {Virtual long __stdcall msg ();}; iMycomtest * plink; then put a button up and write code in related events: void cmyvcview :: onbutton6 () {plink = null; int A = 0; Coinitialize (NULL); A = COCREATEINSTANCE (CLSID_MYCOM, NULL, CLSCTX_INPROC_SERVER, IID_MYCOM, (VOID **) & plink; if (a == s_ok) {long a = plink-> msg ();};} Be sure to remember to call Coinitialize (NULL); this function, otherwise COM cannot be used. Compile operation, you should be able to see A is equal to 1978. Summary to so far, we successfully prepare a simple COM component, and in Delphi
Call successfully in VC. This means that our work is successful. At the same time, we also see that
It is not difficult to achieve a COM. About the COM and DCOM outside the process, the former is based on the LPC local process call, the latter is base
Call in the RPC remote procedure. In addition to the difference in agreement, others are the same. Everyone is interested,
It can be discussed later. About the COM's thread mode, I used to generate the corresponding line in the COM wizard.
Cheng code to achieve multi-threaded. But I will think that there is no such thing at all, COM
Just make a tag, tell the operating system his thread model, as for how to generate threads,
It is done by the operating system. Further research is also required for discussion in this regard. A small tail we know that in Delphi, there is an Import Type library function. can
Guide a COM component to Delphi directly. But if we try to put our just
When I wrote, I also prompt: Delphi will prompt: When loading the type library / DLL error. What is going on? It turns out that this is a small trick in MS / Borland. Let's take a look
The VCL code knows that when the guidance of Delphi creates a COM, it secretly
Added an iProvideClassInfo interface to go in, this interface uses ITYPEINFO
The port is mainly used to provide type information of COM externally. Everyone follows
TTYPEDCOMOBJECT This class will find this mystery. In the precedent, we didn't
There is certainly unable to be loaded by Delphi. How to achieve this
The interface has exceeded the scope of this article, so it is not discussed. Interested friends can continue to pay attention to "COM implementation process (2)", mainly telling how to implement type libraries.
2002/6/27 All rights reprinted, please include the author's name
Sorry, I wrote a wrong, if the other party gives you COM, it is to look at that type, if it is the function imported in the process, if it is outside, one
Run itself register in the system. There is another way to import library Delphi's interface import library Delphi automatically generate VAR TMYTEST: COYOOOOOOURINTERFACE.CREATE; TMYTEST. / / Provide method;
VAR TMYTEST: YOURINTERFACEBEGIN TMYTEST: = CREATECOMOBECT (unique identifier for COM class) TMYTEST.//> Method END
Reprinted (I don't know the source): Chapter 1 Using the interface in Delphi: 1.1 Define Interface: Purpose: What is an interface and an abstract class and different points. Abstract classes (Remarks Understanding Interface The simplest method) can never create an instance of an abstract class; it does not implement functionality, relying on derived class implementation; the interface is declared as an interface type. The interface name begins with letter i. Class type name begins with T. All interfaces are inherited from iUnknown; the interface instance cannot be created; the range indication cannot be specified in the interface. All methods are public, and cannot have a scope instruction in the interface; the variables cannot be declared; the interface can only decide what functions provide, and there is no limit to how to complete the function. All functions and processes declared in the interface are virtual abstract functions and processes. Declaration, you can't bring Virtual; the interface is constant; 1.2 Aiming an interface: How to declare an interface
GWID (Globally Unique Identifier) World Wide Mogue: CocreateGUID Generation (API) 1.3 Implementation Interface Purpose: How to Implement Interface
Realize IUNKNOWN: QueryInterface, _addref, _Release uses TinterfaceObject to move IUnknown, otherwise you want to implement the above method. Create, use, and destroy interface: create; pointing to the interface does not access any information; auto-release, force destruction A interface sets the variable to nil Note: Delphi automatically creates and destroy the interface. Get a pointer to a single interface: Direct assignment: GetInterface (const iid: tguid; out obj) with the interface type that you implemented: Decluders whether the object supports an interface AS operator: object supports a specific interface (the object does not support the interface Wrong, you can intercept errors); AS automatically call counting function; 1.4 Advanced Multi-level interface problem: Multiple interfaces in a class
Implement multiple interfaces in a class TXY = Class (TinterFaceDObject, IXX, IYY): class TXY implements all methods of IXX and IYY interfaces. Multiple interfaces are not multiple inheritance: TXY has only one base class TINTERFAedObject; method distinguishing sentence: When the interface method is implemented in the class, the method resolution clause can use to change his name TxY = Class (TinterFaceDObject, IXX, IYY) Procedure IXX.PXY = PXY1 Procedure Iyy.pxy = PXY2 Interface Authorization: A interface implementation authorization to another class: A class contains a pointer to another class. Internal class: implement one or more interfaces; external classes: Simples pass these methods to internal classes, rather than re-implement interfaces; interface properties: can define read-only, write, or read and write attributes; but all Access must pass through the function, because the interface cannot be defined. 1.5 Small Setting: How to use the interface within the Delphi application to learn about the interface of Delphi language elements. A interface; implement the interface in the class; implement the functions required for IUnknown; the processing of automatic object destructors; implement multiple interfaces in the class; implement the implementation of an interface to an internal object; define and implement interface properties
Chapter 2 Interfaces and COM 2.1 Guids and COM Purpose: CLSID: Class ID is a GUID a specific type of name, registry hkey_classes_root / clsid Each interface CLSID or GUID represents a COM interface implementation COM object: Tcomobject inheritance (TinterFaceDObject) Does not provide the necessary functionality of the COM object) HRESULT: The returns of special types means that the function call success is still failed. OLECHECK: Check the error that the function call may generate; this function should be used when the COM function returns to HRESULT; Class Factory: COM object is not exemplified by the program; COM uses a class factory to create an object; An object, the purpose is to create other objects; each COM has a related class factory, responsible for creating a COM object implemented in the server; class factory separates COM from actually constructing an object, COM has no object constructing process Class Factory Support iClassFactory interface: iClassFactory only defines two functions createInstance and LockServer CreateInstance functions: responsible for creating an instance of COM objects involved in class; LockServer: Keeping the server in memory, do not call him; : 2.2 In-Process CoM Server (In-Process COM Server) Purpose: Understanding the COM server within the process
Commonity: There is a child key of InProcServer32, all processes in all processes output the following four standard functions; DllRegisterServer: 2 ways to automatically call the IDE's Register ActiveX Server menu Windows command line applications Regsvr32.exe (or Boland Apps Tregsvr) Dllunregister: It is the inverse process of DllregisterServer, removes entries in the registration table; DllgetClassObject: is responsible for providing COM a class, this class is created a COM object; each COM server will implement each COM object it output. Class factory; DllcanunloadNow: Returns S_TRUE, CO removes the COM server in memory; if returns S_FALSE Thread Support: Only within the process of internal servers; saved in the registry; thread model Single-Thread Apartment ( STA): In fact, there is no thread support at all, all access to the COM server is executed in Windows, and there is no need to consider the problem of synchronous access. MUTLI-Threaded Apartment (MTA): Allows multiple threads to access COM objects at the same time, must control program code for different threads; Both Apartment: Can be executed in MTS or STA; free registration server (Registering the Server) : All COM servers require a Windows registry to work. Customization Constructor: The base class of the COM object in Delphi includes a series of non-virtual construct functions. Just overload the Initialize method; do not try to overload a constructor of a COM object; create a CREATECOMOBJECT (Const ClassID: tguid): iUnknown; COCREATEINSTANCE Create an instance of the COM object class factory, then use the class Factory to create an object, after Creating the COM object, the class is destroyed; the virtual method table (Virtual method table): The interface is implemented as an independent methodometer, which is implemented in the middle of the VMT; different interfaces Different memory parts, not simply assign an interface value to another interface; usually use the AS operator to convert from an interface to another interface; 2.3 Process COM Server (OUT-OF-Process COM Server) Purpose: Understanding Process COM server
The process external server is implemented in EXE; instanting: Create how many customers needed; can support one of three instantiation methods; Single Instace (single instance): only allows an instance of a COM object to each The application that requires a COM object instance will generate a separate copy of the COM server; Multiple Instance: Refers to multiple copies of a COM object; when the customer requests an instance of the COM object, created by the currently running server An instance of the COM object; Internal Only: Used for COM objects that are not used by the client application; Marshaling Data: A executable program cannot directly access another executable address space; Windows By scheduling process mobile data between calling applications and processes COM servers; Automation compatible with Smallint, Integer, Single, Double, Currency, Tdatetime, WordString, Idispatch, Scode, WordBool, Olevariant, iUnknown, Shortint, Byte; Record and arrays cannot be automatically scheduled; 2.4 Variant population: How to use Variant arrays;
Variant: A variety of data types; you can also tell the current stored data (by using the VARTYPE function); you can assign different data types to the same Variant, as long as Variant contains digital values to perform algorithms; Variant The array is only a Variant type array, does not have to contain the same type of data; Variant array creation method: Function VaRrayCreate (const bounds: array integer; variant; bounds: telling the up and down of array; VARTYPE: Decision What type of data is stored in the array.
For example: Creating an array of arrays, you can imitate any type of data structure type: Vararrayx: = varVarraycreate ([1,10], varvariant); Array's single element can be loaded: VararrayX [1]: = VararrayCreate ([1, 5], Varvariant; Function Vararrayof (Const Values): Variant; Runtime for creating a one-dimensional array; you can create a total value array; for example: MyArray: = variayof (['Li Wei', 30 , '60', 60.369, 'China']); Use a Variant array: similar to the standard Delphi array; VaRraylowBound, VararrayLowBound, VararrayHighBound calculates the boundary; varialdimcount: VararrayDimcount: Calculate the dimension of the array; 2.5 small junction : The basic knowledge of the interface GUID and the process COM server;
What is the guid; and why GUID is so important to COM; how to create a GUID for yourself; the COM server, creation and how to use; Variant array; Windows automatically scheduling type; only the object-oriented language-oriented language that can understand the virtual method (VMT) can access only the unusual method of COM server Chapter IV Chapter IV
3.1 Definition Type Library: Type library is what is, and what is its role.
Benefits using the type library: Early connection (Early Binding); many compilers can automatically generate code for specific programming languages from one type library; utility can be used to read and display related to the type library Information of the COM server; automatic parameter scheduling between COM customers and servers; Type libraries are required for certain COM servers, such as automation servers and ActiveX controls; TTYPEDCOMOBJECT: Delphi provides the type library by this class and its derived class support; Delphi automatically create XXXX_TLB.pas file; COM derived from the TTypedComObject, rather than TComObjectFactory.Create; initially, delphi call TTypedComObjectFactory.create, rather than TComObjectFactory.Create; 3.2 use delphi to create a type library purposes: how to use Delphi Create basic knowledge of type libraries;
You can use the IDL (Interface Definition Language Interface Definition Language) Code Type Library; Type Library Editor: Toolbar: You can add interfaces, methods, and attributes to the COM server; Select the Text Labels command to open the title of the toolbar; Interface: Automatically generate a GUID for each new interface; Dispinterface is similar to the interface, but uses different dispatcher to call the server; CoClass : Is a term, assigned to a COM object that implements the interface; ENUMER