Chapter II

xiaoxiao2021-03-06  52

Talk again to interface and implementation

Users can choose any C compiler, but eventually they must use a compiler of C (not other languages).

COM provides a language, which only uses the syntax of the C language that everyone is familiar with, and adds some capabilities used to eliminate unity in the C language, namely the interface definition language

Idl (Interface Definition Language)

COM IDL adds to COM-related extensions based on OSF DCE RPC IDL (inheritance, polymorphism)

MIDL.EXE is an IDL compiler,

Generate C / C compatible header files

Generate a binary, called type library file

Method and effect

Each COM method returns a HRESULT value,

Divided into three parts: severity, opcode and information code

If HRESULT indicates an abnormal result, the Java virtual opportunity throws an exception.

And C must manually check HRESULT.

Interface and IDL

IDL uses interface keyword definition interface

Definition: Interface name, base interface name, interface body, interface attribute

There are at least two properties per COM interface: one is [object] indicates that COM instead of the DCE-style interface; one is the actual name of the interface [UUID]

GUID is a 128-bit large number, which guarantees unique in time and spatial, standard text format for [UUID]

Iunknown

Method: queryinterface (), addref (), release ();

IUNKNOWN is the root of all interfaces

CoM prohibits multiple interface inheritance, but implementing multiple interfaces can be exposed

Resource Management and IUNKNOWN

CF reference count rules:

1. When a non-empty interface pointer is copied from a memory location to another memory location, addRef ();

2. For the memory location already containing a non-empty interface pointer, Release () is called before replying the memory;

3. If possible, excess addRef () and release () can be optimized;

Call rules:

AddRef ()

A1 When a non-empty interface pointer is written to a local variable;

A2 When the called party writes a non-empty interface pointer to the method or function [OUT] or [IN, OUT] parameters

A3 When the caller returns a non-empty interface pointer as the actual result of the function

A4 When writing a non-empty interface pointer to a data member of an object

RELEASE ()

R1 rewrites a non-air local variable or data member

R2 before the scope of the non-air local variable

R3 is called the [IN, OUT] parameters of the method or function, and the initial value of the parameter is not empty.

R4 rewrites before non-empty data member

R5 before leaving the object paratory function

Special rule

When the S1 caller passes a non-empty interface pointer to the function through the [IN] parameter, do not need to call a or R

Type forced conversion and iUnknown

HRESULT QueryInterface ([in] refiid riid, [out] void ** ppv);

If the object does not support the requested interface type, the PPV is set to NULL, return E_NOIInterface

If support, then rewrite the pointer, return S_OK, call addRef ();

AddRef and Release are not actions for the entire object, but an operation for interface pointers.

Void ** caused by unsafe vulnerability, available macro IID_PPV_ARG to solve

Implement IUNKNOWN

AddRef () and Release ()

The destructor in the class definition is protected, ensuring that the object is always created on the pile, and Delete this is always correct.

But sometimes we want the object not allocating in the heap, then the reference count can be optimized, return a reference value

Queryinterface ()

When a request is supported by multiple interfaces, the type conversion operation must clearly select a more accurate base class.

Use the COM interface pointer

Because the C language mapping of COM does not provide Runtime Layer, the C programmer must display iUnknown method, but this code efficiency is higher.

For Java and VB, IUNKNOWN details are hidden after virtual machines.

The smart pointer can simplify the operation of using the COM interface pointer in C .

But it also brings a lot of problems.

Optimize QueryInterface

Each COM-compatible class provides a table that maps the IID supported by this class to a place supported to the object by a fixed offset or other technology. (Similar to message mapping macro in the MFC)

type of data

Conversion of OleChar to TCHAR

BSTR: String Type

IDL and COM also support United (UNION), to ensure that there is no secondary meaning, providing full discriminator

COM provides a general purpose, useful for VB, namely Variant

The interface of the COM can also be passed as a parameter: static or dynamic

IDL attribute and COM properties

Indicates an object to have some disclosed properties and can access or modify these attributes through the COM interface. Some are useful.

The COM IDL allows the interface to add some comments to reflect the method to read and write an object.

abnormal

Abnormal API: Throw SterrorInfo () Capture Exception GeterrorInfo ();

COM exception object must support IrrorInfo interface

Throw an abnormal object must implement the ISUPPORTERRORINFO interface to determine which interface supports exception

COM requires that "pure C abnormal" spreads outside the border of the method

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

New Post(0)