Log in

View Full Version : .dll and types?



bg5
04-25-2012, 10:15 PM
Simba doesn't see my dll plugin ,since I used custom type (TIntegerArray). How to fix that ?

This is my dll file:

library Mylib;


uses
FastShareMem,
SysUtils,
Classes,
Windows,
Graphics;

{$R *.res}


Function MiddleBox(X1, Y1, X2, Y2 : Integer): TPoint; stdcall;
Begin
Result.X := (X1 + X2) Shr 1;
Result.Y := (Y1 + Y2) Shr 1;
End;

type
TIntegerArray = array of integer;
T2dIntegerArray = array of TIntegerArray;

Function BmpToTIA(DC : integer): T2dIntegerArray; Stdcall;
Var
I, J ,w ,h : Integer;
B : TBitmap;
Begin
try
B := TBitmap.Create;
B.Canvas.Handle := DC;
w := B.Width;
h := B.Height;
SetLength(Result,w);
For I:= 0 to w-1 Do
SetLength(Result[I],h);
For I := 0 to w-1 Do
For J := 0 to h-1 Do
Result[I][J] := B.Canvas.Pixels[I, J];
B.Free;
except
writeln('Exeption in BmpToTIA');
end;
End;

function GetFunctionCount(): Integer; stdcall; export;
begin
Result := 2;
end;

function GetFunctionInfo(x: Integer; var ProcAddr: Pointer; var ProcDef: PChar): Integer; stdcall;
begin
case x of
0:
begin
ProcAddr := @MiddleBox;
StrPCopy(ProcDef, 'function MiddleBox(X1, Y1, X2, Y2 : Integer): TPoint;');
end;
1:
begin
ProcAddr := @BmpToTIA;
StrPCopy(ProcDef, 'Function BmpToTIA(DC : integer): T2dIntegerArray');
end;
else
x := -1;
end;
Result := x;
end;

exports GetFunctionCount;
exports GetFunctionInfo;

end.

Sex
04-25-2012, 10:16 PM
http://villavu.com/forum/showthread.php?p=974474#post974474 :).

bg5
04-25-2012, 10:17 PM
Thx ,you are fast :)

Everything is working now.

bg5
05-06-2012, 12:08 AM
After all ,I have some trouble. I followed template ,but I can't get it to work:

library test;

uses
FastShareMem,
SysUtils,
Classes,
Windows,
Graphics;

{$R *.res}

type
TIntegerArray = array of integer;
T2dIntegerArray = array of TIntegerArray;

function GetHigh(i : TIntegerArray) : integer;
var
x : integer;
begin
writeln('Result is:');
x := High(i);
writeln(x);
Result := x;
end;

function GetHigh2(var i : TIntegerArray) : integer;
var
x : integer;
begin
writeln('Result is:');
x := High(i);
writeln(x);
Result := x;
end;

function GetTypeCount() : Integer; stdcall; export;
begin
Result := 2;
end;

function GetTypeInfo(x: integer; var sType, sTypeDef : string): integer; stdcall;
begin
result := x;
case (x) of
0 : begin
sType := 'TIntegerArray';
sTypeDef := 'array of integer;';
end;
1 : begin
sType := 'T2dIntegerArray';
sTypeDef := 'array of TIntegerArray;';
end
else
result := -1;
end;
end;

function GetFunctionCount(): Integer; stdcall; export;
begin
Result := 2;
end;

function GetFunctionInfo(x: Integer; var ProcAddr: Pointer; var ProcDef: PChar): Integer; stdcall;
begin
case x of
0:
begin
ProcAddr := @GetHigh;
StrPCopy(ProcDef, 'function GetHigh(i : TIntegerArray) : integer;');
end;
1:
begin
ProcAddr := @GetHigh2;
StrPCopy(ProcDef, 'function GetHigh2(var i : TIntegerArray) : integer;');
end;
else
x := -1;
end;
Result := x;
end;

exports GetFunctionCount;
exports GetFunctionInfo;
exports GetTypeCount;
exports GetTypeInfo;
begin
end.

This gives me error:
program new;
//{$I SRL/srl.simba}
{$loadlib test.dll}
var
i :Tintegerarray;
begin
i := [3,3,3,3,3];
writeln(High(i));
writeln(GetHigh(i));
writeln( GetHigh2(i)); //[Error] (11:19): Type mismatch at line 10
end.
What is odd error gives second function with var in argument ,first not.

...Then if I uncomment {$I SRL/srl.simba} another error:

Simba\Includes\SRL/SRL/core/simba.simba(90:24): Type mismatch at line 89

What's wrong with my code?

