MOD EDIT: Please note that although this tutorial was designed for SCAR, it still applies to Simba. However, you will need to use Simba's alternative plugin scheme and if you are using Delphi 2009+ (instead of FreePascal/earlier Delphi versions), ensure you pass all string types as ANSI (AnsiString, PAnsiChar, etc). Please refer to the Simba documentation on plugins for more info.
How to make a Form inside a dll and Load it into SCAR
The reason i'm making this tutorial is because i think forms are an important part of scripting, and i think you can learn alot from them.
NOTE. This is my first tutorial and i might have forgotten some stuff
Requirements:
Delphi 7 (Maybe other versions i use delphi 7 though ).
Some Delphi knowledge.
Small Amount of SCAR Knowledge.
Lets Begin:
Note. This is just an example of the stuff you can make i decided to make a calculator.
First, Open delphi 7, go to File > New > Other, then dll wizard. Now go to File > New > Form, and save it somewhere you will remember.
Building the Form:
Make two edit box's to your form and name them CalcTxt and CalcTxt2. Now add five button's to the Form and name them AddBtn, MultBtn, MinusBtn, DivideBtn, AnswerBtn and give them the caption's, +, x, -, / and =
Now you have your Form ready you can start coding. Go to the code page thing and at the top under var add: (Make sure your on the Form code tab) then go back to the Form and Double click on AddBtn and add the following code:
SCAR Code:
var
Add, Add2: Integer;
begin
Add := StrToInt(CalcTxt.Text);
Add2 := StrToInt(CalcTxt2.Text);
Answer := Add + Add2;
Between procedure TForm1.AddBtnClick(Sender: TObject); and end;
If you do not understand this, please read some beginner or intermediate tutorials. Now back onto the subject, Double click on MultBtn and Add the following code:
SCAR Code:
var
Mult, Mult2: Integer;
begin
Mult := StrToInt(CalcTxt.Text);
Mult2 := StrToInt(CalcTxt2.Text);
Answer := Mult * Mult2;
Between procedure TForm1.MultBtnClick(Sender: TObject); and end;
Then, Double click on MinusBtn and Add:
SCAR Code:
var
Minus, Minus2: Integer;
begin
Minus := StrToInt(CalcTxt.Text);
Minus2 := StrToInt(CalcTxt2.Text);
Answer := Minus - Minus2;
Between procedure TForm1.MinusBtnClick(Sender: TObject); and end;
Then, Double click on DivideBtn and Add:
SCAR Code:
var
Divide, Divide2: Integer;
begin
Divide := StrToInt(CalcTxt.Text);
Divide2 := StrToInt(CalcTxt2.Text);
Answer := Divide div Divide2;;
Between procedure TForm1.DivideBtnClick(Sender: TObject); and end;
And Finally double click on AnswerBtn and Add:
SCAR Code:
ShowMessage('Answer is: '+ IntToStr(Answer));
Between procedure TForm1.AnswerBtnClick(Sender: TObject);
begin and end;
That is all the coding for the Form, now we need to call the Form from the dll, so go to the code page, and at the top you should see two tabs, click on the first one (the one with the dll code in it), and add this procedure:
SCAR Code:
procedure ShowForm;
begin
Form1 := TForm1.Create(nil);
Form1.ShowModal;
end;
This calls the Form with all of its procedures, feel free to mess around with it
Now add the necessary Scar functions so Scar can load the ShowForm procedure
SCAR Code:
function GetFunctionCount(): Integer; stdcall; export;
begin
Result := 1;
end;
And:
SCAR Code:
function GetFunctionInfo(x: Integer; var ProcAddr: Pointer; var ProcDef: PChar): Integer; stdcall;
begin
case x of
0:
begin
ProcAddr := @ShowForm;
StrPCopy(ProcDef, 'procedure ShowForm;');
end;
else
x := -1;
end;
Result := x;
end;
And:
SCAR Code:
exports GetFunctionCount;
exports GetFunctionInfo;
This exports the function.
Now save it and go to Project > Build. Now go to the directory where you saved the project and you should see the name of your file .dll put it into the plugins folder in the scar directory. Now go into scar and type:
Press run and you should see your Form(Calculator) and all of its components. Hope this helps.
Sorry if my grammar is a bit wierd lol.
Also i may have forgotten some stuff its my first tutorial
Final code of Form:
SCAR Code:
unit TutCalc;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
CalcTxt: TEdit;
AnswerBtn: TButton;
CalcTxt2: TEdit;
MultBtn: TButton;
AddBtn: TButton;
DivideBtn: TButton;
MinusBtn: TButton;
procedure AddBtnClick(Sender: TObject);
procedure MultBtnClick(Sender: TObject);
procedure MinusBtnClick(Sender: TObject);
procedure DivideBtnClick(Sender: TObject);
procedure AnswerBtnClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
Answer: Integer;
implementation
{$R *.dfm}
procedure TForm1.AddBtnClick(Sender: TObject);
var
Add, Add2: Integer;
begin
Add := StrToInt(CalcTxt.Text);
Add2 := StrToInt(CalcTxt2.Text);
Answer := Add + Add2;
end;
procedure TForm1.MultBtnClick(Sender: TObject);
var
Mult, Mult2: Integer;
begin
Mult := StrToInt(CalcTxt.Text);
Mult2 := StrToInt(CalcTxt2.Text);
Answer := Mult * Mult2;
end;
procedure TForm1.MinusBtnClick(Sender: TObject);
var
Minus, Minus2: Integer;
begin
Minus := StrToInt(CalcTxt.Text);
Minus2 := StrToInt(CalcTxt2.Text);
Answer := Minus - Minus2;
end;
procedure TForm1.DivideBtnClick(Sender: TObject);
var
Divide, Divide2: Integer;
begin
Divide := StrToInt(CalcTxt.Text);
Divide2 := StrToInt(CalcTxt2.Text);
Answer := Divide div Divide2;
end;
procedure TForm1.AnswerBtnClick(Sender: TObject);
begin
ShowMessage('Answer is: '+ IntToStr(Answer));
end;
end.
Final code of dll:
SCAR Code:
library Tut;
uses
SysUtils,
Classes,
TutCalc in 'TutCalc.pas' {Form1};
{$R *.res}
procedure ShowForm;
begin
Form1 := TForm1.Create(nil);
Form1.ShowModal;
end;
function GetFunctionCount(): Integer; stdcall; export;
begin
Result := 1;
end;
function GetFunctionInfo(x: Integer; var ProcAddr: Pointer; var ProcDef: PChar): Integer; stdcall;
begin
case x of
0:
begin
ProcAddr := @ShowForm;
StrPCopy(ProcDef, 'procedure ShowForm;');
end;
else
x := -1;
end;
Result := x;
end;
exports GetFunctionCount;
exports GetFunctionInfo;
begin
end.
I'v also included all the dll and project files
P.S I don't know if this is usefull. I hope it is though
- Quickmarch