Create COM Object-external mechanism

Source: Internet
Author: User

The so-called "external mechanism" refers to the process in which an application creates an atl com Object factory. The application does not care whether the COM object is implemented by MFC or by ATL. It always uses API functions such as cocreateinstance to create COM objects through the class factory. In ATL, how does an application call cocreateinstance to the createinstance method of the atl com Object Class factory?

 

COM Server

 

The COM Object cannot exist out of thin air. It must exist in an executable file of the operating system. Because only the Windows operating system supports the COM specification, it is natural that COM objects exist in executable files of the Windows operating system.

The executable files in windows are in two formats: EXE and DLL. The difference between the two file formats is unnecessary here. If you do not know, you may not understand this article.

The executable program that can generate COM objects is called the COM server. EXE is an out-of-process server, and DLL is an in-process server. Here we only discuss the DLL situation. Since the DLL itself can only interact with the outside world through external output functions, as a COM server, the DLL also reflects the role of its server through four output functions. This is the famous four functions:

  • Dllregisterserver;
  • Dllunregistersever;
  • Dllgetclassobject;
  • Dllcanunloadnow;

 

The working mechanism of the COM server can be used to represent:

 

 

 

The important functions of the COM server can be summarized into three:

  • Manage the server lifecycle;
  • Manage registration of servers and objects;
  • A class factory that obtains COM objects;

 

We can see that, as the DLL of the COM server, four functions are used to complete these three functions. The call times of the four output functions are as follows:

  • Dllregisterserver and dllunregisterserver: when you use the regsvr32 program to register and unregister the server;
  • Dllcanunloadnow: When the cofreeunusedlibraries system function is called;
  • Dllgetclassobject: This function is called when a COM object is created. We know that the API function for creating a COM object is cocreateinstance. Cocreateinstance is an encapsulation function that encapsulates calls to cogetclassobject and the createinstance function of the corresponding class factory. Cogetclassobject finds the corresponding server through the registry mechanism, and calls the server's dllgetclassobject function to obtain the class factory. Once a class factory object is obtained, you can call the createinstance method of the class factory object to create a COM object.

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.