Pointer To Functions In COM Confusing

This is a discussion on Pointer To Functions In COM Confusing within the C++ Programming forums, part of the General Programming Boards category; Hi I am not one for using virtual inheritance or pointers to functions if I can help it, but I ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    76

    Question Pointer To Functions In COM Confusing

    Hi I am not one for using virtual inheritance or pointers to functions if I can help it, but I am trying to start to learn the basics of direct x and it was recommended i knew a bit about COM first. I was very happy with it until I got to this function which confused me.
    Code:
    HRESULT __stdcall CCOM_OBJECT::QueryInterface(const IID &iid, void **iface)
    {
    	// this function basically casts the this pointer or the iunknown
    	// pointer to the interface requested, notice the comparison with
    	// the GUIDs generated and defined in the beginning of the progra,
    
    	// requestiong the IUNKNOWN base interface
    	if (iid == IID_IUnknown)
    	{
    		cout << "Requesting IUnknown interface" << endl;
    		*iface = (IX*)this;
    
    	}
    
    	if (iid == IID_IX)
    	{
    		cout << "Requestiong IX interface" << endl;
    		*iface = (IX*)this;
    	}
    	else
    	{
    		if (iid == IID_IY)
    		{
    			cout << "Requestion IY interface" << endl;
    			*iface = (IY*)this;
    		}
    		else
    		{
    			cout << "Requesting Unknown interface!" << endl;
    			*iface = NULL;
    			return(E_NOINTERFACE);
    		} // end else
    	}
    	// if everthing went well cast pointer to IUNKNOWN and call addref()
    	((IUnknown *)(*iface))->AddRef();
    
    	return (S_OK);
    
    }
    Now my problem is I have no idea what parameter this is
    void **iface
    at first i thought it was a pointer to a function but that cant be because it gets set to *iface = (IY*)this; which by its nature isn't a function so im completely confused. Even by the line that calls the function which is
    punknown->QueryInterface(IID_IY, (void**)&piy);

    Somebody please help

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    It's actually an interface cast to void. Every COM interface inherits from an interface called IUnknown. This interface has 3 functions that every COM object must implement. 2 of them control reference counting but the third (QueryInterface) allows you to query 1 interface of an object for another.

    For instance; Say I had an object called CGovtCheez. This object might expose an interface called IActLikeAMod and another called IAmAProgrammer...I can have access to IActLikeAMod, but I can also call IActLikeAMod::QueryInterface to give me access to the IAmAProgrammer interface which in turn exposes a load of new methods that I did have access to before

    As the functions exposed by IUnknown are generic and apply to all COM objects, the pointer needs to be cast to void to allow it to be passed back (otherwise you would need an overloaded QueryInterface for all Interfaces exposed...this would be hell)

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    76
    So should QueryInterface(const IID &iid, void **iface)
    really have the void * in brackets so its
    QueryInterface(const IID &iid, (void*)*iface)
    like a standard c type cast?

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    It works with void**. All that means is that you are passing the address of the pointer (a pointer to a pointer).

    It's not the best way to do things from a C++ point of view, but that's the way it's done in COM and as COM covers more than one laguage (Visual Basic, scripting languages, Java) it cant really be expected to adhere to C++ all then way

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    76
    Oh okay thanks mate I think I get it many thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems passing a file pointer to functions
    By smitchell in forum C Programming
    Replies: 4
    Last Post: 09-30-2008, 02:29 PM
  2. Replies: 0
    Last Post: 03-20-2008, 07:59 AM
  3. Pointer to functions
    By seizmic in forum C Programming
    Replies: 13
    Last Post: 09-08-2005, 12:56 PM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  5. Replies: 2
    Last Post: 11-22-2001, 11:22 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21