BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Using MethodAddress for dynamic execution

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi VCL Components Using
View previous topic :: View next topic  
Author Message
Pete Gajria
Guest





PostPosted: Tue Oct 28, 2003 3:15 pm    Post subject: Using MethodAddress for dynamic execution Reply with quote



Hi,
I'm trying to exec published procedures dynamically for a given component

e.g on a form i have a TEdit & a button
The problem is methodaddress always returns nil so obviously ive overlooked
something

heres the code below

any help would be really appreciated

thanks
pete


type
TExecute = procedure of object;

TForm1 = class(TForm)
....etc etc
public
{ Public declarations }
procedure ProcessText(Instance: TObject; Name: string);

implementation
uses
TypInfo;

procedure TForm1.Button1Click(Sender: TObject);
begin
ProcessText(Edit1, 'Clear');
end;


procedure TForm1.ProcessText(Instance: TObject; Name: string);
Var
Routine: TMethod;
Execute: TExecute;
Begin
Routine.Data := Pointer(Instance);
Routine.Code := Instance.ClassType.MethodAddress(Name);
if Routine.Code = nil then
begin
showmessage('failed');
Exit;
end;
Execute := TExecute(Routine);
Execute;

end;
Back to top
Marc Rohloff
Guest





PostPosted: Tue Oct 28, 2003 5:21 pm    Post subject: Re: Using MethodAddress for dynamic execution Reply with quote



On Tue, 28 Oct 2003 07:15:17 -0800, Pete
Gajria<pete.gajria (AT) clarksecurity (DOT) com> said ...
Quote:
Hi,
I'm trying to exec published procedures dynamically for a given component

e.g on a form i have a TEdit & a button
The problem is methodaddress always returns nil so obviously ive overlooked
something

Where do you define the method you want to call dynamically?

Marc

Back to top
Pete Gajria
Guest





PostPosted: Tue Oct 28, 2003 6:31 pm    Post subject: Re: Using MethodAddress for dynamic execution Reply with quote



Here

procedure TForm1.Button1Click(Sender: TObject);
begin
ProcessText(Edit1, 'Clear'); <---- Object, Method
end;


btw i reposted my earlier message in the obj pascal newsgroup
since i thought this was the wrong group to post to

my earlier msg also contains a complete code sample
ive googled & come up with similar examples so i dont understand
why methodaddress is returning nil

regards,
pete



On Tue, 28 Oct 2003 12:21:19 -0500, Marc Rohloff rohloff at bigfoot dot com

Quote:
On Tue, 28 Oct 2003 07:15:17 -0800, Pete
Gajria<pete.gajria (AT) clarksecurity (DOT) com> said ...
Hi,
I'm trying to exec published procedures dynamically for a given
component

e.g on a form i have a TEdit & a button
The problem is methodaddress always returns nil so obviously ive
overlooked something

Where do you define the method you want to call dynamically?

Marc




Back to top
Peter Below (TeamB)
Guest





PostPosted: Tue Oct 28, 2003 8:13 pm    Post subject: Re: Using MethodAddress for dynamic execution Reply with quote

In article <oprxrdzrvzajys4f (AT) newsgroups (DOT) borland.com>, Pete Gajria wrote:
Quote:
Hi,
I'm trying to exec published procedures dynamically for a given component

e.g on a form i have a TEdit & a button
The problem is methodaddress always returns nil so obviously ive overlooked
something

heres the code below

any help would be really appreciated

thanks
pete


type
TExecute = procedure of object;

TForm1 = class(TForm)
....etc etc
public
{ Public declarations }
procedure ProcessText(Instance: TObject; Name: string);

implementation
uses
TypInfo;

procedure TForm1.Button1Click(Sender: TObject);
begin
ProcessText(Edit1, 'Clear');

Clear is not a published method. In fact no VCL control I'm aware of declares
any published methods, so this kind of scheme only works with your own
classes, including forms, frames, datamodules you create in the designer.


--
Peter Below (TeamB)
Use the newsgroup archives :
http://www.mers.com/searchsite.html
http://www.tamaracka.com/search.htm
http://groups.google.com
http://www.prolix.be



Back to top
Pete Gajria
Guest





PostPosted: Tue Oct 28, 2003 8:18 pm    Post subject: Re: Using MethodAddress for dynamic execution Reply with quote

Hi Peter,
Heres what i found in the help for TEdit

Deletes all text from the edit control.
procedure Clear; virtual;

Ok so if I understand you correctly the above method is simply a public
class
member & not published... so how would one go about accessing a public
method in a dynamic manner
like i'm attempting to do

regards,
pete




Quote:

Clear is not a published method. In fact no VCL control I'm aware of
declares any published methods, so this kind of scheme only works with
your own classes, including forms, frames, datamodules you create in the
designer.


--
Peter Below (TeamB) Use the newsgroup archives :
http://www.mers.com/searchsite.html
http://www.tamaracka.com/search.htm
http://groups.google.com
http://www.prolix.be






Back to top
David Knaack
Guest





PostPosted: Tue Oct 28, 2003 8:48 pm    Post subject: Re: Using MethodAddress for dynamic execution Reply with quote

Pete Gajria wrote:
Quote:
Ok so if I understand you correctly the above method is simply a public
class
member & not published... so how would one go about accessing a public
method in a dynamic manner
like i'm attempting to do

