Creating an Instance of a COM, a Bird's Eye View

Let's see, from a bird's eye view, how the whole thing works. To get an interface to the ComInterface client, call:

IComInterface * pComInterface;
HRESULT hr= CoCreateInstance(CLSID_X, NULL,CLSCTX_ALL,IID_X,
                             (void**)& pComInterface);

if(hr == S_OK)
   hr = pComInterface ->method x;

Let's see how this magical call to CoCreateInstance yields us the pointer to the interface, pComInterface, that we so dearly need.

CoCreateInstance calls:

CoGetClassObject(CLSID_X, CLSCTX_ALL /*dwClsContext*/, NULL, ,IID_X,
                 ,[out]pointer to X interface);

CoGetClassObject invokes the COM Service Control Manager, which searches in the Registry using the CLSID_X. It finds the associated ComServer DLL, loads it into memory, and calls the DllGetClassObject method of the DLL.

From the diagram above, it can be seen that the DLL contains an object of ComClassFactory. Also note that ComClassFactory inherits from IClassFactory and implements its methods, mainly CreateInstance.

The DllGetClassObject can be implemeted like this:

{

  ComClassFactory *pClassFactory ;
  pClassFactory = new ComClassFactory;

hresult = pClassFactory -CreateInstance(NULL, ,IID_X,
                                        [out]pointer to X interface)

}

From the diagram, you can see that ComClassFactory contains an object of ComClass.

Note that the ComClass inherits from IComInterface and implements all its methods. So, ComClassFactory :: CreateInstance(..) is implemented like so:

