<< Click to Display Table of Contents >> Navigation: ThinBASIC Core Language > Script structure > Functions/Subs > CodePtr |
Description
Retrieves the address of a proxy function acting by a bridge for a script function.
Syntax
pMem = CodePtr(FunctionName)
Returns
Number, a pointer to a proxy function
Parameters
Name |
Type |
Optional |
Meaning |
FunctionName |
No |
Name of a script function |
Remarks
CodePtr is particularly useful when it is necessary to pass the address of a script FUNCTION to external libraries that need to communicate with script function.
Because thinBasic script functions are NOT real compiled functions, a real pointer to a function is not possible. To achieve this, thinBasic Core engine has up to 30 internal compiled functions ready to be used as proxy functions. What CodePtr does is to allocate FunctionName to a specific proxy function and return its address. Every time external routine will call transfer program execution to the proxy function, it will call the associated script function.
Script functions can be defined as CDECL or SDECL or STDCALL. If nothing is specified, STDCALL will be assumed. SDECL is an alias of STDCALL
1.CDECL: specifies that the declared procedure uses the C calling convention.
Parameters are pushed on the stack from right to left, and the calling code is responsible for removing them.
CDECL should only be used when the external library requires it. Programmer should read library documentation.
2.STDCALL: is the default convention, and should be used whenever possible.
Specifies the "Standard Calling Convention" for Windows. All Windows API use this convention.
Parameters are pushed on the stack from right to left, and the called procedure is responsible for removing them.
Restrictions
There are some IMPORTANT limitations:
1.script function can have from zero to 6 BYVAL LONG or DWORD parameters
2.script function must return a LONG or DWORD value
VALID script functions that can be used with CodePtr:
1.Function MyFun() As Long
2.Function MyFun(ByVal P1 As Long) As Long
3.Function MyFun(ByVal P1 As Long, ByVal P2 As Long) As Long
4.Function MyFun(ByVal P1 As Long, ByVal P2 As Long, ByVal P3 As Long) As Long
5.Function MyFun(ByVal P1 As Long, ByVal P2 As Long, ByVal P3 As Long, ByVal P4 As Long) As Long
6.Function MyFun(ByVal P1 As Long, ByVal P2 As Long, ByVal P3 As Long, ByVal P4 As Long, ByVal P5 As Long) As Long
7.Function MyFun(ByVal P1 As Long, ByVal P2 As Long, ByVal P3 As Long, ByVal P4 As Long, ByVal P5 As Long, ByVal P6 As Long) As Long
8.Function MyFun() CDECL As Long
9.Function MyFun(ByVal P1 As Long) CDECL As Long
10.Function MyFun(ByVal P1 As Long, ByVal P2 As Long) CDECL As Long
11.Function MyFun(ByVal P1 As Long, ByVal P2 As Long, ByVal P3 As Long) CDECL As Long
12.Function MyFun(ByVal P1 As Long, ByVal P2 As Long, ByVal P3 As Long, ByVal P4 As Long) CDECL As Long
13.Function MyFun(ByVal P1 As Long, ByVal P2 As Long, ByVal P3 As Long, ByVal P4 As Long, ByVal P5 As Long) CDECL As Long
14.Function MyFun(ByVal P1 As Long, ByVal P2 As Long, ByVal P3 As Long, ByVal P4 As Long, ByVal P5 As Long, ByVal P6 As Long) CDECL As Long
Examples of INVALID script functions that cannot be used with CodePtr:
1.Function MyFun() As Double '---Not valid because it return a Double
2.Function MyFun(ByVal P1 As Double) As Long '---Not valid because it has one parameter that is not 4 bytes long (Double)
3.Function MyFun(ByVal P1 As Integer, ByVal P2 As Long) As Long '---Not valid because it has one parameter that is not 4 bytes long (Integer)
See also
Examples