You might consider using either Delphi Web Script (don't be mislead by
the 'web' in the name), or Innerfuse Pascal Script. DWS will require
you to wrap all the functions you wish to call, IFS can build the calls
dynamicly given the function prototype.

There are some extensions to DWS written by Mark Ericksen that make the
process of wrapping classes for use with DWS much easier.

Either of these would let you call methods with parameters.

The JCLDebugIDE might also have some possabilities.

DK


Back to top
Pete Gajria
Guest





PostPosted: Tue Oct 28, 2003 9:05 pm    Post subject: Re: Using MethodAddress for dynamic execution Reply with quote

The problem is at runtime i may not know the class of the object being
passed
so basically i wanted to check if it had implemented a particular method &
if so exec it
i dont think innerfuse or dws would work in such a situation but i will
look at them all the same



On Tue, 28 Oct 2003 14:48:28 -0600, David Knaack <davidknaack (AT) cox (DOT) net>
wrote:

Quote:
Pete Gajria wrote:
Ok so if I understand you correctly the above method is simply a public
class
member & not published... so how would one go about accessing a public
method in a dynamic manner
like i'm attempting to do

You might consider using either Delphi Web Script (don't be mislead by
the 'web' in the name), or Innerfuse Pascal Script. DWS will require you
to wrap all the functions you wish to call, IFS can build the calls
dynamicly given the function prototype.

There are some extensions to DWS written by Mark Ericksen that make the
process of wrapping classes for use with DWS much easier.

Either of these would let you call methods with parameters.

The JCLDebugIDE might also have some possabilities.

DK





Back to top
Marc Rohloff
Guest





PostPosted: Tue Oct 28, 2003 9:31 pm    Post subject: Re: Using MethodAddress for dynamic execution Reply with quote

On Tue, 28 Oct 2003 13:05:54 -0800, Pete
Gajria<pete.gajria (AT) clarksecurity (DOT) com> said ...
Quote:
The problem is at runtime i may not know the class of the object being
passed
so basically i wanted to check if it had implemented a particular method &
if so exec it
i dont think innerfuse or dws would work in such a situation but i will
look at them all the same

This is not possible.

Marc

Back to top
Marc Rohloff
Guest





PostPosted: Tue Oct 28, 2003 11:28 pm    Post subject: Re: Using MethodAddress for dynamic execution Reply with quote

Quote:
This is not possible.

OK maybe I shouldn't say that. There are two other ways you could go.

1) If you use packages for all your controls then you can go through the
packages' exports sections and look for the address of the procedure.

2) You can add Map information to your file and use that to do the look
up.

In either case you will have to go up the hierarchy of classes and check
them all (For example TEdit does not supply the clear method, TCustomEdit
does)

Also for both alternatives you would want to look at something like the
JCL Debug routines which show you how to access this information and
provide a lot of utilities to do so.

Marc

Back to top
Pete Gajria
Guest





PostPosted: Tue Oct 28, 2003 11:38 pm    Post subject: Re: Using MethodAddress for dynamic execution Reply with quote

so the point being theres no easy way to get at the public members of a
class
dynamically unless they are published ?

also theres no way to check if a class implements a proc ? whether that proc
is published or public



"Marc Rohloff" <marc rohloff at bigfoot dot com> wrote

Quote:
This is not possible.

OK maybe I shouldn't say that. There are two other ways you could go.

1) If you use packages for all your controls then you can go through the
packages' exports sections and look for the address of the procedure.

2) You can add Map information to your file and use that to do the look
up.

In either case you will have to go up the hierarchy of classes and check
them all (For example TEdit does not supply the clear method, TCustomEdit
does)

Also for both alternatives you would want to look at something like the
JCL Debug routines which show you how to access this information and
provide a lot of utilities to do so.

Marc



Back to top
Robert Cerny
Guest





PostPosted: Wed Oct 29, 2003 11:40 am    Post subject: Re: Using MethodAddress for dynamic execution Reply with quote

A pascal interpreter like the mentioned would do the job. MethodAddress
looks up published members. Interpreter has registered public methods, so it
can execute them by name, IOW it can look up if there's a registered method
for class of particular object.

--
Robert Cerny
DelphiShaman
"Pete Gajria" <pete.gajria (AT) clarksecurity (DOT) com> wrote

Quote:
The problem is at runtime i may not know the class of the object being
passed
so basically i wanted to check if it had implemented a particular method &
if so exec it
i dont think innerfuse or dws would work in such a situation but i will
look at them all the same




Back to top
Marc Rohloff
Guest





PostPosted: Wed Oct 29, 2003 12:23 pm    Post subject: Re: Using MethodAddress for dynamic execution Reply with quote

On Tue, 28 Oct 2003 15:38:20 -0800, Pete Gajria<pete (AT) gajrias (DOT) org> said
....
Quote:
so the point being theres no easy way to get at the public members of a
class dynamically unless they are published ?
Well the two other options I mentioned can access public members too. In

fact the package method will let you access the protected members and the
debug map will let you access even private members. The package exports
method also gives you an indication of the parameters.

Quote:
also theres no way to check if a class implements a proc ? whether that proc
is published or public.
If the class has RTTI enabled and publishes a method then yes

GetMethodAddress will tell you this.

Marc

Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi VCL Components Using All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.