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
  • 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.
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.


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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

This article describes how to add a user-defined command button to the Windows 7 Explorer toolbar.  In the previous article (, we saw how to put the Delete button back there where it belongs.  "Delete" is …
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
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.…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

785 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