CoGetClassObject API

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 ?
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.  

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gilad111397Author Commented:
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 ?

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.
OWASP Proactive Controls

Learn the most important control and control categories that every architect and developer should include in their projects.

gilad111397Author Commented:
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 ?

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.
gilad111397Author Commented:
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.

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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.