CoCreateInstance

My question is what does this function do.

HRESULT CoCreateInstance(
_In_ REFCLSID rclsid,
_In_ LPUNKNOWN pUnkOuter,
_In_ DWORD dwClsContext,
_In_ REFIID riid,
_Out_ LPVOID *ppv
);

And to what points the last argument ( LPVOID *ppv ) ?

Please help!
If you still don't know after reading this, please let us know.
http://msdn.microsoft.com/en-us/library/windows/desktop/ms686615%28v=vs.85%29.aspx
Last edited on
I'm letting you know. I post this thread after seeing this but I don't get anything from there.
COM is Microsoft's object technology.
- Objects can be installed on a computer and discovered at runtime.
- Objects are uniquely identified by large unique numbers called UUIDs (or Class IDs)
- Objects may run within your application (called inproc), as a seperate process or on another computer (in which case, you'll have a stub installed locally).
- Inheritance is not directly supported. Only aggregation is supported.

When you want to instantiate an object, it's created by a factory function that does the grunt work for you. The function that instantiates a COM object is CoCreateInstance. The parameters are:
- rclsid: the unique id of the class
- pUnkOuter: the interface of the outer class for aggregates (not often used)
- dwClsContext: type of instatiation (inproc, server, remote), you usually don't have a choice
- rid: interface id
- ppv: address of the returned interface

If you intend to use COM, don't used the API directly, it's a world of pain. Use ATL instead, as difficult as it may intially seem, it's the easiest was to use COM from C++.
Last edited on
COM isn't the kind of thing you can study for 15 minutes, get yourself confused, then fire off a quick question to some internet forum and then have everything become completely clear. I've studied it intensively for 15 years, have a whole bookshelf of books on it, and still have not completely mastered it to my complete satisfaction. However, to directly answer your question, one of the most common 'design patterns' found with everything related to COM is the concept of returning pointers to objects as the last parameter of a function.

To elaborate, COM objects aren't instantiated the way C++ objects are, that is, with the 'new' operator. Rather, they are instantiated through CoCreateInstance() or CoGetClassObject(), or some such function. Those functions return success/failure codes (a whole topic in itself) - not pointers to the object as with C++'s 'new' operator. That being the case, where does one obtain a pointer to the newly instantiated COM object if not through the function's return value? The answer is through that last parameter, i.e., ppv, of CoCreateInstance. It is an [out] parameter. That's a another whole topic one must master with COM. There are [in] parameters, [out] parameters, [out, retval] parameters, and so forth.

kbw said ...


If you intend to use COM, don't used the API directly, it's a world of pain. Use ATL instead, as difficult as it may intially seem, it's the easiest was to use COM from C++.


That's probably good advice for most people. Maybe unfortunately for me, I'm not like most people. I invested a great deal of time in using ATL, have most books written on it, but eventually turned away from it. I use COM a lot and write my own raw code, including ActiveX controls. My advice to anyone learning COM is to get several books on ATL and attempt to learn and use it, if for no other reason than that all ATL books I've ever seen devote the 1st half of the book to explaining the fundamental concepts of COM and component development.
If don't know how to use COM then ATL will not help you in any way.
@OP: If you're just starting off with COM then my guess is that you missed that asterix on the fifth argument. This function requires a pointer to a void pointer.
Good catch Computergeek01!

Two levels of indirection are definitely involved. Upon CoCreateInstance() returning a successful HRESULT, ppv will point to a VTable. Within that VTable at various memory offsets are pointers to the methods of the object.

It can certainly become confusing, because in the function where your CoCreateInstance() is located, you'll have a declaration of an interface pointer something like so ...

1
2
3
4
5
6
7
8
9
ISomeInterface* pSomeInterface=NULL;
...
...
hResult=CoCreateInstance(CLSID_SOME_OBJECT, ... , (void**)&pSomeInterface)
if(SUCCEEDED(hResult))
{
   pSomeInterface->SomeMethod(...)
   pInterface->Release()
}


You shouldn't get me started on COM!
However, note the OP showed a CoCreateInstance declaration where the last param was declared as LPVOID*, which, when unraveled, ends up being, through the mysterious alchemy of typedef, a **.
Last edited on
COM is only supported in Win 7 and Vista(one of the updates) right?
And it's all about polymorphic classes, "interfaces" and stuff....

Aceix.
my guess is that you missed that asterix on the fifth argument.


The function prototype it is taken straight from MSDN:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms686615%28v=vs.85%29.aspx

COM is only supported in Win 7 and Vista(one of the updates) right?
And it's all about polymorphic classes, "interfaces" and stuff....


No, COM was developed in the early 1990s initially during 16 bit Win 3.0 times. Hard to believe, but true. It reached its full development (32 bit) around Win 95 times in the mid 1990s. So its supported (and a main component of) every Microsoft OS since Win 95. I haven't looked at WinRT yet, but my understanding of that part of Win 8 is that its a further development of COM. So I believe its still an important thing.
... is only supported in Win 7 and Vista
Don't go off thinking there's anything new in Vista or Win 7.

They just added very slow and annoying bells/whistles, UAC, 64 bit support and a huge memory footprint.
64 bit support


There is 64 bit support since windows XP.
Sorry, the reason why I said that was because, I tried using some functions in the <shobjidl> header, but when I compiled and ran, it only run on Windows 7 not my XP.

I used functions like the CoCreateInstance(), The GetSelectedItem() methods of some interfaces,...
...but that part of the code did not work on XP.

Aceix.
Last edited on
.. but no drivers.
closed account (z05DSL3A)
freddie1 wrote:
I haven't looked at WinRT yet, but my understanding of that part of Win 8 is that its a further development of COM. So I believe its still an important thing.


Charles Petzold wrote:
For writing Metro style applications, a new object-oriented API has been introduced called the Windows Runtime or WinRT (not to be confused with the version of Windows 8 that runs on ARM processors, called Windows RT). Internally, the Windows Runtime is based on COM (Component Object Model) with interfaces exposed through metadata files with the extension .winmd located in the /Windows/ System32/ WinMetadata directory.

Programming Windows®, Sixth Edition
Microsoft Press: A Division of Microsoft Corporation
.


I did read somewhere that you can use pure C++ and avoid C++/CX.
There is this info (about windows 8 apps)

http://en.wikipedia.org/wiki/Windows_Runtime
Last edited on
Topic archived. No new replies allowed.