CoGetClassObject API

Posted on 1997-12-30
Last Modified: 2013-12-03
The 1,4 parameters are difficult.
In order to use this API, it is required to provide
the CLSID in the first variable (in REFCLSID type).

The question is: How can I receive this parameter ?
Question by:gilad111397
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3

Accepted Solution

galkin earned 150 total points
ID: 1409714
coGetClassObject is usually used to create class factory, small object intended to create desired object itself. This function as oppose to CoCreateInstance allows you to create IClassFactory(2) interface only once store its pointer somewhere and then use IClassFactory::CreateInstance to create object. It means you avoid creating class factory each time, as it happens to CoCreateInstance. This function should be used if you need create a lot of objects or you need to create then very often.
The first parameter is CLSID of the object server. It is unique in time and space identifier. To be used this server must be register in OLE register data base, i.e. it must create several keys to be identified by OLE. The most important key is key representing server CLSID and it must be created under HKEY_CLASSES_ROOT/CLSID. Look at this key and you will find a lot CLSIDs. This key in turn contains several key, but only one is reqired, namely InProcServer(32) if the server is DLL and LocalServer(32) if it is exe. This Key has default value, full path to server file. So if you know server file you can serach in registry for its CLSID. In coGetClassObject you usually use IID_IClassFactory(2) as 4 parameter since I this function is usually intended to create class factory. When obtaining pointer to class factory via 5 parameter in GoGetClassObject you cann use IClassFactory::CreateInstance passing UUID of interface quired as first parameter and reference to interface as second.  

Author Comment

ID: 1409715
Thank you for answering my question.
I am sorry I wasn't very specific, so I'll ask again:

(not only in this API) how can the compiler know my interface
name when I want to receive it and not IID_IUnknown ?


Expert Comment

ID: 1409716
Each interface is identified by global unique identifier which is 128 bit number and is garanteed to be unique in space in time. This number can be generated using rather complicated mechanism, involving current time, network card and so on. OLE provides uuidgen.exe utility for generating this number. VC++ also provides visual tool called GUIDGEN.exe found in VC/bin derectory. When client asks server to create object and return reference to specific interface it passes IID_IYourInterface either to CoCreateInstance or IClassFactory::CreateInstance. This number is global unique indentifier of the requested interface. So client must know this interface number as well as interface structure to be able to ask server to return it. Besides if you want to marshal your interface acroll tread or process boundaries you must write specific piece of code called marshaller to pass interface reference across these boundaries and you must register your marshaler under HKEY_CLASSES_ROOT/Interface/<UUID of your interface> key. Server when being asked to create object and return reference to specific interface intentified by UUID checks this number and because it is unique create object of specified type or return error indicating it is unable to create object with requested UUID.
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now


Author Comment

ID: 1409717
Thanks again.

one last clarification: am I suppose to write the UUID as
a number or by the name ?
If it is name, how will the compiler know this name ?
If it is UUID, how can I define it in a variable ?


Expert Comment

ID: 1409718
This number is stored as structure of fore members since it is very large 128 bit number. You should use special macro DEFINE_GUID() which takes several parameters and creates necessary structure. If you use GUIDGEN.exe it allows you to generate this UUID and put necessary string containing DEFINE_GUID() to the clipboard so what you need to do then is to paste it into your source code where you need.

Author Comment

ID: 1409719
Hello Galkin,

how can I pull it if I didn't create it, e.g if it is
I need it for QueryInterface function. the interface name is
required in this function and I can't simply write it because
the compiler doesn't recognize it.
The function :
HRESULT QueryInterface(
  REFIID iid,
  void ** ppvObject
and iid is my problem.


Expert Comment

ID: 1409720
I see you don't distinguish between CLSID and interface ID. Inspite of the fact they are both 128 bit number unque in space and time they play diffrent role in OLE. CLSID is class ID and is intended to help OLE to locate server. This is its only purpose. Each server be out of proc(exe) or in proc(DLL) must have this unique numbe and create corresponding key under HKEY_CLASSES_ROOT/CLSID. This key may contain several subkeys but only one is requried is InprocServer(32) if it is DLL or LocalServer(32) if it is EXE which has only one string entry(default) full path to server file. When client wants to create a object it calls OLE helper API CoCreateInstance or CoGetClassObject passing CLSID of the objects server. Client doesn't know where the server resides. It can be in the same computer, on corporate network or even on the Internet. It is InprocServer(32) key by which OLE locates server and loads it in memory if it is DLL or launches it if it is EXE. This feature is called local/remote transparency and is one of the main advantages of OLE/COM. But to retreive reference to specific interface client must somehow identify this interface and interface ID is important. Each interface msut have this unique identifier which is generated by the same mechanism I already described. When client asks server for specific interface it uses IUnknown::QueryInterface which has two parameters. The first is this interface unique identifier and the second is indirect reference to the quired interface returned. So you must know ID of interface you want to quire for. All OLE stundard interfaces(IUnknown, IDispatch, IAdviseSing etc.) already have their unique IDs and they are defined in OLE standard include files and exported from OLE standard DLL(OLe32.DLL, OLEAUT32.DLL etc.). They are defined as IID_IUnknown for IUnknown, IID_IDispatch for IDispatch, IID_IViewObject for IViewObject and so on. If you define your own custom interface you must generate its unique ID by the tools I told you and define it in the same file when interface declaration is defined so client can look at it and can create object retreiving this interface reference.

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As more and more people are shifting to the latest .Net frameworks, the windows presentation framework is gaining importance by the day. Many people are now turning to WPF controls to provide a rich user experience. I have been using WPF controls fo…
What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor ( Top Charts is a view in which you can set seve…

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question