我在ZGameEditor项目的脚本功能中有一个Delphi实现,在下面的文件中搜索"TExpExternalFuncCall.Execute":
http://code.google.com/p/zgameeditor/source/browse/trunk/ZExpressions.pas
在Windows(x86和x64),Linux,Android(ARM)和OS X(x86)下测试和工作.处理stdcall和cdecl调用约定.
但是libFFI可能比我的实现更通用,所以我建议采用这种方法.
这是一个在我的机器上运行的简单示例,但我不是该主题的专家。
procedure TForm4.Button1Click(Sender: TObject);
var
hmod: HMODULE;
paddr: pointer;
c1, c2, ret: cardinal;
begin
c1 := 400; //frequency
c2 := 2000; // duration
hmod := LoadLibrary('kernel32'); // Of course, the name of the DLL is taken from the script
if hmod <> 0 then
try
paddr := GetProcAddress(hmod, 'Beep'); // ...as is the name of the exported function
if paddr <> nil then
begin
// The script is told that this function requires two cardinals as
// arguments. Let's call them c1 and c2. We will assume stdcall
// calling convention. We will assume a 32-bit return value; this
// we will store in ret.
asm
push c2
push c1
call [paddr]
mov ret, eax
end;
end;
finally
FreeLibrary(hmod);
end;
end;
Run Code Online (Sandbox Code Playgroud)