• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 133
  • Last Modified:

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?
0
cabela
Asked:
cabela
  • 3
  • 3
1 Solution
 
freterCommented:
have you tried using ThisIsAnObjectName.QueryInterface(IDesiredInterface, DesiredInterface) ?

where IDesiredInterface is something like

type
  IDesiredInterface = interface
    procedure dummy;
    procedure AndSoOn;
  end;

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 *)
end;


HTH,

</freter>
0
 
MadshiCommented:
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;
begin
  result := self;
end;

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

Regards, Madshi.
0
 
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....
0
Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

 
MadshiCommented:
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!!   :-)
0
 
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...
0
 
MadshiCommented:
It *will* work - try it out...   :-)
0
 
cabelaAuthor Commented:
Thanks Madshi, it works nice.
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now