Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- { Example based upon the SPS Plugin }
- library proj;
- {$mode objfpc}{$H+}
- {$macro on}
- {$define callconv:=
- {$IFDEF WINDOWS}{$IFDEF CPU32}cdecl;{$ELSE}{$ENDIF}{$ENDIF}
- {$IFDEF LINUX}{$IFDEF CPU32}cdecl;{$ELSE}{$ENDIF}{$ENDIF}
- }
- uses
- classes, sysutils, math
- { you can add units after this };
- type
- TCharPtrToStr = function(x: PChar): AnsiString; cdecl;
- var
- PCharToStr: TCharPtrToStr = nil;
- procedure SetStringAllocator(a: TCharPtrToStr); callconv
- begin
- PCharToStr := a;
- end;
- function TestAlloc(): String; callconv
- var
- tmp: String;
- begin
- tmp := 'hello world'+#0;
- Result := PCharToStr(@tmp[1]);
- end;
- // --------------------
- function GetPluginABIVersion: Integer; callconv export;
- begin
- Result := 2;
- end;
- procedure SetPluginMemManager(MemMgr : TMemoryManager); callconv export;
- begin
- SetMemoryManager(MemMgr);
- end;
- function GetTypeCount(): Integer; callconv export;
- begin
- Result := 2;
- end;
- function GetTypeInfo(x: Integer; var sType, sTypeDef: PChar): Integer; callconv export;
- begin
- case x of
- 0: begin
- StrPCopy(sType, '_TStringAllocator');
- StrPCopy(sTypeDef, 'function(x: PChar): AnsiString;');
- end;
- 1: begin
- //-- cdecl on 32bit | ffi_win64 = 64bit
- StrPCopy(sType, 'TStringAllocator');
- StrPCopy(sTypeDef, 'native(_TStringAllocator, ffi_cdecl);');
- end;
- else
- x := -1;
- end;
- Result := x;
- end;
- function GetFunctionCount(): Integer; callconv export;
- begin
- Result := 2;
- end;
- function GetFunctionInfo(x: Integer; var ProcAddr: Pointer; var ProcDef: PChar): Integer; callconv export;
- begin
- case x of
- 0:begin
- ProcAddr := @SetStringAllocator;
- StrPCopy(ProcDef, 'procedure SetStringAllocator(a: TStringAllocator);');
- end;
- 1:begin
- ProcAddr := @TestAlloc;
- StrPCopy(ProcDef, 'function TestAlloc(): String;');
- end;
- else
- x := -1;
- end;
- Result := x;
- end;
- exports GetPluginABIVersion;
- exports SetPluginMemManager;
- exports GetTypeCount;
- exports GetTypeInfo;
- exports GetFunctionCount;
- exports GetFunctionInfo;
- begin
- end.
- Simba test script:
- {$loadlib proj}
- function StringFromChars(chars: PChar): AnsiString;
- begin
- while chars^ <> #0 do
- begin
- Result += chars^;
- Inc(chars);
- end;
- end;
- begin
- SetStringAllocator(@StringFromChars);
- TestAlloc;
- end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement