Board index » cppbuilder » Passing a COM interface to another COM Interface
Matt
![]() CBuilder Developer |
Sun, 16 Oct 2005 22:57:29 GMT
|
Matt
![]() CBuilder Developer |
Sun, 16 Oct 2005 22:57:29 GMT
Passing a COM interface to another COM Interface
I have created two COM objects, one in Delphi and the other in BCB 6. I
wish to pass the Delphi COM object (as an interface) to the C COM object and store it there. However I have problems storing the interface - the interface works correctly when not in a variable, i.e. C_COM_Object :: AddDevice( IDevice ADevice) FDevice -> DisplayMessage(); Quote};//C_COM_Object :: AddDevice( IDevice ADevice) { FDevice -> DisplayMessage(); //Doesn't work and throws an error Quote};//C_COM_Object :: AnotherMethod() anyone? |
Team
![]() CBuilder Developer |
Mon, 17 Oct 2005 00:31:56 GMT
Re:Passing a COM interface to another COM InterfaceQuote"Matt" <matth...@simstor.co.uk> wrote in message Quote> C_COM_Object :: AddDevice( IDevice ADevice) (DelphiInterface or TComInterface), but just a raw pointer, then you must call ADevice->AddRef() manually. Otherwise you're not incrementing the interface's reference count, and the caller may be freeing the interface after AddDevice() returns. That would explain why AnotherMethod() failed - the interface was no longer valid: C_COM_Object :: AddDevice( IDevice *ADevice) Then later on, you need to release the interface as some point: FDevice->Release(); Gambit |
Matt
![]() CBuilder Developer |
Tue, 18 Oct 2005 00:14:00 GMT
Re:Passing a COM interface to another COM InterfaceThankyou for your advice Gambit, after inserting the AddRef call it worked immediately. However I have concerns about the solution. FDevice as you correctly guessed is a raw pointer to a IDevice - each Why would this work in Delphi without the AddRef call needed but not in C? When I pass an interface to one COM object to another is just a pointer Please excuse my naivety as this is my first serious COM project so have Thanks in advance Matt. |
Team
![]() CBuilder Developer |
Tue, 18 Oct 2005 02:09:06 GMT
Re:Passing a COM interface to another COM InterfaceQuote"Matt" <matth...@simstor.co.uk> wrote in message Quote> I am certain that the IDevice has not freed itself (as the form initially 0, and is incremented everytime AddRef() is called. You need to call AddRef() to keep the reference count above 0 so the interface stays alive while you're still using it. As soon as the count drops to 0, the interface must free itself automatically. Whenever you are done using an interface, you must Release() it so the reference count decrements properly. It sounds like your C_COM_Object class is never calling FDevice->Release() when it is done with FDevice and doesn't need it anymore. It should also be mentioned that your AddDevice() method has another design So, for example: C_COM_Object::C_COM_Object() C_COM_Object::~C_COM_Object() C_COM_Object :: AddDevice(IDevice *ADevice) FDevice = ADevice; if( FDevice ) C_COM_Object::AnotherMethod() The easiest way to work around all of these reference counting issues is to class C_COM_Object : ... C_COM_Object :: AddDevice(IDevice *ADevice) C_COM_Object::AnotherMethod() Quote> Why would this work in Delphi without the AddRef call needed but not in C? handles the reference counting for you behind the scenes. Quote> When I pass an interface to one COM object to another Gambit |
Matt
![]() CBuilder Developer |
Tue, 18 Oct 2005 23:12:58 GMT
Re:Passing a COM interface to another COM InterfaceI think that I understand it now. Cheers for your time. "Remy Lebeau (TeamB)" <gambi...@yahoo.com> wrote in message Quote
|
1. COM interface returning interfaces
2. COM Problem: Using within one Interface Methode another Interface as Parameter
3. Passing ADOConnection, ADOQuery et al as interfaces to COM components
4. COM Plus straight COM interfaces don't work
5. Passing Interface pointer to a DLL + COM thoughts
6. Passing Interfaces between COM objects
7. how passing an array of variant via com interface
8. Still tring to pass a COM Interface