COM technology insider

Source: Internet
Author: User
Tags case statement ole

Chapter 4 components

1. com, the component object model, is a standard on how to build components and how to build applications through components.
2. Advantages of components: applications can develop and change over time, custom applications, Component Libraries, and distributed components.
3. Requirements for components: components must be dynamically connected; internal implementation details must be hidden.
4. the COM component is an executable code that is published in the Win32 dynamic link library (DLLs) or executable file (exes ).

. Components compiled according to the COM specification can meet all the requirements of the component vendors. COM components are dynamically linked.

Use DLL to dynamically link components. It is easy to encapsulate COM components. COM components are declared in a standard way

Deploy them. Com is an excellent way to provide object-oriented APIs or services to other applications.
5. com is not a computer language.
6. It is inappropriate to compare com with DLL. In fact, com uses DLL to provide dynamic links to components.
7. com is not a function set like WIN32API. It is mainly a function set that can provide services in the form of object-oriented APIs.

Service components.
8. com is not a C ++ class library similar to MFC. Com provides developers with a language-independent component library.

But COM does not provide any implementation.
9. com has an API called a com library. It provides component management services that are useful to all customers and components.

.

//////////////////////////////////////// ////////////////////////////

Chapter 4 Interfaces

1. interfaces in COM are everything.
(1) The interface can protect the system from external changes.
(2) interfaces allow customers to process different components in the same way.
2. (1) The COM interface is implemented using a pure abstract base class in C ++.
(2) a com component can provide multiple interfaces.
(3) a c ++ class can use multiple inheritance to implement a component that can provide multiple interfaces.
3. A class is not a component.
4. interfaces are not always inherited. The inheritance of interfaces is just an implementation detail. Besides a class

In addition to several different interfaces, you can use a single class to implement each interface and then use pointers to these classes.
5. components support any number of interfaces. To support multiple interfaces, you can use multiple inheritance. Components supporting multiple interfaces

It can be considered as a set of interfaces.
6. immutability, polymorphism, and inheritance of COM interfaces.
(1) Once an interface is published, it will remain unchanged. When the component is upgraded, the existing

But add some new interfaces.
(2) polymorphism means that different objects can be processed in the same way.
7. virtual function table (vtbl): contains a set of pointers to virtual function implementations.
Define a pure abstract base class that defines the corresponding memory structure. However, this memory is only used when the abstract base class is implemented in the derived class.

To be allocated. When a derived class inherits an abstract base class, it inherits the memory structure.
8. In COM, access to a component can only be done through functions, rather than directly using variables.

9. The real power of an interface is that all classes inheriting this interface can be processed by the customer in the same way.

//////////////////////////////////////// ////////////////////

Chapter 2 QueryInterface Functions

1. Interface query:
The customer interacts with the component through an interface. Other interfaces of the customer query component are also completed through interfaces.

. This interface is iunknown.
The iunknown interface definition is included in the unknown. h header file of Win32 SDK.
Interface iunknown
{
Virtual hresult _ stdcall QueryInterface (const IID & IID, void ** GMM) = 0;
Virtual ulong _ stdcall addref () = 0;
Virtual ulong _ stdcall release () = 0;
}
In iunknown, a function named QueryInterface is defined. You can call QueryInterface to determine components.

Whether a specific interface is supported.
2. All com interfaces must inherit iunknown.
3. Since all com interfaces inherit iunknown, the first three functions in vtbl of each interface are

QueryInterface, addref, and release. If the first three functions in the vtbl of an interface are not the three functions

Is a COM interface. Because all interfaces are inherited from iunknown, all interfaces support

QueryInterface. Therefore, any interface of the component can be used by the customer to obtain other interfaces supported by the component.
4. Non-virtual inheritance: note that iunknown is not a virtual base class, so the COM interface cannot inherit iunknown in virtual mode,

This is because vtbl is incompatible with COM. If the COM interface inherits iunknown in virtual mode, the vtbl of the COM interface

The first three functions in will not point to the three member functions of iunknown.
5. A quertyinterface can be implemented using a simple if-then-else statement, but the case statement cannot be used because

Indicates that the interface identifier is a structure rather than a number.
6. Multiple types and type conversion
7. QueryInterface rules
(1) The same iunknown pointer is always returned by QueryInterface.
(2) If the customer has obtained an interface, it will always obtain this interface.
(3) The customer can obtain the existing interfaces again.
(4) The customer can return from any interface to the starting interface.
(5) If you can obtain a specific interface from an excuse, you can obtain it from any interface.
8. The IID of the interface determines its version. When any of the following conditions is changed, a new ID should be specified for the new interface:
(1) number of functions in the interface.
(2) functions in the interface are sequential.
(3) parameters of a function.
(4) Order of a function parameter.
(5) Type of a function parameter.
(6) Possible return values of the function.
(7) Description of function parameters.
(8) Meaning of functions in the interface.
9. Avoid violation of the implied contract:
(1) make the interface work normally no matter how it is called in its member functions.
(2) force the customer to use this interface in a certain way and clarify this point in the document.

