Cocreateinstance internal implementation

Source: Internet
Author: User
Cocreateinstance (....) {//....... iclassfactory * pclassfactory = NULL; cogetclassobject (clsid_object, callback, null, iid_iclassfactory, (void **) & pclassfactory); pclassfactory-> createinstance (null, callback, (void **) & punk); pclassfactory-> release ();//........}

In this section, the class factory object is obtained first, and then the class factory creates components to obtain the iunknown pointer. Take a further step and check the internal pseudo code of cogetclassobject:

Cogetclassobject (.....) {// check the registry clsid_object to find the location and file name of the component DLL // load the dll library // use the getprocaddress function (...) obtain the function pointer of the dllgetclassobject function in the dll library. // Call dllgetclassobject} // What is dllgetclassobject, which is used to obtain the class factory object. Only the class factory can be used to create components. /// The following is the pseudo code of dllgetclassobject: dllgetclassobject (...) {//...... cfactory * pfactory = new cfactory; // class factory object pfactory-> QueryInterface (iid_iclassfactory, (void **) & pclassfactory); // query iclassfactory pointer pfactory-> release (); //......} /// the cogetclassobject process has reached this point. Now, return cocreateinstance and check the pseudo code of createinstance: cfactory: createinstance (.....) {//........... cobject * pobject = new cobject; // Component Object pobject-> QueryInterface (iid_iunknown, (void **) & punk); pobject-> release (); //...........}

In this part, we will construct a minimum framework structure for creating COM components, and then let's take a look at the internal processing process.

 
Iunknown * punk = NULL; iobject * pobject = NULL; coinitialize (null); cocreateinstance (clsid_object, clsctx_inproc_server, null, iid_iunknown, (void **) & punk ); punk-> QueryInterface (iid_iojbect, (void **) & pobject); punk-> release (); pobject-> func (); pobject-> release (); couninitialize ();

 
Cocreateinstance (....) {....... iclassfactory * pclassfactory = NULL; cogetclassobject (clsid_object, callback, null, iid_iclassfactory, (void **) & pclassfactory); pclassfactory-> createinstance (null, callback, (void **) & punk); pclassfactory-> release ();........}

This is a typical framework for creating COM components, but I am interested in cocreateinstance. Let's take a look at what it has done internally. The following is an internal implementation of the pseudoCode:

 
Cogetclassobject (.....) {// check the registry clsid_object to find the location and file name of the component DLL // load the dll library // use the getprocaddress function (...) obtain the function pointer of the dllgetclassobject function in the dll library. // Call dllgetclassobject} What is dllgetclassobject, which is used to obtain the class factory object. Only the class factory can be used to create components. the following is the pseudo code of dllgetclassobject: dllgetclassobject (...) {...... cfactory * pfactory = new cfactory; // class factory object pfactory-> QueryInterface (iid_iclassfactory, (void **) & pclassfactory ); // query the iclassfactory pointer pfactory-> release ();......} the cogetclassobject process has reached this point. Now, return cocreateinstance. Check the pseudo code of createinstance: cfactory: createinstance (.....) {........... cobject * pobject = new cobject; // Component Object pobject-> QueryInterface (iid_iunknown, (void **) & punk ); pobject-> release ();...........}

In this section, the class factory object is obtained first, and then the class factory creates components to obtain the iunknown pointer. Take a further step and check the internal pseudo code of cogetclassobject:


Is an example of the copy from the COM + technology insider, you can clearly see the entire process of cocreateinstance.

(7) four functions required by a typical self-registered com DLL

Dllgetclassobject: used to obtain the class factory pointer
Dllregisterserver: registers necessary information to the Registry.
Dllunregisterserver: uninstall Registration Information
Dllcanunloadnow: This function is called when the system is idle to determine whether the DLL can be detached.

DLL also has a function named dllmain, which does not need to be implemented in COM, but is automatically included in the components generated by VC, it is mainly used to obtain a global instance object.

(8) Important Role of registry in COM

first, you must understand the guid concept. All classes, interfaces, and type libraries in COM are uniquely identified by guid, which is a 128-Bit String, the GUID generated based on the algorithm is the only guid in the world. COM component creation and Query Interfaces are all performed through the registry. With the registry, the application Program does not need to know the DLL file name and location of the component. You only need to query the component by clsid. When the version is upgraded, you only need to modify the registry information to transfer it to the new version of DLL.

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.