COM in-process external components

Source: Internet
Author: User
COM in-process components and out-of-process components 1. com in-process Components

Components in the process exist in the form of a dynamic link library (DLL Extension). When the client needs to use the services provided by the components in the process, it only needs to load the DLL into the client process. The in-process component must provide the self-registration function so that the client process can find the required information from the registry and load the component.

 

Four functions need to be exported for In-process Components

Dllregisterserver:

Parameter: None

Returned value: hresult indicates the Error Type of the registration operation.

 

Dllunregisterserver

Parameter: None

Returned value: hresult indicates the error type for deleting corresponding items in the registry.

 

Dllgetclassobject

Parameter 1: CLSID & CLSID of the component (because a DLL can be used as the carrier of multiple components)

Parameter 2: IID &, The IID of the interface to be obtained from the component

Parameter 3: void **, receiving the double pointer of the obtained interface pointer

Returned value: hresult indicates the Error Type of the operation result, which is usually (e_outofme \

Mory, s_ OK, e_nointerface, class_e_classnotavailable );

Dllcanunloadnow

Parameter: None

Returned value: hresult indicates whether the component can be detached.

Note: The com library automatically calls this method at intervals to determine whether the component can be detached.

 

Content required in the internal component Registry

Located under CLSID of hkey_classes_root

Among them, 7f21137d... Is the CLSID of the component.

The value corresponding to inprocserver32 is the complete path of the component in the process.

The value corresponding to progid is the component's note name. The client can obtain the CLSID of the component through this name.

 

2. com out-of-process components 2.1 LPC and RPC

Com uses the LPC (local process call) and RPC (Remote process call) methods for communication between processes. LPC is used for communication between different processes on the same machine, RPC is used for communication between processes on different machines.

2.2 stub proxy DLL

To provide LPC processing, com provides a very simple method. When the component is an out-of-process component, the customer communicates the DLL of the same imitation component, this dll can be used to adjust parameters and call LPC for the customer. This dll is called a proxy DLL on the client side, and is called a stub DLL on the server side.

To generate a proxy stub dll:

 

1. Compile the IDL file

This IDL file contains all the interfaces defined by the server component, except for the CLSID of the component identifier.

2. Use midl to compile the IDL file

Several files generated after compilation

1) xxx_h.h contains the header file defined by the interface and the IID declaration of all interfaces.

2) xxx_ I .c interface IID Definition

3) xxx_p.c proxy File

4) dlldata. c Data File

3. Use the above file to generate the stub proxy DLL

1) Add register_proxy_dll in preprocessing.

2) provide the input library rpcrt4.lib UUID. Lib

3) Add the def file and export the five functions.

4) compile and generate DLL

The generated dll has five export functions, as shown in figure

 

The first four components are consistent with those in-process components.

 

2.3 Register components outside the process

The registration of out-of-process components requires the registration of the stub proxy DLL and the EXE file. The registration of the stub proxy DLL is consistent with that of the In-process components, the registration of EXE files relies on its command parameters.

The two entry functions of the component program for registration are dllregisterserver and dllunregisterserver. To register the component, run the following command: regsvr32.
*. Dll. Run the regsvr32/u *. dll command for the anti-register component. Use this command for internal component registration. The external component registration must support two command line parameters:/regserver and/unregserver.

CLSID subkey:

Figure 1

 

Figure 2

Under the interface subkey:

Figure 3

Figure 4

 

Proxystubclsid32 is the CLSID of the stub proxy DLL of the interface, as shown in Figure 1. In this way, when querying an interface, the com library will know where to load the stub proxy DLL.

Category 3 Factory

The com library creates a COM object through a class factory. Each class corresponds to a com class, and a class factory is used to create the com class object. The factory itself is also a COM object, which supports the interface iclassfactory.

Createinstance creates the corresponding COM Object and lockserver controls the life cycle of the component.

The class factory is created by the dllgetclassobject function.

Dllgetclassobject returns the interface pointer of the class factory object, and then creates the corresponding COM object through createinstance.

4. Interaction between the com Library and the class factory

Create an object function:

1) if you create a remote object or want to obtain multiple interface pointers of the object at a time, use cocreateinstanceex.

2) If you want to obtain a class factory object or call some member functions of the class factory, use cogetclassobject. Generally, IID = iid_iclassfactory. In-process components directly call the DLL cogetclassobject, if the factory object created by cogetclassobject is located outside the process, the function starts the component process and waits until the component process registers the factory of the COM class object it supports to com, return the factory information.

3) In other cases, cocreateinstance is used to create an object. cocreateinstance encapsulates the process of creating an object in the class factory. The interface pointer of the COM object is returned, and objects on the remote machine cannot be created.

 

 


5. com Library

Com library initialization function: coinitialize

Com library termination function: couninitialize

Com Library Memory Management:

The Memory Manager standard provided by COM is a COM interface imalloc. When the memory allocation and release of components are not in the same module, the memory manager is required. The com library encapsulates three API functions for memory allocation and release.

Cotaskmemalloc

Cotaskmemfree
Cotaskmemrealloc

 

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.