|
Navigation: thinBasic language > Script structure > External function declarations > DECLARE (General form) |
![]() ![]()
|
Description
Sometimes is not possible to directly declaring 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.
Syntax
DECLARE {SUB | FUNCTION} ProcName [([Arguments])] [AS ReturnType]
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 |
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.
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
| © 2004-2008 thinBasic. All rights reserved. | Version 1.6.0.7 | Web Site: http://www.thinbasic.com |