//////////////////////////////////////// //////////////////////////

Chapter 1 reference count

1. lifecycle Control
The other two member functions addref and release of iunknown are used to provide customers with a way to indicate when the processing is complete.

An interface.
2. addref and release implement a memory management technology called reference count.
Reference counting is the simplest and most efficient way for a component to delete itself.
The COM component maintains a value called reference count. When the customer obtains an interface from the component, the reference count value increases by 1.

. After a customer uses an interface, the reference count of the component is reduced by 1. When the reference count value is 0, the component can

Memory deletion.
3. Use the reference counting rule correctly:
(1) Call addref before returning. For functions that return interface pointers, apply corresponding pointer calls before returning

Addref. These functions include QueryInterface and createinstance. In this way, when the customer obtains an interface from this function

It does not need to call addref.
(2) Call release after using the interface. Call the release function of an interface after it is used.
(3) Call addref after assigning values. When you assign an interface pointer to another interface pointer, you should call addref. Change

The reference count of the corresponding component should be added after another reference of the interface is created.
4. In the customer's opinion, the reference count is at the interface level rather than the component level.
5. Why do I choose to maintain a single reference count for each interface instead of maintaining the reference count for the entire component? (1) Enable

Program debugging is more convenient; (2) support on-demand acquisition of resources.
6. Example of addref & release
Ulong _ stdcall addref ()
{
Return interlockedincrement (& m_cref );
Ulong _ stdcall release ()
{
If (interlockeddecrement (& m_cref)
{
Delete this;
Return 0;
}
Return m_cref;
}
7. When creating a new component, a reference to this component should be created. Therefore, when a component is created, the pointer is returned to the customer.

Previously, we should increase the reference count value of the component. This allows programmers not to call createinstance or

After QueryInterface, remember to call addref.
8. Optimized reference counting rules:
(1) output parameter rules: any function that returns a new interface pointer in the output parameter or the return value of this parameter must be connected

The port pointer calls addref.
(2) input parameter rules: you do not need to call addref and release for the interface pointer of the input function. This is because the function life

Period is nested in the caller's life cycle.
(3) input-output function rules: For interface pointers passed in with input-output parameters, you must assign another

Call its release before the interface pointer. Before returning a function, you must call the interface pointer stored in the output parameter.

Addref. For example:
Void exchangeforcachedptr (int I, IX ** ppix)
{
(* Ppix)-> FX (); // do something with in-parameter.
(* Ppix)-> release (); // release in parameter.
* Ppix = g_cache [I]; // get cached pointer.
(* Ppix)-> addref (); // addref pointer.
(* Ppix)-> FX (); // do something with out-parameter.
}
(4) local variable rules: for partial replication interface pointers, because they only exist within the life cycle of the function

You do not need to call addref and release.
(5) global variable rules: For interface pointers stored in global variables, before passing them to another function

You must call its addref. Because this variable is global, any function can terminate its lifecycle by calling its release.

Period. Interface pointers stored in member variables should also be processed in this way. Because any member function in the class has

You can change the status of the sub-interface pointer.
(6) Rules for uncertain: addref and release pairs should be called in any uncertain circumstances.
 
//////////////////////////////////////// ////////////////////////////

Chapter 2 Dynamic Links

1. output function from dll: Mark with extern "C.
2. dumpbin can be used when Vc is used. EXE to get the list of symbols output in a DLL. Run the following command:

Dumpbin-Exports cmpnt1.dll
3. Load dll: loadlibrary takes the name of the loaded dll as the parameter and returns a handle pointing to the loaded DLL.

The getprocaddress function of Win32 can use this handle and the name of the function to be used, and then return a function pointing to the next function.

Pointer.
4. The reason for using DLL to implement components: dll can share the address space of the applications linked to it.

//////////////////////////////////////// //////////////////////////////

Chapter 3 details about hresult, guid, registry, and other

1. Structure of the hresult value:
_________________________________________________________

15bits device code 16bits return code
_________________________________________________________
31 30 16 15 0

2. Common hresult values:
3. Generally, you cannot directly compare the hresult value with a successful code (such as s_ OK) to determine whether a function is successful or not.

You can directly compare it with a failed code (such as e_fail) to determine whether the function call fails. Should be used

Secceeded and failed macros.
Hresult hR = cocreateinstance (...);
If (failed (HR ))
Return;
HR = pi-> QueryInterface (...);
If (succeeded (HR ))
{
PIX-> FX ();
PIX-> release ();
}
Pi-> release ();

4. The device Code defined currently:
----------------------------------
Facility_windows 8
Facility_storage 3
Facility_sspi 9
Facility_rpc 1
Facility_win32 7
Facility_control 10
Facility_null 0
Facility_itf 4
Facility_dispatch 2
Facility_cert 11
----------------------------------

5. General rules for defining hresult:
(1) do not use the values in the range of 0x0000 and ix01ff as the return code. These values are the facility_itf generation defined by COM.

. Only by following this rule will the User-Defined code not be confused with the code defined by com.
(2) do not spread the facility_itf error code.
(3) use the common com Success and Failure Code as much as possible.
(4) avoid defining your own hresult, but use an output parameter in the function.
6. Use the make_hresult macro to define an hresult value. This macro can be used based on the provided severity level, device code, and return generation.

Code to generate an hresult value. For example:
Make_hresult (severity_error, facility_itf, 100 );
7. GUID is the abbreviation of Globally Unique Identifier (globally unique identifier. IID is a 128 Ratio

A guid structure of the (16) bytes.
8. Generate guid: uuidgen. EXE and guidgen. exe
9. Comparison of guid: Operator =; equivalent function isequalguid, isequaliid, isequalclsid.
10. Use guid as the component identifier
11. Because a guid value occupies 16 bytes, The GUID parameter is generally not required. A large amount of data is transmitted by reference.

Delivery.
12. com only uses one branch of the Registry: hkey_classes_root.
13. The Registry CLSID is a string in the following format:
{********-****-****-****-************}
14. The default value of the subkeyword inprocserver32 of the CLSID keyword is the DLL file name of the component.
15. Some special keywords:
(1) appid: The subkeyword under this keyword is used to hide an appid (Application ID) into a remote service.

Tool Name. Distributed COM will use this keyword.
(2) group view category: this branch of the registry can map the catid (Component category ID) to a specific component category.
(3) interface: used to map an IID to information related to an interface.
(4) licenses: saves some license information authorized to use the COM component.
(5) typelib: The Type Library keyword stores information about parameters used by interface member functions.
16. progid naming conventions:
<Program>. <component>. <version>
17. Conversion from progid to CLSID: COM library function: clsidfromprogid and progidfromclsid:
CLSID;
Clsidfromprogid ("*****. ******. ******", & CLSID );
18. Self-Registration: dll must output the following two functions:
Stdapi dllregisterserver ();
Stdapi dllunregisterserver ();
You can use the regsvr32.exe program to register a component. In fact, the preceding function is used to register the component.

.
19. Component categories enable developers to determine whether a component is required by agents without creating component instances. One

A component category is actually an interface set, which is assigned to a guid, which is called a catid.

For a component, if it implements all interfaces of a component category, it can register it as

One member. In this way, the customer can find it accurately by selecting a component that only belongs to a specific component category from the registry.

Required components.
20. Purpose of component category: Specify the set of interfaces that a component must implement. It is used to specify the interfaces that a component needs to provide.

Set.
22. Before using other functions in the com Library (except cobuildversion, this function returns the version number of the COM Library ),

You must first call coinitialize to initialize the com library function. When the process no longer needs to use the com library function, it must call

Couninitialize. For each process, the com library function only needs to be initialized once. This does not mean that multiple calls are not allowed.

Coinitailize, but make sure that each coinitialize has a corresponding counoinitialize call. When the process has

After coinitialize is called, the returned value of calling this function again will be s_false instead of s_ OK.
23. Ole is based on COM. It adds the types library, clipboard, drag-and-drop, ActiveX document, automatic

And ActiveX controls. Include additional support for these features in the OLE library. When you need to use these features

Use oleinitailize and oleuninitialize instead of coinitailize and couninitialize. Ole * function will call

Co * function. However, if the program does not use those additional functions, using OLE * will result in a waste of resources.
24. Standard Method for allocating and releasing memory in COM: Task memory distributor. With this distributor, components can be provided to customers

Memory that can be deleted by the customer. It can be used in multi-threaded applications.
Some convenient functions:
Void * cotaskmemalloc (
Ulong CB // size in bytes of block to be allocated
);
Void cotaskmemfree (
Void * PV // pointer to memory block to be freed
);
25. stringfromguid2 can convert a guid into a string:
Wchar_t szclsid [39];
Int r =: stringfromguid2 (clsid_component1, szclsid, 39 );
The parameter passed to stringfromguid2 is a unicode string (that is, an array of the wchar_t type with a wide character rather than a char class)

Character array ). In a non-Unicode system, the result must be converted to a single-byte character (char ). To this end, you can

Use the ANSI wcstombs function as follows:
# Ifndef _ Unicode
Char szclsid_single [39];
Wcstombs (szclsid_single, szclsid, 39 );
# End if
26,
----------------------------------------------------------------------------
Function Purpose
-----------------------------------------------------------------------------
Stringfromclsid converts CLSID into a text string
-----------------------------------------------------------------------------
Stringfromiid converts IID into a text string
------------------------------------------------------------------------------
Stringfromguid2 converts a guid into a text string. This string will be stored in the buffer allocated by the caller.
------------------------------------------------------------------------------
Clsidfromstring converts a text string to CLSID
------------------------------------------------------------------------------
Iidfromstring converts a text string to an IID
-----------------------------------------------------------------------------

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.