How to create a plugin for Simba
First, you will need to obtain the 32-Bit version of Lazarus + fpc 2.4.3 (Obviously download a later version if available).
Once you have that installed and setup, your environment should look similar to this:
Now go to File -> New ... and select Library.
You should see this:
Next go to File -> Save As and save the file under the name you want to call your plugin.
For example, if I save my plugin as "TestPlugin" you should see the code in the editor change to this:
Notice "library Project1" changed to "library TestPlugin".
Next we need to add "SysUtils" to the uses declaration:
If it is needed you can add other items to the uses declaration, such as "math".
Now we will add your functions. Example:
Code:
function Internal_Func: String;
begin
Result := 'Internal_Func';
end;
function Test_Func(var T: String): Integer; register;
begin
T := Internal_Func;
Result := 123;
end;
procedure Test_Proc(var T: String); register;
begin
T := 'Test_Proc';
end;
Internal_Func will be used within the plugin only, whilst Test_Func and Test_Proc will be exported.
Once you have added your functions you need to export them. This can be done with the following three functions.
1:
Code:
function GetFunctionCount(): Integer; stdcall; export;
begin
Result := 2;
end;
This is the amount of functions we are exporting.
2:
Code:
function GetFunctionCallingConv(x : Integer) : Integer; stdcall; export;
begin
Result := 0;
case x of
0..1 : Result := 1;
end;
end;
Adjust 0..1 to the amount of functions you are exporting. For example, if I had 4 functions I would change it to:
Code:
function GetFunctionCallingConv(x : Integer) : Integer; stdcall; export;
begin
Result := 0;
case x of
0..3 : Result := 1;
end;
end;
3:
Code:
function GetFunctionInfo(x: Integer; var ProcAddr: Pointer; var ProcDef: PChar): Integer; stdcall; export;
begin
case x of
0:
begin
ProcAddr := @Test_Func;
StrPCopy(ProcDef, 'function Test_Func(var T: String): Integer;');
end;
1:
begin
ProcAddr := @Test_Proc;
StrPCopy(ProcDef, 'procedure Test_Proc(var T: String)');
end;
else
x := -1;
end;
Result := x;
end;
When adding your own functions change the value of "ProcAddr" to the name of your function without parametres, and in StrPCopy use the full name of the function with parametres.
Lastly, you need to finish the plugin with:
Code:
exports GetFunctionCount;
exports GetFunctionInfo;
exports GetFunctionCallingConv;
begin
end.
Here is an example of what your finished plugin could look like:
Code:
library TestPlugin;
{$mode objfpc}{$H+}
uses
Classes, sysutils
{ you can add units after this };
{$R *.res}
function Internal_Func: String;
begin
Result := 'Internal_Func';
end;
function Test_Func(var T: String): Integer; register;
begin
T := Internal_Func;
Result := 123;
end;
procedure Test_Proc(var T: String); register;
begin
T := 'Test_Proc';
end;
function GetFunctionCount(): Integer; stdcall; export;
begin
Result := 2;
end;
function GetFunctionCallingConv(x : Integer) : Integer; stdcall; export;
begin
Result := 0;
case x of
0..1 : Result := 1;
end;
end;
function GetFunctionInfo(x: Integer; var ProcAddr: Pointer; var ProcDef: PChar): Integer; stdcall; export;
begin
case x of
0:
begin
ProcAddr := @Test_Func;
StrPCopy(ProcDef, 'function Test_Func(var T: String): Integer;');
end;
1:
begin
ProcAddr := @Test_Proc;
StrPCopy(ProcDef, 'procedure Test_Proc(var T: String);');
end;
else
x := -1;
end;
Result := x;
end;
exports GetFunctionCount;
exports GetFunctionInfo;
exports GetFunctionCallingConv;
begin
end.
Once you are finished, go ahead and press Ctrl - F9, or Run -> Build. This will compile the plugin. If everything was correct you should see this:
Finally, browse to the folder where you saved the plugin and locate the dll. Copy and Paste it to Simba/Plugins.
An example of using your plugin in Simba:
Simba Code:
program SimbaPluginTest;
{$LoadLib TestPlugin}
var
t: String;
begin
Writeln(Test_Func(t));
Writeln(t);
Test_Proc(t);
Writeln(t);
end.
Replacing "TestPlugin" with the name of your plugin.
Running this produces:
Code:
Compiled succesfully in 15 ms.
123
Internal_Func
Test_Proc
Successfully executed.
Congratulations, you have created a basic plugin for Simba
Errors
If you receive the following error message in Simba:
Code:
Exception in Script: Error loading plugin
This means you have compiled your plugin with a 64-Bit compiler, whilst using a Simba compiled in 32-Bit.
Thanks for reading.