{

  ComClass *pComClass;
  pComClass = new ComClass;    // create a new object
  pComClass->QueryInterface( IID_X,[out]pointer to X interface)

Use the object pointer to call its implemented QueryInterface and return a pointer to the interface that we were seeking (one having IID_X). This is how we magically get our dear pointer to interface X.

Summary

CoCreateInstance(CLSID_X, NULL,CLSCTX_ALL,IID_X, (void**)&
                 pComInterface);

calls

CoGetClassObject(CLSID_X, CLSCTX_ALL /*dwClsContext*/, NULL, ,IID_X,
                 ,[out]pointer to X interface);

which calls SCM to search in the Registry for the location of the DLL using CLSID_X, loads the dll, and calls

DllGetClassObject(CLSID_X, ,IID_X, ,[out]pointer to X interface);

which creates an object of ComClassFactory:

ComClassFactory *pClassFactory ;
pClassFactory = new ComClassFactory;

and calls

hresult = pClassFactory -CreateInstance(NULL, ,IID_X,
                                        [out]pointer to X interface)

which creates an object of ComClass,

ComClass *pComClass;
pComClass = new ComClass;    // create a new object

and calls

pComClass->QueryInterface( IID_X,[out]pointer to X interface)

which gives us the pointer to the X interface that we so needed.

Alex C. Punnen
alexcpn@hotmail.com



Comments

  • Cosmopolitan Gossip - gucci Thought of as A Must These days

    Posted by incockDak on 03/29/2013 08:03am

    The Secret master the gucci-scene Is Quite Easy! [url=http://growth-management.alachua.fl.us/comprehensive_planning/gucci.html]バック[/url] Cutting edge gucci Guide Unveil The Right Way To Rule The gucci Scene [url=http://growth-management.alachua.fl.us/comprehensive_planning/saclongchamp.php]Sac longchamp[/url] HmvIotZnxNcn [url=http://running-nike0.seesaa.net/]ナイキ[/url]WvgCnqIgkKga [[url=http://free-nike-nikeo.seesaa.net/]ナイキ フリー[/url]DmxHijBmaMro [url=http://xn--nike-ul4c5c5fyqb.seesaa.net/]nike スニーカー[/url]RjdOkyLltSgj [url=http://nikejapan0.seesaa.net/]スニーカーナイキ[/url]CkwZhlZueSfj [url=http://nikesneakersjp.seesaa.net/]nike スニーカー[/url]TlrHaeJgjTjr [url=http://nikegolf00.seesaa.net/]ナイキ[/url]WqeQvkGalFoh [url=http://nikeshoes00.seesaa.net/]nike シューズ[/url]CmbCwxCmxHkp [url=http://sneaker-adidas-jp.seesaa.net/]スニーカー adidas[/url]JuzOcxPhxKaf

    Reply
  • A bvlgari purse Service Speak -- Those Who really cares about zilch is announced the champ?!?

    Posted by expopmerm on 03/22/2013 08:30pm

    [url=http://www.mcmoutletjpinoue.com]mcm バッグ[/url] mcm Is Giving Completely New Life For An Old Issue. . . Metallic Standardized [url=http://www.mcmsalejapanoka.com]mcm バッグ[/url] mcm Is Giving Completely New Lifespan To A Old Subject: Metallic Standards [url=http://www.mcmzankijpshop.com]mcm バッグ[/url] Here Is A Strategy That Is Also Assisting mcm-Industry Experts To Advance [url=http://www.mcmstorejpkodo.com]mcm バッグ[/url] mcm Counterfeits : A Fantastic mcm 'Cheat' Which Fools 97% Of The Users Concise report unwraps the incontestable information regarding bvlgari purse and also the way it could have an effect people. [url=http://www.bvlgarishopjprie.com]ブルガリ[/url] An ideal strategy for bvlgari purse that you will learn about right now. [url=http://www.bvlgarioutletshopjp.com]ブルガリ[/url] Find who's writing about bvlgari purse and the particular reason why you ought to get worried. [url=http://www.bvlgarisalekodojp.com]ブルガリ 財布[/url] Innovative publishing unveils the low down of bvlgari purse in addition , the reasons why you need to take action straight away. [url=http://www.bvlgarisakaijpsale.com]ブルガリ 時計[/url] New essay tells the know-how for bvlgari purse and as a result the reasons you have got to take action soon. [url=http://www.bvlgariaokisalejp.com]ブルガリイルカフェ[/url] Marketplace News : bvlgari purse Understood to be A Must In the present day Be The 1st To View What Scientists Are Saying Over [url=http://www.mcmbagsjphadori.com]mcm バッグ[/url] mcm The Most Common Myths Vs. The Unquestionable Aspects

    Reply
  • I am getting an error while trying to register my component? i have logged in as Restricted user.

    Posted by sreeharsha.kottadamane on 06/23/2007 11:41am

    Hi, I am getting an error while trying to register my component? i have logged in as Restricted user.The error that i am getting is "Class not registered". Can you please help me ? I want to register my component programmatically.

    Reply
  • Excellent! Thank you.

    Posted by Legacy on 12/05/2003 12:00am

    Originally posted by: SAS

    Succinct and informative.

    Reply
  • Tell me how you fuck your mother if you have no sister

    Posted by Legacy on 09/08/2003 12:00am

    Originally posted by: Good man

    s

    Reply
  • Can you tell me how to register a COM exe server?

    Posted by Legacy on 06/18/2003 12:00am

    Originally posted by: Bill Gates

    Tell me plz

    Reply
  • Great Work

    Posted by Legacy on 06/17/2003 12:00am

    Originally posted by: John

    Thanks alex nice work

    Reply
  • What is the point of this article?

    Posted by Legacy on 06/16/2003 12:00am

    Originally posted by: Goo

    What is the point of this article?

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: November 20, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT Are you wanting to target two or more platforms such as iOS, Android, and/or Windows? You are not alone. 90% of enterprises today are targeting two or more platforms. Attend this eSeminar to discover how mobile app developers can rely on one IDE to create applications across platforms and approaches (web, native, and/or hybrid), saving time, money, and effort and introducing apps to market faster. You'll learn the trade-offs for gaining long …

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds