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

  • The explosion in mobile devices and applications has generated a great deal of interest in APIs. Today's businesses are under increased pressure to make it easy to build apps, supply tools to help developers work more quickly, and deploy operational analytics so they can track users, developers, application performance, and more. Apigee Edge provides comprehensive API delivery tools and both operational and business-level analytics in an integrated platform. It is available as on-premise software or through …

  • Java developers know that testing code changes can be a huge pain, and waiting for an application to redeploy after a code fix can take an eternity. Wouldn't it be great if you could see your code changes immediately, fine-tune, debug, explore and deploy code without waiting for ages? In this white paper, find out how that's possible with a Java plugin that drastically changes the way you develop, test and run Java applications. Discover the advantages of this plugin, and the changes you can expect to see …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds