如何计算函数/过程代码的大小和地址?(50分)

  • 主题发起人 zhoufujin
  • 开始时间
Z

zhoufujin

Unregistered / Unconfirmed
GUEST, unregistred user!
如何计算函数/过程代码的大小和地址?
Type
TStr=String[20];
有过程Procedure MyProc(Str1,Str2:TStr);
有函数Function MyFunc(Str1,Str2:TStr):Boolean;
Procedure MyProc(Str1,Str2:TStr);
Var
I:Integer;
begin
.
.
.
end;

Function MyFunc(Str1,Str2:TStr):Boolean;
Var
I:Integer;
begin
Result:=False;
.
.
.
Result:=True;
end;

如何得到MyProc和MyFunc的在地址和他们的代码大小?
 
很久以前我做过类似的东西,地址用Addr即可,
MyProc的大小:·@MyFunc-@MyProc
在MyFunc之后再增加一个函数如
procedure AfterMyFunc;
那么MyFunc的大小=@AfterMyFunc-@MyFunc
这几个函数必须相邻
悄悄告诉我:是不是要对这些函数加密啊?
 
如果 Procedure MyProc(Str1,Str2:TStr)和Function MyFunc(Str1,Str2:TStr):Boolean;
在Forma1中private下声明,P:pointer;
P:=Addr(MyProc)是不允许的,还有办法吗?
 
难道Form1不是你自己的吗?
你可以将取地址和尺寸的函数放到Form1所在单元,也可将MyProc升为Public
 
在同一个Form1内使用P:=Addr(MyProc)是不允许的
Type
TForm1=class(TForm)
...
Private
Procedure MyProc;
Function MyFunc:Boolean;
Function getMyProcAddress:pointer
Public
....
end;
....
Procedure MyProcedure;
.....
Procedure TForm1.getMyProcAddress:pointer
begin
Result:=Addr(MyProc);<<===不允许
Result:=Addr(MyFunc);<<===不允许
Result:=Addr(MyProcedure)<<===允许
end;

您理解了吗?
我不想用Result:=Addr(MyProcedure),因为他在Form外面声明的
不安全。
您有别的办法吗?
 
用Published方法
eturns the address of a published method.
class function MethodAddress(const Name: ShortString): Pointer;
Description
MethodAddress is used internally by the streaming system. When an event property is read from a stream, MethodAddress converts a method name, specified by Name, to a pointer containing the method address. There should be no need to call MethodAddress directly.
If Namedo
es not specify a published method for the object, MethodAddress returns nil.
 
不好意思,昨天没区分普通过程和方法
取方法的地址:
type
TMyProcMethod=procedure of object;
TMyFuncMethod=function:boolean of object;
function GetMyProcAddr(M:TMyProcMethod):pointer;
begin
Result:=TMethod(M).Code;
end;
function GetMyFuncAddr(M:TMyFuncMethod):pointer;
begin
Result:=TMethod(M).Code;
end;
 
龙丹:
请斧正:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TMyProcMethod=procedure of object;
TMyFuncMethod=function:boolean of object;
type
TForm1 = class(TForm)
Button1: TButton;
Label1: TLabel;
Label2: TLabel;
procedure Button1Click(Sender: TObject);
private
Procedure MyProc;
Function MyFunc:Boolean;
procedure AfterMyFunc;
function GetMyProcAddr(M:TMyProcMethod):pointer;
function GetMyFuncAddr(M:TMyFuncMethod):pointer;
function GetCodeSize(PB,PE:pointer):LongInt;
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
implementation
{$R *.dfm}
function TForm1.GetMyProcAddr(M:TMyProcMethod):pointer;
begin
Result:=TMethod(M).Code;
end;

function TForm1.GetMyFuncAddr(M:TMyFuncMethod):pointer;
begin
Result:=TMethod(M).Code;
end;

procedure TForm1.MyProc;
begin
Showmessage('Thank You');
Showmessage('Thank You');
end;

function TForm1.MyFunc: Boolean;
begin
Showmessage('Thank You');
Showmessage('Thank You');
Result:=True;
end;

procedure TForm1.AfterMyFunc;
begin
Showmessage('Thank You');
Showmessage('Thank You');
end;

function TForm1.GetCodeSize(PB, PE: Pointer):LongInt;
begin
Result:=LongInt(PE)-LongInt(PB);
end;

procedure TForm1.Button1Click(Sender: TObject);
Var
S:String;
begin
S:=IntToStr(GetCodeSize(GetMyProcAddr(MyProc),GetMyFuncAddr(MyFunc)));
S:=S+' '+IntToStr(GetCodeSize(GetMyFuncAddr(MyFunc),GetMyProcAddr(AfterMyFunc)));
Label1.Caption:=S;//结果44 44 为什么前指针-后指针=正值,相反的=负值?
S:=IntToStr(GetCodeSize(GetMyFuncAddr(MyFunc),GetMyProcAddr(MyProc)));
S:=S+' '+IntToStr(GetCodeSize(GetMyProcAddr(AfterMyFunc),GetMyFuncAddr(MyFunc)));
Label2.Caption:=S;//结果-44 -44
end;
end.
 
你的答案是正确的呀,再好好看看!
 
谢谢¥50全给你了
 

Similar threads

S
回复
0
查看
962
SUNSTONE的Delphi笔记
S
S
回复
0
查看
784
SUNSTONE的Delphi笔记
S
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
顶部