View Full Version : thinBASIC IUP
John Spikowski
01-03-2013, 02:09
*** deleted ***
ReneMiner
01-03-2013, 19:56
I'm trying to get callbacks working with thinBASIC. I'm not sure if this is even possible...
:yes:
sounds familiar...
Just some nosy questions:
What about that IUP? It's some .dll that I can find in your scriptbasic-examples-thread I guess?
And- why are the statements as in IupSetAttributes(btn1, "TITLE=Button1, EXPAND=HORIZONTAL") in quotes as it were strings?
I think that's not a good idea if you think about adding code-spell-correction one day.
And - are those string-statements maybe case-sensitive?
Edit-
What makes me wonder too:
Function_GetPtr
help says: "Returns the internal pointer of a script function or sub"
Returned pointer is thinBasic internal pointer. It has no meaning outside thinBasic.
Do not confuse it with machine or code pointers.
ReneMiner
01-03-2013, 20:06
just edited my last post- so please have another look at it
ReneMiner
01-03-2013, 20:19
Just snooped around in the helpfile because I thought it might be that thinBasic is not able to pass pointers to external functions. This is what I found about
"External function declarations"
There are still some limitations regarding type of parameters thinBasic is able to pass to external functions.
If you are in trouble, please visit thinBasic forum and leave a message regarding the problem you are facing and possibly specify an exact example. We will do our best to try to solve the problem as soon as possible.
but it's just a guess since I have no idea about the internal stuff.
Maybe you could store the result of Function_GetPtr in another variable and pass this?
ReneMiner
01-03-2013, 20:37
"Byval ih as Long" ??? Why?
ErosOlmi
01-03-2013, 20:39
Hi all,
as I said many times in this forum, unfortunately there is no way (at the moment) to pass thinBasic script function pointer simply because there is no function and there is no pointer.
When an external function ask for a function pointer or a code pointer they are referring to pointers to machine code currently running in the current process.
But thinbasic functions are not translated into machine code, they are just text (simple and damn text) parsed and interpreted on the fly by thinBasic engine.
thinBasic source code is never translated/converted/compiled into machine code of pcode or intermediate code (or whatever you call it)
thinBasic source code is just text during all the process of thinBasic source code execution.
In any case, it is some time (some months) I'm thinking about this trying to find a solution.
What I have in mind is to create a sort of "machine code alias" for each thinBasic source code function present into a script in such a way every source code function (or sub) has a machine code ghost function into thinBasic engine in such a way to pass pointer of the machine code ghost function to external libraries asking for a callback.
Sooner or later ...
ErosOlmi
01-03-2013, 20:56
Well,
thanks to you too for spending your time in showing how to use IUP library in thinBasic.
I really appreciate it :drink:
Ciao
Eros
José Roca
01-03-2013, 23:38
Ban removed.
Petr Schreiber
02-03-2013, 10:41
Hi John,
when I am in situation I need a callback in ThinBASIC, I use Oxygen module to handle this for me. Below little example:
' ---------------------------------------------------------------------------------------
' JIT Part Beginning
' ---------------------------------------------------------------------------------------
Uses "Oxygen"
DWord oxyCallback ' -- This will contain pointer to JIT function Oxygen_Callback
DWord oxyTerminate ' -- This will contain pointer to JIT function Oxygen_Terminate
String callbackCode = "
basic
Sub Oxygen_Callback CDECL ( ByVal param1 As DWord) Link #oxyCallback
Dim strMsg As String
strMsg = `Passed param = ` & Str(param1)
Print strMsg
End Sub
Sub Oxygen_Terminate() Link #oxyTerminate
terminate
End Sub
"
' -- JIT compile
O2_BASIC callbackCode
' -- Check for errors
If Len(O2_ERROR) Then
MsgBox 0, O2_ERROR, "JIT compilation failed"
Stop
End If
' -- Activate
O2_EXEC
' ---------------------------------------------------------------------------------------
' JIT Part End
' ---------------------------------------------------------------------------------------
' -- Declare functions from oxygen
Declare Sub Oxygen_Callback ( ByVal param1 As DWord)
Declare Sub Oxygen_Terminate ()
' -- Assign them address to make em callable
Declare Set Address Oxygen_Callback , oxyCallback
Declare Set Address Oxygen_Terminate, oxyTerminate
' -- Main code
Function TBMain()
MsgBox 0, "The code pointer to Oxygen_Callback is &h0" + Hex$(oxyCallback)
MsgBox 0, "The code pointer to Oxygen_Terminate is &h0" + Hex$(oxyTerminate)
Oxygen_Callback(123)
Oxygen_Terminate()
End Function
Petr
Petr Schreiber
02-03-2013, 20:46
Hi John,
you have an interesting observation, as I never noticed the dropping behavior when using OpenGL or OpenCL. But Eros will know more.
In the meantime, maybe the Library_Exists could help. Why? Help says the syntax is the following:
Library_Exists[ ( sLibName [, KeepLibLoaded ]) ]
Which means, that if you pass second parameter equal to true, it will keep the lib... well, loaded :) Never had a reason to use it so far, so cannot add more info.
Petr
Charles Pegge
03-03-2013, 00:35
This is the most complex thread I have ever seen :D
You can, in fact callback a thinBasic function from Oxygen or a module, making it possible to implement callbacks from another DLL. This old example needed a minor repair, and I have extracted the 2 thincore modular functions need to make it happen.
uses "oxygen"
'thinBasic calls Oxygen function
'Oxygen calls thinBasic function
'===============================
dim v as long
dim p1,p2,p3,p4 as long
dim src as string
src = "
'
#basic
'
'
'include "c:..\thincore.inc"
'FROM THINCORE
'----------------------------------------------------------------------------
'thinBasic_FunctionParseAndGetPtr
'----------------------------------------------------------------------------
' Parse the next token (or string expression). Check if it is a function name
' If yes, returns a PTR to internal data function
'----------------------------------------------------------------------------
Declare Function thinBasic_FunctionParseAndGetPtr _
Lib "thinCore.DLL" _
Alias "thinBasic_FunctionParseAndGetPtr" _
( _
Optional ByVal CheckIfCallBack As Long _
) As Long
'----------------------------------------------------------------------------
'thinBasic_FunctionCall_ByPtr
'----------------------------------------------------------------------------
' Call a script function and optionally returns it value (numeric or string)
' This function permits to call script functions passing parameters
' Parameters are passed using a shared memory area made by a sequence of DOUBLEs
' (8 bytes) one for each parameter to be passed.
' The paramaters memory area will be interpreted by thinBasic core engine
' depending by parameters declaration made in script.
'
' So far the following type of parameters are supported:
' - numeric parameters passed BYVAL up to DOUBLE range
'
' Example: if programmer wants to pass 2 parameters, set lModParams = 2 and setup
' a 16 bytes memory area (2 params * 8 bytes each) where to store the 2 parameter
' values.
'----------------------------------------------------------------------------
Declare Function thinBasic_FunctionCall_ByPtr _
Lib "thinCore.DLL" _
Alias "thinBasic_FunctionCall_ByPtr" _
( _
ByVal FunctionPtr As Long , _ '---PTR to script function
ByVal lModParams As Long , _ '---MANDATORY: number of params module is passing
ByVal lModParamsPtr As Long , _ '---MANDATORY: pointer to a memory area where to find parameters (8 bytes for each parameter)
Optional _ '---If no need to get back script function result value, do not indicate the next 2 parameters
ByVal ptrDOUBLE As Double Ptr , _ '---Pointer to DOUBLE that will get back script function return value in case of numeric
ByVal ptrSTR As String Ptr _ '---Pointer to a OLE32 dynamic string handle that will get back script function return value in case of string
) As Long
'
function callback_token() as long link #p2
'==========================================
function=thinBasic_FunctionParseAndGetPtr 0
end function
function callback_to(byval a as long) as long link #p3
'======================================================
dim as double d
dim as string s
dim as double p(10)
p(1)=42
s="!!"
thinBasic_FunctionCall_ByPtr a, 1, &p, d, s
print `thinBasic returns to Oxygen: ` s
'print `thinBasic returns to Oxygen: ` str d
function=1
end function
sub finish() link #p1
'====================
terminate
end sub
'
"
'THINBASIC/OXYGEN COMPILE & LINKAGE
'
'
'msgbox 0, o2_prep src
o2_basic src
if len(o2_error) then
msgbox 0, o2_error : stop
end if
o2_exec
declare sub finish() at p1
declare function callback_token() as long at p2
declare function callback_to(byval a as long) as long at p3
'THINBASIC CALLBACK FUNCTION
'
function targetfun(byval a as double) as string 'double
'======================================================
msgbox 0, "ThinBasic receives from Oxygen: "+str$(a)
function=str$(a*2)
end function
'THIBASIC INVOKE CALLBACK TEST
'
v=callback_token targetfun
callback_to v
finish
Charles
Charles Pegge
03-03-2013, 01:27
This is a question for Eros, as to how thincore handles asynchronous calls requesting the interp/execution of a thinbasic function. They are going to be quite expensive callbacks anyway.
Another trick would be to use shared variables, such as mouse position x,y. Could run the GUI is a separate thread, with Oxygen fielding the callbacks, so that thinBasic can run independently and just monitor the shared state variables.
ErosOlmi
05-03-2013, 22:56
John I'm interested but I have no time to be online as much as I would like.
Among other quite big project I'm currently working on a Microsoft CRM project that is absorbing all my daily seconds.
Anyway, can you please be so kind to let me know where I can download IUP dlls?
I've downloaded them from http://www.tecgraf.puc-rio.br/iup/
but iupcontrols.dll continues to say CD.DLL is missing blocking during dll load.
I'm not able to find CD.DLL library. Can you drive me into direction?
ErosOlmi
05-03-2013, 23:15
Ok found.
iupcontrols.dll is dependent from:
cd.dll (436 Kb)
iupcd.dll (44 Kb)
freetype6.dll (464 Kb)
zlib1.dll (81 Kb)
iup.dll (487 Kb)
Anyway thinBasic String handling for passing strings to external libs is not broken at all otherwise you would have GPF since the first call.
ErosOlmi
05-03-2013, 23:45
thinBasic uses OLE32 dynamic string (BSTR) for not fixed len strings.
When passing BSTR string to external functions having a parameter defined as
BYVAL anyString AS STRING
a temporary BSTR string is created and a pointer to the first byte of that string is pushed into the stack.
This is equivalent of passing a STRPTR of the string
void IupSetAttribute (Ihandle* ih, const char* name, const char* value);
Here name and value are just pointers to null terminated strings (sequence of bytes) so your declaration of
Declare Function IupSetAttribute Lib "iup.dll" Alias "IupSetAttribute" (ByVal ih As Long, ByVal atrb_name As String, ByVal atrb_value As String)
is ok to me, in fact
IupSetAttribute(dlg, "TITLE", $DQ & "thinBasic Editable Grid" & $DQ)
is just working fine
Anyway, I'm checking.
There are strange dependencies between all the different DLLs need to load IUP system.
ErosOlmi
06-03-2013, 00:34
Problem found!
It is the way IUP internally stores string attributes.
IUP has two functions: IupSetAttribute and IupStoreAttribute
IupSetAttribute does not use the passed strings as info but instead it used the passed pointers to string and reference to them. In few words he wants the main application to store strings by itself and IUP will reference to them. It is a strange behave but understandable because it used strings for all its commands so making a reference to them will save some memory
IupStoreAttribute instead accepts strings as parameters and internally makes a copy of them. This is the usual behave
http://www.tecgraf.puc-rio.br/iup/en/func/iupstoreattribute.html
http://www.tecgraf.puc-rio.br/iup/en/func/iupsetattribute.html
So uses IupStoreAttribute and NOT IupSetAttribute
' IUP Editable Grid (Matrix) Control Example
Declare Function IupOpen Lib "iup.dll" Alias "IupOpen" (ByVal ArgC As Long, ByVal ArgV As Long) As Long
Declare Function IupCreate Lib "iup.dll" Alias "IupCreate" (ByVal ctlname As String) As Long
Declare Sub IupSetAttribute Lib "iup.dll" Alias "IupSetAttribute" (ByVal ih As DWord, ByVal atrb_name As String, ByVal atrb_value As String)
Declare Sub IupStoreAttribute Lib "iup.dll" Alias "IupStoreAttribute" (ByVal ih As DWord, ByVal atrb_name As String, ByVal atrb_value As String)
Declare Function IupAppend Lib "iup.dll" Alias "IupAppend" (ByVal to_ih As Long, ByVal from_ih As Long ) As Long
Declare Function IupShow Lib "iup.dll" Alias "IupShow" (ByVal ih As Long) As Long
Declare Function IupMainLoop Lib "iup.dll" Alias "IupMainLoop" () As Long
Declare Sub IupClose Lib "iup.dll" Alias "IupClose" ()
Declare Function IupControlsOpen Lib "iupcontrols.dll" Alias "IupControlsOpen" () As Long
Declare Function IupMatrix Lib "iupcontrols.dll" Alias "IupMatrix" (ByVal cbttpe As String) As Long
Dim dlg, mx As DWord
IupOpen(0, 0)
dlg = IupCreate("dialog")
IupStoreAttribute(dlg, "TITLE", "thinBasic Editable Grid")
IupControlsOpen()
mx = IupMatrix("")
IupStoreAttribute(mx, "NUMCOL", "20")
IupStoreAttribute(mx, "NUMLIN", "30")
IupStoreAttribute(mx, "NUMCOL_VISIBLE","2")
IupStoreAttribute(mx, "NUMLIN_VISIBLE","3")
IupStoreAttribute(mx, "0:0","Inflation")
IupStoreAttribute(mx, "1:0","Medicine")
IupStoreAttribute(mx, "2:0","Food")
IupStoreAttribute(mx, "3:0","Energy")
IupStoreAttribute(mx, "0:1","January_2013")
IupStoreAttribute(mx, "0:2","February_2013")
IupStoreAttribute(mx, "1:1","5.6")
IupStoreAttribute(mx, "2:1","2.2")
IupStoreAttribute(mx, "3:1","7.2")
IupStoreAttribute(mx, "1:2","4.5")
IupStoreAttribute(mx, "2:2","8.1")
IupStoreAttribute(mx, "3:2","3.4")
IupStoreAttribute(mx, "RESIZEMATRIX","YES")
IupStoreAttribute(mx, "MARKMODE","LINCOL")
IupStoreAttribute(mx, "MULTIPLE","YES")
IupStoreAttribute(mx, "AREA","NOT_CONTINUOUS")
IupAppend(dlg, mx)
IupShow(dlg)
IupMainLoop()
IupClose()
Charles Pegge
06-03-2013, 01:22
Quick note about Bstrings/OLEstrings.
It is perfectly safe to pass a bstring to a function expecting a char*. The caller will eventually release the Bstring as part of its cleanup duties.
The problems arise when a char* string is passed to a function expecting a bstring. (the function will misread the string length).
And also when a function returns a bstring to a caller expecting a char*.
The latter is probably the main cause of memory leaks. The call appears to work perfectly, but the caller never gets to release the Bstrings returned to it.
Charles
ReneMiner
07-03-2013, 10:23
Regarding the post you made before the last one, it would be interesting if some utility that adds the "Declare"-lines would generate also the possible Callback-Subs in a way as visual basic did, before it was vbDotnet.
Anyway, I would use the UI-or IUP-module which has this feature and a handy visual designer together to create windows apps in thinBasic.
You might "steal" or "copy" vb6's visual ide that not just draws windows and controls but also generates the scripts skeleton as a complete form-unit-file with all possible callbacks - which are indeed just used if they get... used by the ...user and I will use your module (and also thinBasic then) to create window-stuff for a lifetime since vb6 doesnt install on my win8-system.
So in order to provide an easier win-gui-handling there would be needed some Main-Project-Unit which is also generated from that Utility above. The main unit would also hold the information what (Form- and other) Unit-Files belong to the project. So this Main-Project-Unit should not be a user-typed-in script and gets all "written" if the user clicks some selections in dropdown-menus or lists, selects additional files or modules to use or include and checks some checkboxes. Eventaully types in just some filename for the project.
ReneMiner
07-03-2013, 11:29
good point to start from.
Does it actually draw this during design-time?
The double-colons is Scriptbasic-Syntax?
Have you ever thought about something alike
With IUP::
'do this
'do that
End With
or some similar switch so you don't have to write IUP:: in every line
- something like that:
Import "iup.bas" To GlobalNamespace
Open()
Show(LayoutDialog(Dialog(Label("Hello World!"))))
MainLoop()
Close()
ReneMiner
08-03-2013, 00:47
I was really nosy and downloaded that iup-stuff (iup3.7-Sources.zip) from sourceforge. Now I have some folder with a lot of subfolders and files inside and I have found none inside which I can use somehow in tB. I'm not into that kind of language- whatever it is- how would some stupid basic-only user as me get that running without any complicated hassle so I could try your examples?
ReneMiner
08-03-2013, 12:00
As I wrote - in the zip I don't find any file of use for thinBasic, there's no .dll-file but just 235 *.h-files, and 480 *.c-files, about 200 *.vcxproj's a couple of *.lua and *.def and *.manifests - I have no idea how to compile that nor do I have tools such as a C-compiler
Edit: @James
So multiplatform includes windows. That's all we need. If it has more? Fine with me...does not bother
jcfuller
08-03-2013, 12:14
Why would anyone even want to use IUP with ThinBasic a Windows only interpreter?
The appeal of IUP is it's multi-platform libraries?
James
ReneMiner
09-03-2013, 09:23
So where can I get the right zip? On their site (http://www.tecgraf.puc-rio.br/iup/) links everything to the same download on this site (http://sourceforge.net/projects/iup/)- that's the one I have - and I can't find any other IUP (except Indiana University of Pennsylvania etc.)
ReneMiner
09-03-2013, 10:35
thnaks, I would never have found this without your help.
Now I'm playing around with it and I would like to checkout for the Layout-Design-Stuff (your example on previous page of this thread) - so I collected all Declare's from all code-examples but I don't get all threads about iup together since forum search complains to need more than just 3 letters to search for.
So how about some "Iup-Declarations-Names-Library-File" where one can just copy & paste the needed Declare-Statements from or could include whole file?
Is there already one available?
So all Declarations will have the same name in all thinBasic-scripts and follow one standard. It's surely confusing if one uses "thinOpen()" and another uses "iupOpen()" for the same thing.
RE: Is there anyone using IUP in Thinbasic?
I'm using IUP in Lua. I know this is a TB forum, but for the life of me I can't understand how such a great and easy to use library has such completely inpenetrable docs. Apart from the very few samples included in the IUP package, you're pretty much on you own. There's a very nice manual thats makes no sense what-so-ever. Perhaps I need to be Portugese. (Strange since 'Programming in Lua' is so very clear)
Lately I've been trolling about in Perl and TB forums trying to glean how to make things work in Lua. Which is crazy.
So John, if you have it your heart to help a thoroughly confused lua programmer understand the iup manual:
I'm a delphi refugee trying to rewrite a delphi database app in lua, so I can make it cross-platform as well as be assured it will actually run in Win7, Win8, or Win whatever.
1. How do I highlight the current row of a iupMatrix when I click on it?
2. How do I add/delete rows to the matrix when I add/delete rows to the database?
3. If I have to do refresh the data shown? Can I re-query from a db to a data_array and then refresh the matrix, or do I have to close the matrix and rebuild it?
4. Can I get incremental searching on a combobox (past the first character)?
Do you have better docs than the stuff that comes with iup?
Thanks.
Marc