函数指针的问题(50分)

  • 主题发起人 主题发起人 yyanghhong
  • 开始时间 开始时间
Y

yyanghhong

Unregistered / Unconfirmed
GUEST, unregistred user!
i write some code below, but it can no be compile, who knows what is wrong,
type
; PSomeTypeOfFunc = ^TSomeTypeOfFunc;
; TSomeTypeOfFunc = function (junk : integer; stuff : string) : boolean;
;....

function Func(junk : integer; stuff : string): boolean;
begin
; ; ; ;result:=false ;
end;

function MyCustomSort (BlahFunc : Pointer) : integer;
var
; AFunc ; ;: PSomeTypeOfFunc;
begin
; AFunc := BlahFunc;
; AFunc(1,'anything'); ; ; ; ; ; // can no compile
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
; ; ;MyCustomSort(@Func);
end;
 
请将PSomeTypeOfFunc改成TSomeTypeOfFunc;
function MyCustomSort (BlahFunc : Pointer) : integer;
var
; AFunc ; ;: TSomeTypeOfFunc;//改此处
begin
; AFunc := BlahFunc;
; AFunc(1,'anything'); ; ; ; ; ; // can no compile
end;
 
thanks, i ignore this,
in fact, i want to implement this function, like
CallFunc('function_name');

then the process invoke this function according the function name, do you have
any idea
 
I had solve this problem, the code is below, I use seek to linked map file to get
Function address to do it, so you have to go to project -> options -> linker,
then set mapfile to detailed. then the IDE would generate ;map file with execute file.


unit Unit1;

interface

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

type
; TForm1 = class(TForm)
; ; Button1: TButton;
; ; Label1: TLabel;
; ; procedure Button1Click(Sender: TObject);
; ; procedure FormCreate(Sender: TObject);
; ; procedure FormDestroy(Sender: TObject);
; private

; ; { Private declarations }
; public
; ; { Public declarations }
; end;


type
TActionFunc = function (ProcessId: Integer; AttributeId :Integer):boolean;

const
; MSG_UNKNOWN ; ; ; ; ; ;= 'Unknown';
; MAPFILE_M_1 ; ; ; ; ; ;= 7;
; MAPFILE_M_2 ; ; ; ; ; ;= 16;
; MAPFILE_FCT ; ; ; ; ; ;= 22;
; ADDR_LENGTH ; ; ; ; ; ;= 8;

var
; Form1: TForm1;
; FAdress: TStringS;
; MapFileBase : integer = $00400000 + $1000;

implementation

{$R *.DFM}

function Custom_Func1(ProcessId: Integer; AttributeId :Integer):boolean ;
begin
try
; ShowMessage('Function 1');
; result:=true;
except
; ; Result:=false;
end;
end;

function Custom_Func2(ProcessId: Integer; AttributeId :Integer):boolean ;
begin
try
; ShowMessage('Function 2');
; result:=true;
except
; ; Result:=false;
end;
end;

procedure RunAction(PFunc:pointer);
var
; FAction: TActionFunc;
; Fprocess: integer;
; FAttribute: integer;
begin
; ; FAction:=PFunc;
; ; FAction(Fprocess, FAttribute);
end;

procedure SetFileName(Value : TFileName);
var i, ideb, iend, i1, i2, i2b, i3 : integer;
begin
// ;if (FFileName = Value) ;then Exit;
// ;FSegments.Clear;
; FAdress.Clear;
// ;FLines.Clear;
// ;FPresent := false;
; if (Value = '') or (not FileExists(Value)) then Exit;
; with (TStringList.Create) do
; ; try
; ; ; LoadFromFile(Value);
; ; ; i1 ; := 0;
; ; ; i2 ; := 0;
; ; ; i2b ;:= 0;
; ; ; i3 ; := 0;
; ; ; ideb := 0;
; ; ; iend := Pred(Count);
; ; ; for i := ideb to iend do if (Pos('Detailed map of segments', Strings) <> 0) ; ; ; ; ; then begin i1 ;:= i; break; end;
; ; ; for i := i1 ; to iend do if (Pos(' ;Address ; ; ; ; Publics by Name', Strings) <> 0) ;then begin i2 ;:= i; break; end;
; ; ; for i := i2 ; to iend do if (Pos(' ;Address ; ; ; ; Publics by Value', Strings) <> 0) then begin i2b := i; break; end;
; ; ; for i := i2b ;to iend do if (Pos('Line numbers for ', Strings) <> 0) ; ; ; ; ; ; ; ; ;then begin i3 ;:= i; break; end;
; ; ; if (i1 = 0) or (i2b = 0) or (i3 = 0) then raise Exception.CreateFmt('%s', [Value]);
// ; ; ;for i := i1 ;to Pred(i2) do FSegments.Add(Strings);
; ; ; for i := i2b to Pred(i3) do FAdress.Add(Strings);
// ; ; ;for i := i3 ;to iend ; ; do FLines.Add(Strings);
// ; ; ;FPresent ;:= true;
// ; ; ;FFileName := Value;
; ; finally
; ; ; Free;
; ; end;
end;


function isAddress (ParamStr:string):boolean;
var
i:integer;
bPass:boolean;
NumSet: set of Char;
sTemp:string;
begin
sTemp:=trim(ParamStr);
if length(sTemp)=0 then
begin
result:=false;
exit;
end;
bPass:=true;
NumSet:=['0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'];
for i:=1 to length(sTemp) do
begin
; if not (sTemp in NumSet) then
; ;bPass:=false;
end;
result:= bPass;
end;

function GetFunctionAddr(FuncName:string):pointer;

var i, val, paddr : integer;
FProcName:string;

begin
; FProcName := MSG_UNKNOWN;
// ;FProcAddr := 0;
// ;if (not FPresent) then Exit;
; val := 0;
; with (FAdress) do
; ; for i := 0 to Pred(Count) do
; ; ; begin
; ; ; ; try
; ; ; ; ; paddr := val;
; ; ; ; ; if isAddress(Copy(Strings, MAPFILE_M_1, ADDR_LENGTH)) then
; ; ; ; ; val ; := StrToInt('$' + Copy(Strings, MAPFILE_M_1, ADDR_LENGTH));
; ; ; ; except
; ; ; ; ; continue;
; ; ; ; end;
; ; ; ; FProcName := Trim(Copy(Strings, MAPFILE_FCT, Length(Strings) - MAPFILE_FCT + 1));
; ; ; ; if FProcName=FuncName then
; ; ; ; begin
; ; ; ; ; ; Result:=Ptr(val+MapFileBase);
; ; ; ; ; ; break;
; ; ; ; end;

; ; ; end;
end;


procedure TForm1.Button1Click(Sender: TObject);
begin
; label1.caption:=inttostr(integer(@Custom_Func2)) + inttostr(integer(@Custom_Func1));
; RunAction(GetFunctionAddr('Custom_Func2'));
; RunAction(GetFunctionAddr('Custom_Func1'));
end;



procedure TForm1.FormCreate(Sender: TObject);
begin
FAdress:=TStringList.create;
SetFileName('Project1.map');
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
FAdress.Destroy;
end;

end.
 
接受答案了.
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
628
import
I
后退
顶部