函数指针怎么用
函数指针使用的一个例子。
请问如果FuncA是含参数的函数
FuncA(a:Integer):Interger怎么处理?type
fProc=function:integer;function FuncA:integer;
begin
result:=1;
end;function FuncB:integer;
begin
result:=2;
end;procedure TForm1.Button1Click(Sender: TObject);
var
proc:fProc;
begin
proc:=FuncA;
showmessage(inttostr(proc));
proc:=FuncB;
showmessage(inttostr(proc));
end;
函数指针使用的一个例子。
请问如果FuncA是含参数的函数
FuncA(a:Integer):Interger怎么处理?type
fProc=function:integer;function FuncA:integer;
begin
result:=1;
end;function FuncB:integer;
begin
result:=2;
end;procedure TForm1.Button1Click(Sender: TObject);
var
proc:fProc;
begin
proc:=FuncA;
showmessage(inttostr(proc));
proc:=FuncB;
showmessage(inttostr(proc));
end;
type
TParamFun = function(Param :integer) :integer of object定义
fun1 :TParamFun写语句
函数:
function TForm1.Example(Param :integer) :integer
begin
ShowMessage(IntToStr(Param);
end;
调用
TMethod(fun1)[email protected];
TMethod(fun1).Data=Self
Fun1(1111111);
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
Function Example(Param:integer):String;
private
{ Private declarations }
public
{ Public declarations }
end;Type
TParamFun = function(Param :integer) :integer of object;
var
Form1: TForm1;
fun1 :TParamFun;
implementation{$R *.dfm}
Function TForm1.Example(Param:integer):String;
begin
Result := IntToStr(Param);
end;procedure TForm1.Button1Click(Sender: TObject);
begin
TMethod(fun1).Code := @TForm1.Example;
TMethod(fun1).Data := Self; Fun1(1111111);
end;
end.
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TTestFunObj = function(Param :integer) :string of Object; TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
P :TTestFunObj;
{ Private declarations }
public
function TestFun(Param :integer) :string;
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
begin
TMethod(P).Code := @TForm1.TestFun;
TMethod(P).Data := Self;
ShowMessage(P(1111));
end;function TForm1.TestFun(Param: integer): string;
begin
Result := IntToStr(Param);
end;end.
没错啊,我这边运行正常,变量不是全局的,是类里的
我把函数换成了CreateProcess, 调用外部函数,结果就是
Access violation at address 004E71A in module 'a.exe', Read of address 00000000.function TRunDosThread.RunDOS (const CommandLine,Dir: String; ExitCode :Dword): String;
var
HRead,HWrite:THandle;
StartInfo:TStartupInfo;
ProceInfo:TProcessInformation;
b:Boolean;
sa:TSecurityAttributes;
inS:THandleStream;
sRet:TStrings;
begin
Result := '';
FillChar(sa,sizeof(sa),0);
//设置允许继承,否则在NT和2000下无法取得输出结果
sa.nLength := sizeof(sa);
sa.bInheritHandle := True;
sa.lpSecurityDescriptor := nil;
b := CreatePipe(HRead,HWrite,@sa,0);
CheckResult(b); FillChar(StartInfo,SizeOf(StartInfo),0);
StartInfo.cb := SizeOf(StartInfo);
StartInfo.wShowWindow := SW_HIDE;
//使用指定的句柄作为标准输入输出的文件句柄,使用指定的显示方式
StartInfo.dwFlags := STARTF_USESTDHANDLES+STARTF_USESHOWWINDOW;
StartInfo.hStdError := HWrite;
StartInfo.hStdInput := GetStdHandle(STD_INPUT_HANDLE);//HRead;
StartInfo.hStdOutput := HWrite; b := CreateProcess(Nil,//lpApplicationName: PChar
PChar(CommandLine), //lpCommandLine: PChar
nil, //lpProcessAttributes: PSecurityAttributes
nil, //lpThreadAttributes: PSecurityAttributes
True, //bInheritHandles: BOOL
CREATE_NEW_CONSOLE,
nil,
PChar(Dir),
StartInfo,
ProceInfo ); CheckResult(b);
WaitForSingleObject(ProceInfo.hProcess,INFINITE);
GetExitCodeProcess(ProceInfo.hProcess,ExitCode);
// GetExitCodeProcess(ProceInfo.hProcess,ExitCode);
inS := THandleStream.Create(HRead);
if inS.Size>0 then
begin
sRet := TStringList.Create;
sRet.LoadFromStream(inS);
Result := sRet.Text;
sRet.Free;
end;
inS.Free;
CloseHandle(HRead);
CloseHandle(HWrite);
end;