ShawnjohnSJ
05-06-2012, 12:27 AM
You must convert an integer to a String using IntToStr(); before you can write it to the console.

bg5
05-06-2012, 01:03 AM
You must convert an integer to a String using IntToStr(); before you can write it to the console.
Uhm ,you don't need ,try it.

Brandon
05-06-2012, 01:19 AM
library test;

{$mode objfpc}{$H+}

uses
FastShareMem, SysUtils, Classes, Windows, Graphics;

{$R *.res}

type
TIntegerArray = array of integer;
T2DIntegerArray = array of TIntegerArray;

function GetHigh(i: TIntegerArray): integer; Register;
var
x : integer;
begin
writeln('Result is:');
x := High(i);
writeln(x);
Result := x;
end;

function GetHigh2(var i: TIntegerArray): integer; Register;
var
x : integer;
begin
writeln('Result is:');
x := High(i);
writeln(x);
Result := x;
end;

{ EXPORTS }


procedure SetPluginMemoryManager(MemMgr : TMemoryManager); stdcall; export;
begin
SetMemoryManager(MemMgr);
end;


{ EXPORT TYPES }


function GetTypeCount(): Integer; stdcall; export;
begin
Result := 2;
end;

function GetTypeInfo(x: Integer; var sType, sTypeDef: string): integer; stdcall;
begin
case x of
0: begin
sType := 'TCharArray';
sTypeDef := 'Array of Char;';
end;
else
Result := -1;
end;
end;



{ EXPORT FUNCS }

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 := @GetHigh;
StrPCopy(ProcDef, 'Function GetHigh(i: TIntegerArray): Integer');
end;
1:
begin
ProcAddr := @GetHigh2;
StrPCopy(ProcDef, 'Function GetHigh2(var i: TIntegerArray): Integer;');
end;
else
x := -1;
end;
Result := x;
end;


//Exports
exports SetPluginMemoryManager;

//Types
exports GetTypeCount;
exports GetTypeInfo;

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

begin
end.

bg5
05-06-2012, 03:28 AM
I compiled this ,only removed {$mode objfpc}{$H+},because of "invalid compiler directive 'mode' ",and fixed GetTypeInfo. Still same errors. :/ I use Delphi7 ,maybe should I try other compiler?

Sex
05-06-2012, 05:02 AM
Use Lazarus..

ShawnjohnSJ
05-06-2012, 05:24 AM
Sorry, last time I scripted in SCAR you had to use IntToStr to convert an integer to a string.

Sex
05-06-2012, 06:21 AM
Sorry, last time I scripted in SCAR you had to use IntToStr to convert an integer to a string.

If you are just WriteLn'ing the integer by itself you do not need to convert it:
writeln(i);
If you are combining it with a string literal you do have to convert it:
writeln('i: ' + ToStr(i)); // could also use IntToStr however ToStr converts floats, bools, etc.

Wizzup?
05-06-2012, 10:59 AM
I started to work on this yesterday: http://docs.villavu.com/simba/simbaref/plugins.html although I'm not sure if it is on any interest you to.

Anyway, you should generally write plugins for Simba with lazarus if you want to directly share Pascal types (that is arrays and strings). In all other languages (Delphi included) this is not possible without dirty hacks. So FastShareMem won't help.

bg5
05-07-2012, 02:43 AM
What a shame I can't get it to work :( Still same errors :
Simba reject my dll:

Simba\Includes\SRL/SRL/core/simba.simba(90:24): Type mismatch at line 89

And when I comment {$I SRL/srl.simba} it gives type mismatch in second function(which uses var as argument).

That's my lazarus code:
library project1;
{$mode objfpc}{$H+}

uses
SysUtils, Classes, Windows, Interfaces ,Graphics ;

{$R *.res}

type
TIntegerArray = array of integer;
T2DIntegerArray = array of TIntegerArray;

function GetHigh(i: TIntegerArray): integer; Register;
var
x : integer;
begin
writeln('Result is:');
x := High(i);
writeln(x);
Result := x;
end;

function GetHigh2(var i: TIntegerArray): integer; Register;
var
x : integer;
begin
writeln('Result is:');
x := High(i);
writeln(x);
Result := x;
end;

{ EXPORTS }


procedure SetPluginMemoryManager(MemMgr : TMemoryManager); stdcall; export;
begin
SetMemoryManager(MemMgr);
end;


{ EXPORT TYPES }


function GetTypeCount(): Integer; stdcall; export;
begin
Result := 2;
end;

function GetTypeInfo(x: integer; var sType, sTypeDef : string): integer; stdcall;
begin
result := x;
case (x) of
0 : begin
sType := 'TIntegerArray';
sTypeDef := 'array of integer;';
end;
1 : begin
sType := 'T2dIntegerArray';
sTypeDef := 'array of TIntegerArray;';
end
else
result := -1;
end;
end;



