Pointer to object from interface

Is it in any way possible to get a pointer to a TCOMObject-based class from a pointer to an iterface the object implements?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

have you tried using ThisIsAnObjectName.QueryInterface(IDesiredInterface, DesiredInterface) ?

where IDesiredInterface is something like

  IDesiredInterface = interface
    procedure dummy;
    procedure AndSoOn;

and DesiredInterface is of type IDesiredInterface,
and ThisIsAnObjectName is an Object that implements the interface IDesiredInterface.

obtw: you should check wether the interface query was successfull by writing:

if SUCCEEDED(ThisIsAnObjectName.QueryInterface(IdesiredInterface, DesiredInterface)) then begin
  (* code that uses DesiredInterface *)


cabela, AFAIK, that is not possible if the object does not have a function for that.
In my base interface I've added such a function:

  function GetSelfAsTObject : TObject;

function TIBaseInterface.GetSelfAsTObject : TObject;
  result := self;

If we're talking about COM interfaces, that you didn't write yourself, I guess, you have bad luck...   :-(

Regards, Madshi.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
cabelaAuthor Commented:
to freter:

I don't need a pointer to a specific interface but a pointer to the object itself. Meaning I then can call methods of the object that are not in any interface.

to Madshi:

Yeah, that would work, but I need the interface to be Automation compatible i.e. no pointers to Delphi types.

I have source of the object, so I guess if it is possible to get a pointer to the interface from the pointer to the object, it might be possible to do it the other way, of course digging a little in the Delphi sources....
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

There is no support for that in Delphi's interface logic, nor is there support in Microsofts COM logic. Perhaps this kind of support is added somewhere in TComObject (I don't know TComObject so well), but I don't think so.

Emhh... Well, you say "I need the interface to be Automation compatible i.e. no pointers to Delphi types". What do you mean??? It was exactly your question to get the pointer to the object itself. And this object *IS* always a Delphi type for in Delphi implemented COM objects. So what do you mean? I think, you're contradicting yourself!!   :-)
cabelaAuthor Commented:
Well, you right. My problem is exactly the contradiction. I want to use the object via the interface and I want to use some methots that are not in the interface. And to get from the interface to the object which is exactly against the COM paradigm.

My idea was like getting somehow an offset to substract from the pointer to the interface in order to get the object pointer....

But the more I think about your first answer, the more I like it. I maybe could provide another interface with only the method you proposed and the first interface remains intact and the functionality is there... It might work...
It *will* work - try it out...   :-)
cabelaAuthor Commented:
Thanks Madshi, it works nice.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.