Solved

CoGetClassObject API

Posted on 1997-12-30
7
493 Views
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 ?
0
Comment
Question by:gilad111397
  • 4
  • 3
7 Comments
 
LVL 7

Accepted Solution

by:
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.  
 
0
 

Author Comment

by:gilad111397
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 ?


0
 
LVL 7

Expert Comment

by:galkin
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.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:gilad111397
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 ?

0
 
LVL 7

Expert Comment

by:galkin
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.
0
 

Author Comment

by:gilad111397
ID: 1409719
Hello Galkin,

how can I pull it if I didn't create it, e.g if it is
exist.
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.


0
 
LVL 7

Expert Comment

by:galkin
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.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article shows how to make a Windows 7 gadget that accepts files dropped from the Windows Explorer.  It also illustrates how to give your gadget a non-rectangular shape and how to add some nifty visual effects to text displayed in a your gadget.…
For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared 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. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

746 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now