Re: Instantiate abstract class
yes.. that's what I ment... (or meant)
But I don't understand. Why would a derived class of a derived class work? But a derived class would not work?!
The CConnection class is already a derived class of class (interface) IConnection and some atl class (which I think have defined the pure virtual functions).
Re: Instantiate abstract class
Quote:
Originally Posted by Tischnoetentoet
yes.. that's what I ment... (or meant)
But I don't understand. Why would a derived class of a derived class work? But a derived class would not work?!
The CConnection class is already a derived class of class (interface) IConnection and some atl class (which I think have defined the pure virtual functions).
Your original post makes it appear that CConnection has the pure virtual functions.
Regardless, you can't instantiate a class until you've provided implementations of the pure virtual functions.
Re: Instantiate abstract class
In actual fact ATL classes are NOT designed to be derived from. They are designed to be CoCreateInstance-d and therefore if you derive from them your derived class will never be used because client's won't have access to it.
Obviously this is not strictly true : you can do some clever stuff with CoGetClassObject to return the derived class but why would you want to do this ?
So the question needs to be asked : why are you trying to derive from an ATL COM-object class in the first place ? You're not really going to get any benefit from doing this.
Darwen.
Re: Instantiate abstract class
Why are you trying to instatiate a COM object?? You get an interface to a COM class and let the COM framework instatiate the class. Your code should look like this:
Code:
CComPtr<IConnection> pTemp;
pTemp.CoCreateInstance( CLSID_CConnection );
IConnection is the name of the interface and CLSID_CConection is the CLSID of the COM interface. Then call your methods off the interface. Do not try to instatiate a COM class yourself (and remove the changes you made to AddRef, Release, and QueryInterface).
Re: Instantiate abstract class
Hi,
Thanks for all the replies.
Quote:
So the question needs to be asked : why are you trying to derive from an ATL COM-object class in the first place ? You're not really going to get any benefit from doing this.
I don't want to create pointers in my client, but in my server (COM object itself). The COM object has an interface IConnection which is responsible for 1 connection. It also has an interface CCollection which is responsible for storing several connections.
That's the reason why I want to instantiate the class. It should be possible to have more connections in one COM object, isn't it???
Re: Instantiate abstract class
Hang on - CCollection is an interface ? Why didn't you make it IConnectionCollection or something.
I'm starting to get really confused here. Either CCollection is a class or it's an interface - one or the other. It can't be both. So which one is it ?
Oh and if you want more than one connection CoCreateInstance it from inside of your dll. That's the safest way of doing it.
There are other ways (like the static CComObject<>::CreateInstance) but this I would suggest is the best for you.
So why don't you have an IConnectionConnection interface to an CConnectionCollection class (created using the ATL wizard). Inside of this you'd have an array (as a member) containing IConnection interfaces. These will have been CoCreateInstanced by your CConnectionCollection class as and when it feels like it.
I really don't think you quite understand the difference between an interface and a COM object. A COM object isn't an interface. A COM object HAS interfaces.
Darwen.
Re: Instantiate abstract class
whoops!
that is a type mistake, which is not very smart of me! I am very sorry! Of course the CCollection is an interface, but it's real class name is ICollection.
I'll try the CoCreateInstance solution...
Thanks for you answer, and I am sorry I confused you a lot!
Re: Instantiate abstract class
ok, searched in the help for the function CoCreateInstance. Help says you should use this function if you want only one object on the same computer.
However, I need more instances, so I kept searching, and find the function CoGetClassObject. The help says you have to use this function when you want more instances of a class on the same computer.
So, with this information, I decided to use the CoGetClassObject function. I wrote a function AddConnection. However, the return value is always NULL.
Code:
// Declare variables
LPVOID pInterfacePointer;
// Create the object
if (CoGetClassObject(CLSID_Connection,
CLSCTX_INPROC_HANDLER,
NULL,
IID_IConnection,
&pInterfacePointer) != S_OK)
{
// It failed!
}
// Add new object to array of connections
m_arrConnections[m_iConnectionCount++] = (IConnection * )pInterfacePointer;
// Init object
m_arrConnections[m_iConnectionCount-1]->Init(ip, port, name);
The strange thing is that the function CoGetClassObject is returning S_OK, but pInterfacePointer is NULL. How is this possible?
And do you think I have the right function now?
Thanks!
Re: Instantiate abstract class
CoGetClassObject is usually used to get the IClassFactory interface for your object. This is the implementation of the class factory of your object which is responsible for creating the instances. In actual fact you're using it the wrong way : it doesn't create an instance of your object, it creates a class factory for your object.
Where'd you get the fact that you should only use CoCreateInstance once per object instance ?
Actually you can use it as many times as possible. The CoGetClassObject route is really just an optimisation : and it really only comes into force if you're creating thousands of like COM objects.
I've never used it personally and have always used to CoCreateInstance COM objects with no perceivable speed impact.
Darwen.
Re: Instantiate abstract class
Quote:
Where'd you get the fact that you should only use CoCreateInstance once per object instance ?
Quote from MSDN:
Quote:
CoCreateInstance
Creates a single uninitialized object of the class associated with a specified CLSID. Call CoCreateInstance when you want to create only one object on the local system. To create a single object on a remote system, call CoCreateInstanceEx. To create multiple objects based on a single CLSID, refer to the CoGetClassObject function.
But I'll try the CoCreateInstance functions as you say!
I'll let you know if it works!
Thanks again!
Re: Instantiate abstract class
It works perfect!
No compile errors, and I can really create instances of the IConnection interface!
Thanks a lot!
There is one last question. Do I have to delete this object myself with delete?
Re: Instantiate abstract class
You have to release the object by calling it's Release() method.
Never delete COM objects directly : call their Release() methods instead.
Darwen.
Re: Instantiate abstract class
ok, thanks you very much for your help!
I really appreciate it!