PDA

View Full Version : Delphi plugin example for Simba.[example]



CynicRus
09-11-2013, 07:10 AM
Hey all. Well, today I tried to write the plugin for Simba on the unicode version of Delphi:) Experiment was succesful finished, but when you write on Delphi(2010 ->XE5) for Lazarus, you must have to remember: string type in Lazarus = ansistring in the unicode version of Delphi. For Delphi 7, string = string in Lazarus.

Plugin example code in Delphi XE3:


library Plugin;

{ Important note about DLL memory management: ShareMem must be the
first unit in your library's USES clause AND your project's (select
Project-View Source) USES clause if your DLL exports any procedures or
functions that pass strings as parameters or function results. This
applies to all strings passed to and from your DLL--even those that
are nested in records and classes. ShareMem is the interface unit to
the BORLNDMM.DLL shared memory manager, which must be deployed along
with your DLL. To avoid using BORLNDMM.DLL, pass string information
using PChar or ShortString parameters. }

uses
System.SysUtils,
System.Classes,
math;

var
OldMemoryManager: TMemoryManager;
memisset: Boolean = False;

{$R *.res}

type T3DIntegerArray = array [0..0] of array[0..0] of array [0..0] of integer;

function HelloPlugin(s: AnsiString): AnsiString; Cdecl;
begin
result := s;
end;

function GetPluginABIVersion: Integer; Cdecl;export;
begin
Result := 2;
end;

procedure SetPluginMemManager(MemMgr : TMemoryManager); Cdecl;export;
begin
if memisset then
exit;
GetMemoryManager(OldMemoryManager);
SetMemoryManager(MemMgr);
memisset := true;
end;

procedure OnDetach; Cdecl; export;
begin
SetMemoryManager(OldMemoryManager);
end;

function GetTypeCount(): Integer; Cdecl; export;
begin
Result := 1;
end;

function GetTypeInfo(x: Integer; var sType, sTypeDef: PAnsiChar): integer; Cdecl;export;
begin
case x of
0: begin
StrPCopy(sType, 'T3DIntegerArray');
StrPCopy(sTypeDef, 'array of array of array of integer;');
end;

else
x := -1;
end;

Result := x;
end;

function GetFunctionCount(): Integer; Cdecl; export;
begin
Result := 1;
end;

function GetFunctionInfo(x: Integer; var ProcAddr: Pointer; var ProcDef: PAnsiChar): Integer;Cdecl; export;
begin
case x of
0:
begin
ProcAddr := @HelloPlugin;
StrPCopy(ProcDef, 'function HelloPlugin(s: String): String;');
end;

else
x := -1;
end;

Result := x;
end;

exports GetPluginABIVersion;
exports SetPluginMemManager;
exports GetTypeCount;
exports GetTypeInfo;
exports GetFunctionCount;
exports GetFunctionInfo;
exports OnDetach;

begin
end.



Test script code:

program new;
{$loadlib plugin.dll}
begin
WriteLn(HelloPlugin('I am Delphi-Plugin!'));
end.


Output:


Compiled successfully in 15 ms.
I am Delphi-Plugin!
Successfully executed.


Compiled version of the test plugin you may find in attachment.

Cheers,
Cynic.

Mark
09-11-2013, 08:42 AM
hehe not working under lape try again :D duplicated T3DIntegerArray

edit: Good job tho updated version on rick's http://villavu.com/forum/showthread.php?t=58815 which was just for the pascal interpreter.

CynicRus
09-11-2013, 09:28 AM
Lol, just test it in PS:)