Board index » delphi » Getting a method's address

Getting a method's address

Quote
In article <4p03r7$...@hume.nmia.com> Mark Iuzzolino wrote:
>I'm having a problem getting the address of procedure that is within an
>object.  Here is an example of what I'd like to do:

>type foo:object
>          Proc:procedure;
>      procedure ActualProc1;
>          procedure ActualProc2;
>     end;

>procedure foo.ActualProc1;
>begin
> Proc:=ActualProc2;
>end;

>procedure foo.ActualProc2;
>begin
> Proc:=ActualProc1;
>end;

>BP says that it is an Invalid procedure or function reference.  Everything is
>compiled far, so that is not the problem.  Does anyone know of a way to get
>around this, or to get the address of a method in an object?

A procedure and a method are not the same.  I don't know how to
explain it except in how the underlying code is put together.  
Since your procedure takes no parameter, it compiles to a simple
call instruction.  The methods however have a hidden parameter
pushed to the stack.  This is Self, which is a pointer to the
object's instance.  Definitely not the same as a procedure.

The only way that I know to handle this situation is to make the
two methods far procedures that receive a pointer to the object, or
receive the object as a var parameter.  Shortcoming is that they
can only access public variable and methods.

If you know which method is needed before the object is initialized,
you could make the method virtual and declare two separate types.
Then simply construct the proper type.

Any other ideas?

--
RDon...@gnn.com
http://members.gnn.com/rdonais/index.html
-------------------------------------------
Knowledge is one of the few things that you
can give away and still keep for yourself.

 

Re:Getting a method's address


I'm having a problem getting the address of procedure that is within an
object.  Here is an example of what I'd like to do:

type foo:object
          Proc:procedure;
          procedure ActualProc1;
          procedure ActualProc2;
         end;

procedure foo.ActualProc1;
begin
 Proc:=ActualProc2;
end;

procedure foo.ActualProc2;
begin
 Proc:=ActualProc1;
end;

BP says that it is an Invalid procedure or function reference.  Everything is
compiled far, so that is not the problem.  Does anyone know of a way to get
around this, or to get the address of a method in an object?

--TCA of NewOrder

Re:Getting a method's address


Quote
Mark Iuzzolino wrote:

> I'm having a problem getting the address of procedure that is within an
> object.  Here is an example of what I'd like to do:

> type foo:object
>           Proc:procedure;
>           procedure ActualProc1;
>           procedure ActualProc2;
>          end;

> procedure foo.ActualProc1;
> begin
>  Proc:=ActualProc2;
> end;

> procedure foo.ActualProc2;
> begin
>  Proc:=ActualProc1;
> end;

> BP says that it is an Invalid procedure or function reference.  Everything is
> compiled far, so that is not the problem.  Does anyone know of a way to get
> around this, or to get the address of a method in an object?

> --TCA of NewOrder

You can get the address of a method using the address-of operator "@".
However, when calling it, the procedure prototype needs an extra
parameter (pointer to the object, the "self" pseudo-parameter), which
you must supply in the call.

Note:  if what you want is simply parameterizing some procedure with
different operation depending on the type of an object, you should
instead make this operation a virtual method and simply call the
virtual method for that object.

- Alf

Other Threads