Solved

need help with "MethodAddress"

Posted on 2000-03-08
10
269 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 150 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from 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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Intraweb download file link ? 1 132
URL for downloading Google Chrome for Win XP 2 152
RESTRequest Parameter 4 30
Twebbrowser in Fire monkey android scrolling to bottom 1 29
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

776 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