16,748
社区成员
发帖
与我相关
我的任务
分享
asm
mov edi, Para
mov edx, [edi]-4
@AGAIN:
mov eax, [edi]+edx*4-4
push eax
dec edx
cmp edx, 0
jnz @AGAIN{比较不为零直接使用jnz跳转循环}
call F
mov Rst, eax
end;
type
TFunc = function (v1,v2: integer): integer;
function XX(v1,v2: integer): integer;
begin
Result := V1 + V2;
Showmessage(inttostr(result));
end;
procedure TForm1.Button1Click(Sender: TObject);
var
P: Pointer;
begin
P := @XX;
TFunc(P)(1,2);
end;
{-----------以下是测试用的Project.DLL------------------}
library Project1;
uses
SysUtils,
Classes;
function Test(V1, V2, V3, V4, V5: integer): integer;stdcall;
begin
Result := V1 + V2*V3 - V4 div V5;
end;
exports
Test;
{$R *.res}
begin
end.
{---------------以下是调用----------------}
type
TArr = array of integer;
function RunDllFunc(DllName, FuncName: string; Para: TArr): integer;
var
H: THandle;
F : function:integer;stdcall;{按楼主要求,这样声明的}
Rst: integer;
begin
H := Windows.LoadLibrary(PChar(DllName));
if H<>0 then begin
@F := GetProcAddress(H, PChar(FuncName));
end;
asm
mov edi, Para
mov edx, [edi]-4
@AGAIN:
mov eax, [edi]+edx*4-4
push eax
dec edx
cmp edx, 0
jz @DONE
jmp @AGAIN
@DONE:
call F
mov Rst, eax
end;
Result := Rst;
FreeLibrary(H);
end;
procedure TForm1.Button1Click(Sender: TObject);
var
Para : TArr;
Rst : integer;
begin
SetLength(Para, 5);
Para[0] := 2;
Para[1] := 4;
Para[2] := 5;
Para[3] := 9;
Para[4] := 3;
Rst := RunDllFunc('project1.dll', 'Test', Para);
Showmessage(InttoStr(Rst));{2 + 4*5 - 9/3 = 19}
end;
{只要Para的元素个数与DLL函数的参数个数一致,就可以了。}