Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

need help with "MethodAddress"

Posted on 2000-03-08
10
Medium Priority
?
285 Views
Last Modified: 2010-04-04
hi all:
i wanna get a function's address by MethodAddress, and then call it.
But, in the function, if i access a VCL(eg. form,memo), i always get a "Access violation" error. if dont access VCL, it works fine.

how to resolve it? thanks!
0
Comment
Question by:cAkk
  • 5
  • 4
10 Comments
 
LVL 1

Expert Comment

by:yk030299
ID: 2599401
pls show here your codes for us to understand your situation.
"Access voilation" means you are accessing address was not allocated to your program.
it often happened when you code that in a function:

in method
 procedure TForm1.metho1();
 begin
   //Form1.property1:=something;
   //Form1.method2();
   //something like that!!!
 end;
 ...
  var F:TForm1;
  F:=TForm1.Create(application);
  F.method1;

it means Form1 is not created but want to call it.
0
 
LVL 12

Expert Comment

by:rwilson032697
ID: 2599411
Yes, please post a code snippet where you get the error.

Cheers,

Raymond.
0
 
LVL 12

Accepted Solution

by:
rwilson032697 earned 600 total points
ID: 2599434
OK...

Methodaddress returns a pointer to the code for that published method in a class. But to call it you must be in the context of an instance of the class.

You can use a TMethod to hold the pointer to the instance data and the pointer to a methodaddress result.

Here is an example:

unit Unit18;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
   published
     procedure a;
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.a;
begin
  ShowMessage('!');
end;

procedure TForm1.Button1Click(Sender: TObject);
type
  t = procedure of object;
var
  x : tmethod;
  y : t;
begin
  x.data := @self;
  x.code := self.methodaddress('a');
  y := t(x);
  y;
end;

end.

Cheers,

Raymond.

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 12

Expert Comment

by:rwilson032697
ID: 2599438
Where I Said: "But to call it you must be in the context of an instance of the class." I really meant you need to have the context of an instance, you don't need to be in the instance itself to call it.

Cheers,

Raymond.
0
 

Author Comment

by:cAkk
ID: 2599839
i do as this:

type
  tform1=class(tform)
   ....
  published
   procedure abc;
end;

....

procedure tform1.abc;
begin
  //error occur at this sentence
  memo1.lines.add('abc');
end;

procedure tform1.buttonclick(...);
var func:procedure;
begin
  func:=MethodAddress('abc');
  func;
end;
0
 

Author Comment

by:cAkk
ID: 2599846
rwilson :

in your sample code, drag a TMemo to your form, and replace "showmessage" with "memo1.lines.add(...);"

the error will occur.

i means: if you access a VCL in the function, it dont work.
0
 
LVL 12

Expert Comment

by:rwilson032697
ID: 2602199
Whoops: I didn't need to take the address of self, just cast it to a pointer. The following works:

unit Unit6;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
   published
     procedure a;
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.a;
begin
memo1.lines.add('...');
end;

procedure TForm1.Button1Click(Sender: TObject);
type
  t = procedure of object;
var
  x : tmethod;
  y : t;
begin
  x.data := pointer(self);
  x.code := self.methodaddress('a');
  y := t(x);
  y;
end;

Cheers,

Raymond.
0
 

Author Comment

by:cAkk
ID: 2602812
rwilson: your code works. but, if i want add parameters to function "a", how to do that?

eg: procedure a(s:string)
0
 
LVL 12

Expert Comment

by:rwilson032697
ID: 2602864
You need to know what the parms are in advance, and declare the appropriate proc type and function:

   published
     procedure a(Arg : Integer);

type
  t = procedure(Arg : Integer) of object;


Cheers,

Raymond.
0
 

Author Comment

by:cAkk
ID: 2603661
i see. thanks!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question