{ EXPORT FUNCS }

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 := @GetHigh;
StrPCopy(ProcDef, 'Function GetHigh(i: TIntegerArray): Integer');
end;
1:
begin
ProcAddr := @GetHigh2;
StrPCopy(ProcDef, 'Function GetHigh2(var i: TIntegerArray): Integer;');
end;
else
x := -1;
end;
Result := x;
end;


//Exports
exports SetPluginMemoryManager;

//Types
exports GetTypeCount;
exports GetTypeInfo;

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

begin
end.
+ I added LCL to required packets in project inspector


btw. Delphi's output dll has 97 KB ,when Lazarus creates 13 MB ! ,lol . It push into dll a lot of unnecessary stuff!


I started to work on this yesterday: http://docs.villavu.com/simba/simbaref/plugins.html although I'm not sure if it is on any interest you to.
That's great idea ,could spare a lot of time ppl like me :)

bg5
05-08-2012, 01:50 AM
bump

Anyone can compile this code in lazarus and check if dll works? So I will know if it's problem with code or something with my system or settings.

code to check dll:
program new;
{$I SRL/srl.simba}
{$loadlib test.dll}
var
i :Tintegerarray;
begin
i := [3,3,3,3,3];
writeln(High(i));
writeln(GetHigh(i));
writeln( GetHigh2(i));
end.

Frement
05-08-2012, 01:57 AM
Don't you have to export GetTypeInfo() too?

Brandon
05-08-2012, 01:57 AM
bump

Anyone can compile this code in lazarus and check if dll works? So I will know if it's problem with code or something with my system or settings.



I don't have lazarus installed atm but when I used to make plugins for encryption, I remember having to strip them.

http://villavu.com/forum/showthread.php?p=902012#post902012

This will shrink it from that 13mb to around 0.4 or something close to 1mb.. Can't remember. Also I think you need LibMML includes :S See this for my old plugin examples: http://villavu.com/forum/showthread.php?t=69447

bg5
05-08-2012, 02:33 AM
This will shrink it from that 13mb to around 0.4 or something close to 1mb.. Can't remember. Also I think you need LibMML includes :S See this for my old plugin examples: http://villavu.com/forum/showthread.php?t=69447

Thx ,It's awesome.

LibMML ,you mean MufasaTypes unit?

@E:
I added some units you have in your plugin.
SysUtils, Classes, Windows, Interfaces ,Graphics ,
Forms, Dialogs, LCLType, Controls, IniFiles, MufasaTypes ;

Still same errors :/

bg5
05-10-2012, 01:04 AM
Ok...so I resolved one issue with type mismatch - instead of exporting types I included MufasaTypes unit.
But i have still problem with arrays:
http://puu.sh/tSuy

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

uses
Classes,SysUtils, FileUtil, Interfaces ,MufasaTypes;

type
T3DIntegerArray = array of T2DIntegerArray;
T4DIntegerArray = array of T3DIntegerArray;

function GetHigh(i: TIntegerArray): integer; Register;
var
x : integer;
begin
writeln('Result is:');
x := High(i);
writeln(x);
Result := x;
end;

function MakeArr( hi: integer): TIntegerArray; Register;
var
a : integer;
begin
SetLength(Result,hi+1);
for a:=0 to hi do
Result[a] := a;
end;

{ EXPORTS }


procedure SetPluginMemoryManager(MemMgr : TMemoryManager); stdcall; export;
begin
SetMemoryManager(MemMgr);
end;


{ EXPORT TYPES }

function GetTypeCount(): Integer; stdcall; export;
begin
Result := 2;
end;

function GetTypeInfo(x: Integer; var sType, sTypeDef: string): integer; stdcall; export;
begin
case x of
0: begin
sType := 'T3DIntegerArray';
sTypeDef := 'array of T2DIntegerArray;';
end;
1: begin
sType := 'T4DIntegerArray';
sTypeDef := 'array of T3DIntegerArray;';
end;
else
x := -1;
end;
Result := x;
end;
{ EXPORT FUNCS }

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 := @GetHigh;
StrPCopy(ProcDef, 'Function GetHigh(i: TIntegerArray): Integer;');
end;
1:
begin
ProcAddr := @MakeArr;
StrPCopy(ProcDef, 'function MakeArr( hi: integer): TIntegerArray;');
end;
else
x := -1;
end;
Result := x;
end;


//Exports
exports SetPluginMemoryManager;

//Types
exports GetTypeCount;
exports GetTypeInfo;

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

begin
end.