View Full Version : It works for me.

20-11-2011, 09:56
I tried Free Pascal (FPC Version: 2.4.2).

It is a compiler.

It can make DLLs.

I guess that means, theoretically, it could be called by thinBasic.


(I didn't see there was a forum for it at thinBasic, until I looked to upload this post.)

I haven't the vaguest notion about SDKs.

I definitely need to have a serious run with it and see if I can make a thinBasic SDK for Free Pascal in order to be able to develop thinBasic module under Free Pascal.

If it happens, it would be nice.
I think if I knew how to make a DLL and call it from thinBasic, I would be incentivized.
For me, I don't want PowerBasic, and, I don't want C.
I would like Fortran or Free Pascal - if I knew how to make an SDK, and it was easy, I would try to do one or both myself.



There are 7 documents in PDF files.
(The Run-Time Library (RTL) units reference manual, is 1739 pages.)

IDE = Lazurus

Free Pascal + Lazurus --> installer (76.2 MB)

(compiled to RAM, and run from IDE)

' code ----------------------------------------------------------------------------------------------------------------------

program test;

uses sysutils,dateutils;

// n equals one billion

t1,t2: tdatetime;


// Calculate the square roots.
for i:=1 to n do

// Write the statistics.
f:= millisecondsbetween(t2,t1)/1000;
writeln('time to calculate the square roots of 1 to ',n,':');
writeln('elapsed seconds = ',f);

// Write some square root values.
for i:=1 to 9 do
writeln(i,' ',sqrt(i));


' output --------------------------------------------------------------------------------------------------------------------

time to calculate the square roots of 1 to 1000000000:
elapsed seconds = 2.1450000000000000E+0001

1 1.0000000000000000E+0000
2 1.4142135623730950E+0000
3 1.7320508075688773E+0000
4 2.0000000000000000E+0000
5 2.2360679774997897E+0000
6 2.4494897427831781E+0000
7 2.6457513110645906E+0000
8 2.8284271247461901E+0000
9 3.0000000000000000E+0000

20-11-2011, 10:22
If you want to have an insode about thinBasic modules ...
in thinBasic Journal issue number 2 (http://www.thinbasic.com/community/showthread.php?9450-ThinBasic-Journal-Issue-2) at page 32 I started to explain what is a thinBasic module and how it works

Than go into your thinBasic installation directory and check under directory \thinBasic\SDK\
You will find SDK.ZIP file where there are examples on how to create a thinBasic module but unfortunately not for Pascal. We had for Delphi in the past.
If there will be real interest I can have a look and revamp it in some way.


20-11-2011, 16:58
there's an example on how to make a dll in the demo\win32 folder.
but it won't compile as is, the compiler does not like the string function length by itself so you need to assign it to a variable, writing it out probably would work also, here's the modified testdll.pas
the change is in the procedure p1

Copyright (c) 1998 by Pierre Muller
Win32 DLL usage example. It needs dlltest.pp
library testdll;
function GetModuleFileName(hModule:longint;lpszPath:pchar;cchPath:longint):longint;
stdcall; external 'kernel32' name 'GetModuleFileNameA';
procedure beep(ID:longint);
stdcall; external 'user32' name 'MessageBeep';
teststr : string;
procedure P1(var s:string);export;
p:array[0..255] of char;
writeln('DLL: Hello, I''m DLL ',pchar(@p));
procedure P2(x:longint);export;
writeln('DLL: Argument X=',x);
writeln('DLL: New teststr="',teststr,'"');
procedure P3(var t);export;
p : pointer;
procedure P4(x1:pointer);export;
procedure NewExit;
writeln('DLL: Exit from testdll');
P1 index 1,
P2 name 'Proc2',
P4 resident,
teststr name 'FPC_string';
writeln('DLL: HInstance ',Hinstance,' PrevInst ',Hprevinst,' DLLReason ',DLLreason,' DLLParam ',DLLparam);
teststr:='DLL init done';

20-11-2011, 21:51
Thanks Johan.

I found it.

I think I see the change,



So far, making a thinBasic module looks complicated to me.

Under the example for PowerBasic, I see 6 files called, "UserDefinedLib", with different extensions.

As of now, from my viewpoint, it looks like magic.

But many modules exist, so, apparently, people do it.


21-11-2011, 00:20
Im still not sure what is thinBasic modul...
Is this some kind of static lib?

21-11-2011, 15:11
So far, making a thinBasic module looks complicated to me.
Under the example for PowerBasic, I see 6 files called, "UserDefinedLib", with different extensions.
As of now, from my viewpoint, it looks like magic.
But many modules exist, so, apparently, people do it.

The only two important files are:
Consider it as a C header file. Is the interface exposed by thinBasic Core engine to all modules. It contains all the functions avaibale in thinCore.dll (thinBasic Core engine) and thanls to those interfaces your module functions will be able to be executed by a running script.
Is the example module created to show how to create a thinbasic module.
Just copy it into another one and personalize it

Im still not sure what is thinBasic modul...
Is this some kind of static lib?
This is one of those case reading something about is important.
I strongly suggest to read from page 32 of thinBasic Journal issue number 2.

In any case nothing strange. A thinBasic module is a standard 32bit DLL that implements 2 important functions: LoadLocalSymbols and possibly, but not mandatory, UnLoadLocalSymbols
The rest is done by the continuous interaction between Core engine (thinCore.dll) and developed module functions during script execution.

If you are interested, please give me a very easy function (described with its syntax you would like to have in thinbasic) you would like to have in an hypotetical module and I will write it for you explaining all the steps.


21-11-2011, 20:41
Very good.

I was hoping to receive such a reply from you.

I'm slowly working on things which are compiled.

This gives me hope that they will be able to be used with thinBasic.

And therefore, my motivation is increased.

I will bookmark this thread.

Thank you.

21-11-2011, 21:07

Pascal is my preferred programming language after Basic.

I'm also motivated to see if FreePascal can be used for developing thinBasic modules. It seems to have all we need to interface numbers, I need to test about dynamic string direct compatibility or some transformations are needed.

I've just downloaded Lazarus + FreePascal (http://www.lazarus.freepascal.org/)
My first step will be how to use DLLs from FreePascal. This will open the road to use thinCore.dll

I cannot promise anything at the moment because I will work on this in my spare time but I will try.


22-11-2011, 01:22
Dear Dan,

I had some time and I developed a extremely base example of a thinBasic module developed with Free Pascal.
Module implements a single function called HalfNumber whose syntax is the following:

n = HalfNumber(AnyNumericExpression) AS Extended

In order to keep things as simple as possible, for the moment I've implemented a function that expects just a numeric expression as input.
As soon as you have got the whole picture we can see how to develop a new keyword that expects additional parameters.

Attached all the needed files:

a Lazarus project you can open and compile or change as you need.
It produces the DLL module.
Attention: if you compile source code by manual, library MUST be compiled using -WR directive (Generate a relocatable library. This library can be moved To another location In memory If the ImageBase address it wants Is already In use)
a thinBasic example using such a module
Here the source code of the Free Pascal module just to have a look at it before downloading.

library FreePascalExample;

{$mode objfpc}{$H+}

Classes, Windows
{ you can add units after this };

{$R *.res}

// ------------------------------------------------------
// thinBasic_LoadSymbol_Delphi is used to declare new keywords in thinBasic language
function thinBasic_LoadSymbol_Delphi(SymbolName: string; ReturnCode: LongInt; FunctionOrSubPointer: pointer; ForceOverWrite: LongInt = 0): LongInt; stdcall; external 'thinCore.dll';
// ------------------------------------------------------

// Tell thinCore to parse a single numeric expression
// ------------------------------------------------------
function thinBasic_Parse1Number (): Extended; stdcall; external 'thinCore.dll' ;
// ------------------------------------------------------

// ------------------------------------------------------
// Ok, here we are with our first new thinBasic new keyword function
// Syntax of the function will be: n = HalfNumber(AnyNumericExpression) AS Extended
function Exec_HalfNumber(): Extended; stdcall;
MyExt: Extended;
//thinBasic_Parse1Number will parse any numeric expression it will encounter
//passing back the result of the operation
MyExt := thinBasic_Parse1Number;
//Now that we have a number, we can get its half and return result
Exec_HalfNumber := MyExt / 2;

// ------------------------------------------------------
// LoadLocalSymbols is automatically executed by thinCore
// consider LoadLocalSymbols as entry point of the module
// when "Uses" key is encountered
// LoadLocalSymbols is the place where all internal module functions
// must be declared to thinCore using "thinBasic_LoadSymbol_Delphi" function
// LoadLocalSymbols can also be used to initialize any variable or element needed by module
// LoadLocalSymbols IS MANDATORY and MUST be present in all thinBasic module
// ------------------------------------------------------
function LoadLocalSymbols(sPath: WideString): Longint; cdecl;
// messagebox(0, Pointer(sPath),'Hello World from module. Path of module is',MB_OK or MB_ICONSTOP);

// here we are telling that the module implements a new thinBasic function called "HalfNumber"
// and it is connected to Exec_HalfNumber function
thinBasic_LoadSymbol_Delphi ('HalfNumber', 10, @Exec_HalfNumber, 0);

// Just return zero
LoadLocalSymbols := 0;

// ------------------------------------------------------
// UnLoadLocalSymbols is executed by thinCore when thinBasic is going
// to unload from memory
// Use UnLoadLocalSymbols to execute any needed module clean up
// UnLoadLocalSymbols is NOT mandatory to exist
// ------------------------------------------------------
function UnLoadLocalSymbols(): Longint; cdecl;
UnLoadLocalSymbols := 0;

LoadLocalSymbols, UnLoadLocalSymbols;

Hope this can help to start.

22-11-2011, 08:26
Thank you, I downloaded the folder (I don't know how you do that stuff so fast.).

I'm already working on something, and, I want to get it to work first.

First I started it with C, then, I abandoned it and went to PowerBasic, then, I abandoned it and went to Fortran (gfortran).

Gfortran works good (with Code::Blocks, http://darmar.vgtu.lt/, multi-file projects work approximately perfectly), and, I think before long I should have something to show.

But, anyway, Toom-Cook is not the fastest way of multiplying big integers, Schonhage-Strassen is faster.

I am doing Toom-Cook instead of Schonhage-Strassen, because, I understand Toom-Cook, and, I don't understand Schonhage-Strassen.

Schonhage-Strassen uses the Fast Fourier Transform (FFT), hopefully, in the future I will be able to implement it.

Both Toom-Cook and Schonhage-Strassen function recursively, so, to me, they are not so easy to do.

I am just feeling my way along.

I want to get Toom-Cook to work so that, at least I have something to start with.

At some point, I would like to implement all of the math functions for arbitrary size large numbers (both integers and floating point), besides arithmetic, also, trig, exponential, log, etc.

Whether or not being able to calculate the sine of a number to 10,000 decimal places has practical value, people are fascinated by being able to do it.

So, the whole thing is interesting to me, it's not clear that I can do it, so, it's a challenge, like climbing a mountain.

Making it (a little at a time) into a thinBasic module, is an additional challenge.


The first language I ever used at the university to make big projects was Turbo Pascal.

There, they didn't care what language you used, they only cared about the output you got.

Then, I abandoned Turbo Pascal for Modula-2.

Modula-2 is basically Turbo Pascal with units.

Both Pascal and Modula-2 were created by Niklaus Wirth.


I think that for sure I can do everything I want to, using Free Pascal.

So, if we can get it to cooperate with thinBasic, then, I say, very good.

(Of course, once you get something to work in one language, it's pretty easy to translate it into another.)


22-11-2011, 08:31
I have switched to freepascal from delphi when I use object pascal. It is nice to see you support it Eros.

Have fun Dan.

22-11-2011, 17:47
this is great Eros, i am always wants to make a module.
i will try with lazarus or delphi 7 withiin a few days.
i suggest to make a copy of your file thinBasic_FreePascalExample.zip with a meaningful title to the pascal/delphi section so the occasional visitors searching for delphi/pascal will find it easily. it is valuable.