Navigation:  ThinBASIC Language > Script structure > Functions/Subs >

CodePtr

Previous pageReturn to chapter overviewNext page

 

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 4 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 LongAs Long
3.Function MyFun(ByVal P1 As LongByVal P2 As LongAs Long
4.Function MyFun(ByVal P1 As LongByVal P2 As LongByVal P3 As LongAs Long
5.Function MyFun(ByVal P1 As LongByVal P2 As LongByVal P3 As LongByVal P4 As LongAs Long
 
6.Function MyFun() CDECL As Long
7.Function MyFun(ByVal P1 As LongCDECL As Long
8.Function MyFun(ByVal P1 As LongByVal P2 As LongCDECL As Long
9.Function MyFun(ByVal P1 As LongByVal P2 As LongByVal P3 As LongCDECL As Long
10.Function MyFun(ByVal P1 As LongByVal P2 As LongByVal P3 As LongByVal P4 As LongCDECL 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 DoubleAs Long '---Not valid because it has one parameter that is not 4 bytes long (Double)
3.Function MyFun(ByVal P1 As IntegerByVal P2 As LongAs Long '---Not valid because it has one parameter that is not 4 bytes long (Integer)

 

 

See also

 

Examples