Articles   Members Online: 3
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to execute a method by name Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
30-Aug-02
Category
Algorithm
Language
Delphi All Versions
Views
100
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Tomas Rutkauskas

I have a reference to a TMethod (with its Data and Code pointers) and would like to 
execute the associated method. Does anyone know how I can do this?

Answer:

Here is an example that executes a method by name. Note that the method to be 
called (in the example that is SomeMethod) must be declared as published, otherwise 
MethodAddress wil return nil.


1   { ... }
2   type
3     PYourMethod = ^TYourMethod;
4     TYourMethod = procedure(S: string) of object;
5   
6   procedure TMainForm.Button1Click(Sender: TObject);
7   begin
8     ExecMethodByName('SomeMethod');
9   end;
10  
11  procedure TMainForm.ExecMethodByName(AName: string);
12  var
13    PAddr: PYourMethod;
14    M: TMethod;
15  begin
16    PAddr := MethodAddress(AName);
17    if PAddr <> nil then
18    begin
19      M.Code := PAddr;
20      M.Data := Self;
21      TYourMethod(M)('hello');
22    end;
23  end;
24  
25  procedure TMainForm.SomeMethod(S: string);
26  begin
27    ShowMessage(S);
28  end;


			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC