<< Click to Display Table of Contents >> Navigation: ThinBASIC Core Language > Script structure > External function declarations > DECLARE (General form) |
Description
Sometimes it is not possible to directly declare an external function or sub because there is no way to know in which library it is.
To solve this problem, thinBasic allows declaration of external functions or sub without having the library name and alias. thinBasic will store all function or sub information for subsequent usage.
Later, where there will be the possibility to know the function address, you can assign that address to the declared function in order to be able to call it. To assign the address, see DECLARE SET ADDRESS statement.
Syntax
DECLARE {SUB | FUNCTION} ProcName [([Arguments])] [AS ReturnType] [AT Address]
Returns
None
Parameters
Name |
Type |
Optional |
Meaning |
ProcName |
String |
No |
Is the name of the function that will be used inside you script. This name must be a unique name not yet used as variable, as equate and not be a keyword or a reserved word. |
Arguments |
Yes |
List of arguments to pass to function/sub. Arguments must contain the name and the type of the parameter the function/sub expects. Each argument can be preceded by an optional BYVAL or BYREF. |
|
ReturnType |
Yes |
In case of function, the returning function type |
|
Address |
Numeric |
Yes |
Optional address where machine code of the declared function is located. This address can be set at any time using DECLARE SET ADDRESS ... |
Remarks
To be able to use the general declared function or sub, it has to be assigned a process address using DECLARE SET ADDRESS statement.
This statement is needed only for external functions or inline functions created with machine code. It has nothing to share with standard functions or subs defined inside the script source code.
Many programming languages use DECLARE statement to let the parser know about functions present inside the source code and maybe used in the code before their declaration. This is mainly done because those languages make just one single parsing pass. This is not necessary in thinBasic because thinBasic makes all the necessary steps to automatically detect functions present in source code. Do not use DECLARE statement to describe source functions.
Restrictions
See also
Examples
'---
'---Standard API functions to get a function address. Those functions are used to simulate Petr SomeDirtyAPIToTellMeHandle function
'---
Declare Function LoadLibrary Lib "KERNEL32.DLL" Alias "LoadLibraryA" (lpLibFileName As ASCIIZ) As Long
Declare Function GetProcAddress Lib "KERNEL32.DLL" Alias "GetProcAddress" (ByVal hModule As DWORD, lpProcName As ASCIIZ) As Long
'---Commented line is a standard API declare for IsCharUpper function. We will not use this way in this example but ... (see next comment)
'DECLARE FUNCTION IsCharUpper LIB 'USER32.DLL' ALIAS 'IsCharUpperA' (BYVAL cChar AS BYTE) AS LONG
'--- ...we will tell thinBasic you are defining a generic function not jet initialized (ie, library and address are missing)
Declare Function IsCharUpper (ByVal cChar As Byte) As Long
'---OK, here we start simulating the assigment of the procedure address
Dim hLib As Long
Dim hProc As Long
'---First we need to load the library from where we want the address of the function
hLib = LoadLibrary("USER32.DLL")
'---If return value is NOT zero all is ok
If hLib <> 0 Then
'---Now we try to get the address of the prodecure inside the library
hProc = GetProcAddress(hLib, "IsCharUpperA")
'---If return value is NOT zero all is ok
If hProc <> 0 Then
'---
'---Here the new thinBasic functionality. It assign a process address to a generic previously declared function allowing subsequent calling
Declare Set ADDRESS IsCharUpper, hProc
'---So we are telling thinBasic that the previous declared function 'IsCharUpper' has now its process address
'---Now we try to use the new functionality that will tell us if a char is upper or not
Dim Char As String VALUE "F"
Dim AsciiChar As Byte VALUE ASC(Char)
MSGBOX 0, "OK. IsCharUpper for char " & Char & " returned: " & IsCharUpper(AsciiChar) & " (1=true and 0= false)"
Else
MSGBOX 0, "It was not possible to get the procedure address"
End If
Else
MSGBOX 0, "It was not possible to load library"
End If