Thread: Basic COM function question - Factory or CoCreateInstance?

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    233

    Post Basic COM function question - Factory or CoCreateInstance?

    Hi there,

    I've been trying to learn at least something about the Component Object Model. It was very confusing at first and I got fed up, but after a slight personal meltdown I've managed to return to it and learn a few things.

    Reading the MSDN stuff (which is actually very good when you get to a certain part of it) it discusses the CoCreateInstance function.

    Now AFAIK this function will take a CLSID of an object and a GUID of an interface and then call them as follows:

    Code:
    IDrawable *pShape;
    hr = CoCreateInstance(CLSID_Shape, NULL, CLSCTX_INPROC_SERVER, IID_IDrawable,
         reinterpret_cast<void**>(&pShape));
    Now apparently this creates an object and then associates it with an interface. I'm guessing you can then access the object through the interface pointer. Seems that the object essentially inherits from an abstract class - that abstract class being the interface. Thus you can access the object through a pointer to its abstract parent class.

    The function also takes a flag depending on the intended usage, and also a NULL parameter if you are just making one object.

    Hmm.. ok I mostly get that bit. Not too sure exactly where the CLSID and GUID's are defined though but I don't think I need to worry about that for now.

    So this would give me an object which I could access through an interface pointer. I assume the object in question has to override some/all of the functions in the parent interface class if they are pure virtual functions. I don't know if all functions in the abstract class are pure virtual though (I'm thinking maybe they are).

    So given one now has an object tied to an interface, I tried to look at some of the code I've been compiling lately to try and make sense of it. Seems it's done rather differently though:

    Code:
    ThrowIfFailed(CreateDXGIFactory1(IID_PPV_ARGS(&mdxgiFactory)));
    Which I then changed to (successfully):

    Code:
    ThrowIfFailed(CreateDXGIFactory1(__uuidof(IDXGIFactory4), (void**)&mdxgiFactory));
    You can ignore the ThrowIfFailed macro it's just for error checking the returned HRESULT. Note however that there is only an interface being created here, if created is even the right word to use? I don't see an object at all. The Factory function just requires the GUID of the interface and a ppVoid to an empty pointer made earlier like so:

    Code:
    Microsoft::WRL::ComPtr<IDXGIFactory4> mdxgiFactory;
    So my question is.. where is the actual object associated with the mdxgiFactory pointer?

    In the CoCreateInstance function I can see it as a CLSID reference to a class which I assume is already defined (both the CLSID and the actual class itself).

    I hope that doesn't sound like a load of old waffle. I'd appreciate any insight at all anyone could give me to any part of this. Thanks

  2. #2
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    639
    So my question is.. where is the actual object associated with the mdxgiFactory pointer?
    If you go to a shop that only sells one type of item (CreateDXGIFatory), the shop keeper can be pretty certain what sort of item you want, otherwise why would you go there?
    On the other hand, if you go to a shop filled with many types of items (CoCreateInstance), you really need to tell the shop keeper what sort of item you want because he's not going to look through his entire stock for your item.

    At least, I think the question was "Why does CreateDXGIFactory not have a CLSID and CoCreateInstance does?"

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    233
    Much thanks adey for your reply. I'm pretty sure I got what you meant, I will promptly re-read your reply as much as I have to, to be certain I understood it. Thanks very much for helping

    Edit:

    Think I got it, that particular function is object specific, thus will internally reference a particular specific CLSID to tie to the interface. The more generic function CoCreateInstance is not, so one must provide the CLSID of the object the user wants to create
    Last edited by shrink_tubing; 03-07-2024 at 03:40 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. basic question of using function in C++
    By faisalfn in forum C++ Programming
    Replies: 4
    Last Post: 12-28-2016, 09:30 PM
  2. Basic pointer in function question
    By Origin in forum C Programming
    Replies: 15
    Last Post: 04-01-2010, 10:14 AM
  3. Question about the Factory pattern.
    By h3ro in forum C++ Programming
    Replies: 14
    Last Post: 11-27-2008, 04:55 PM
  4. Function name basic question help
    By kenryuakuma in forum C++ Programming
    Replies: 7
    Last Post: 09-24-2008, 07:48 AM
  5. Stream Factory Interface (Question of Preference)
    By phantomotap in forum C++ Programming
    Replies: 7
    Last Post: 05-02-2008, 03:43 PM

Tags for this Thread