Sex
2012-03-10 18:43:47
This is outdated as far as I now know
I just wanted to put the source code of example plugins for Lape here :).
FreePascal:
C:
Lape passes a pointer to an array of pointers for the parameters and a pointer to the result for the result.
Therefore, to access a parameter, you must first cast it as a pointer of the type you're expecting, dereference the params pointer, and then dereference the casted pointer.
I just wanted to put the source code of example plugins for Lape here :).
FreePascal:
library simpasplug;
{$mode objfpc}{$H+}
uses
Classes, SysUtils, lptypes;
{$R *.res}
procedure a(Params : PParamArray; Result : Pointer);
begin
PLongInt(Result)^ := 42;
end;
function GetFunctionCount(): Integer; stdcall; export;
begin
Result := 1;
end;
function GetFunctionInfo(x: Integer; var ProcAddr: Pointer; var ProcDef: PChar): Integer; stdcall; export;
begin
case x of
0: begin
ProcAddr := @a;
StrPCopy(ProcDef, 'function a(): integer;');
end;
else
x := -1;
end;
Result := x;
end;
exports GetFunctionCount;
exports GetFunctionInfo;
initialization
end.This Pascal plugin uses the lptypes unit which is in lape (lptypes = lape types). So you'll have to add it to your project if you want to compile the above plugin. If you don't want to add the type, you can declare your own types. TParamArray = array[word] of Pointer, PParamArray = ^TParamArray, and PLongInt should already be defined. If not, PLongInt = ^LongInt. C:
#include <string.h>
static void __fastcall a(void **params, void *result) {
*(int *)(result) = 42;
}
static int __stdcall GetFuncCount(void) {
return 1;
}
static int __stdcall GetFuncInfo(int x, void **addr, char **def) {
switch (x) {
case 0:
*addr = (void *)&a;
strcpy(*def, "function a(): integer;");
break;
default:
x = -1;
}
return x;
}
int GetFunctionCount(void) __attribute__((alias("GetFuncCount@0")));
int GetFunctionInfo(int x, void **addr, char **def) __attribute__((alias("GetFuncInfo@12")));
I don't really know C, I just used the example plugin provided by Dgby and converted it to the way Lape uses it. I did have one problem though, Lape didn't like the __stdcall calling convention and Niels suggested I use __fastcall and that worked :).Lape passes a pointer to an array of pointers for the parameters and a pointer to the result for the result.
Therefore, to access a parameter, you must first cast it as a pointer of the type you're expecting, dereference the params pointer, and then dereference the casted pointer.