Log in

View Full Version : Plugin help



Richard
01-18-2012, 02:40 AM
I'm about to begin developing a plugin for this new idea I'd had, but I've never previously developed a plugin before, so I thought I'd have a go at using one of my more basic functions in there. The point is very simple, it's returning nothing when it should return something.

Here is the code I'm using in the plugin:


library project1;
{$mode objfpc}{$H+}

uses
Classes, sysutils, mufasatypes
{ you can add units after this };

function IntArrayTo2D(intarray: TIntegerArray; dimlength: Integer) : T2DIntegerArray; register;
var
i: Integer;
begin
if (Length(intarray) mod dimlength) <> 0 then
exit;
SetLength(Result, Length(intarray) div dimlength);
for i := 0 to High(Result) do
SetLength(Result[i], dimlength);
for i := 0 to High(intarray) do
Result[i div dimlength][i mod dimlength] := intarray[i];
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 := @IntArrayTo2D;
StrPCopy(ProcDef, 'function IntArrayTo2D(intarray: TIntegerArray; dimlength: Integer) : T2DIntegerArray;');
end;
else
x := -1;
end;
Result := x;
end;

exports GetFunctionCount;
exports GetFunctionInfo;

begin
end.

And using IntArrayTo2D there does NOT work. However, if I then use the same code, but simply have it as a script in Simba, it works perfectly:

program SimbaPluginTest;
//{$LoadLib project1}

function IntArrayTo2D(intarray: TIntegerArray; dimlength: Integer) : T2DIntegerArray;
var
i: Integer;
begin
if (Length(intarray) mod dimlength) <> 0 then
exit;
SetLength(Result, Length(intarray) div dimlength);
for i := 0 to High(Result) do
SetLength(Result[i], dimlength);
for i := 0 to High(intarray) do
Result[i div dimlength][i mod dimlength] := intarray[i];
end;

begin
writeln(IntArrayTo2D([5,2,7,1],2));
end.

Plugin output is: "[]"
Script output is "[[5, 2], [7, 1]]"


Really have no idea where I'm going wrong.

Brandon
01-18-2012, 03:22 AM
function GetFunctionCount(): Integer; stdcall; export;
begin
Result := 1;
end;

function GetFunctionCallingConv(x : Integer) : Integer; stdcall; export;
begin
Result := 0;
case x of
0 : Result := 1;
end;
end;

function GetFunctionInfo(x: Integer; var ProcAddr: Pointer; var ProcDef: PChar): Integer; stdcall; export;
begin
case x of
0:
begin
ProcAddr := @IntArrayTo2D;
StrPCopy(ProcDef, 'function IntArrayTo2D(intarray: TIntegerArray; dimlength: Integer) : T2DIntegerArray;');
end;
else
x := -1;
end;
Result := x;
end;

exports SetPluginMemoryManager;

//Types
//exports GetTypeCount; //Un-comment if exporting types
//exports GetTypeInfo; //Same as above..

//Functions
exports GetFunctionCount;
exports GetFunctionInfo;
exports GetFunctionCallingConv;

Richard
01-18-2012, 10:36 AM
Thanks, that seems to have sorted it :)

Dgby714
01-18-2012, 10:40 AM
You can try.. If it doesn't work I can help you a little later.


function IntArrayTo2D(intarray: TIntegerArray; dimlength: Integer) : T2DIntegerArray; stdcall;

Richard
01-18-2012, 10:45 AM
You can try.. If it doesn't work I can help you a little later.


function IntArrayTo2D(intarray: TIntegerArray; dimlength: Integer) : T2DIntegerArray; stdcall;

It's fine, I actually did manage to get it working with ggzz's code, I just missed a bit that I was supposed to copy :p Thanks though

Dgby714
01-18-2012, 10:50 AM
It's fine, I actually did manage to get it working with ggzz's code, I just missed a bit that I was supposed to copy :p Thanks though

We both suggested basically the same thing, mine just used less changes.

Simba supports both stdcall and register. (stdcall is the default)
Using the function GetFunctionCallingConv you can set it to ether for each export.

nielsie95
01-18-2012, 10:57 AM
Just a little tip for the plugin: in FPC/Delphi you can pass multiple parameters to SetLength. SetLength(a, 10, 10) creates a 10x10 matrix :)