A basic COM interface implemented in C IFoo (ii)

Source: Internet
Author: User
Tags function definition function prototype

The COM interface IFoo implemented in the C Implementation COM interface Series 1 is coupled with the customers who use it, without implementing the modules that are separate from each other, and therefore does not conform to modular programming ideas. This issue adds class factory support so that the implementation of the interface is separated from the use of the interface.

---------------------------------------------------

What is the role of the class factory in the end?

Separate the implementation of the interface from customer use?

Necessarily With CoCreateInstance, a customer can have no knowledge of the existence of a class factory at all, and create components, get the interfaces implemented by the components, and use them.

That is, the COM library can completely abandon the concept of a class factory, but provide a prototype of such a function: Cocreateobject (REFID rclsid,......,refid riid,void **PPITF), the user can provide riid when called iid_ Unknown, or an interface specific to the object, gets the object's IUnknown or specific interface pointer directly.

As you can see, this is exactly what CoCreateInstance did.

Class 1 provides a way to indirectly create class objects: The user can first acquire and hold the class factory interface pointer and create class objects from the class factory interface that the pointer points to. For places where you need to create multiple (or duplicate) class objects, you reduce the overhead of locating object libraries every time and loading object libraries into memory.

The 2 class plant provides another way to ensure that the component library stays in memory and is not unloaded: Class-factory interface function Lockserver. The component library maintains a library-scoped counter, which is only 0 o'clock, and the component library allows itself to be unloaded out of memory. (In contrast, reference counts are scoped to the class object and are maintained by the various interfaces implemented by the class.) If the reference count of a class object reaches 0, the memory occupied by the object is freed and the interface pointer on the object is no longer valid. )

In addition to calling the Lockserver Lock component library, the component library cannot be unloaded when the number of components created is greater than 0 o'clock. It can also be said that invoking the Lockserver () once is equivalent to creating a component.

-----------------------------------------------------------------------

Client side: 1 What information do you need to know to use an interface?

Alternate: Interface IID Class object (class Factory) CLSID (or ProgID)

Interface function prototype (number of parameters, type, return value)

The threading model that implements the interface component (in-process, out-of-process, remote)?

Type library typelib information?

Service side: 2 Implement a component and interface for customer invocation, what needs to be provided?

Alternative: The content that all customers need to use components and interfaces is additional:

--------------------------------------------------------------------

Add to the DLL. Does the def file differ from specifying _DECLSPEC (dllexport) directly in the function definition where you want to export it? What difference does it have?

Related Article

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.