Creates an assembly thunk which emulates a C callback function.

CreateCallbackFunc(cCallback, cReturnType, cParamTypes [, oObject [, nFlags]])

Parameters

cCallback

The name of the function to call back.

cReturnType

The datatype of the returnvalue of the C callback function.

One of:

DatatypeDescription
VOID or empty stringno returnvalue
INTEGER or LONGa signed 32bit integer
UINTEGER or ULONGan unsigned 32bit integer
SHORTa signed 16bit integer
USHORTan unsigned 16bit integer
BOOLa boolean 32bit value - 0 is mapped to .F. everything else to .T.
SINGLEa 32bit floating point value
DOUBLEa 64bit floating point value
cParamTypes

A comma seperated list of parameter types the C function expects.

Valid types:

DatatypeDescription
INTEGER or LONGa signed 32bit integer
UINTEGER or ULONGan unsigned 32bit integer
SHORTa signed 16bit integer
USHORTan unsigned 16bit integer
BOOLa boolean 32bit value - 0 is mapped to .F. everything else to .T.
SINGLE (0-6)a 32bit floating point value *
DOUBLE (0-16)a 64bit floating point value *
STRING (ANSI | UNICODE)a C style string **
INT64 (BINARY | LITERAL | CURRENCY)a signed 64 bit interger value ***
UINT64 (BINARY | LITERAL | CURRENCY)an unsigned 64 bit integer value ***

Note

* For the SINGLE and DOUBLE datatypes an optional precision value can be specified e.g.
lnCallback = CreateCallbackFunc('Foo','DOUBLE 15','INTEGER, INTEGER')
If you omit the precision value the default value 6 is used.

Note

** By default C style strings are marshaled as numeric pointer values.
You can get the actual string by passing the value to the ReadCString (Ansi) or ReadWString (Unicode) function.
If you additionally pass ANSI or UNICODE the string is marshaled by these functions automatically.

Note

*** By default signed and unsigned 64-bit integers are marshaled as numeric values. The callback will truncate the values
if the parameter exceeds the VFP numeric limit. If you're not 100% sure that such big numbers are never passed specify the BINARY, LITERAL
or CURRENCY modifier. BINARY marshals the parameter as a 8 byte varbinary value, LITERAL marshals the parameter as a string of digits
and CURRENCY as a currency literal.

Note

The number of parameters is limited to 27, the limit of parameters a VFP function can except.

oObjectRef (optional)

If the FoxPro method should be called back on an object reference, you can pass the object reference in this parameter.
If you want to pass the nFlags parameter but don't want to pass an object pass NULL instead.

nFlags (optional, additive)

default = CALLBACK_SYNCRONOUS

valid values:

FlagDescription
CALLBACK_SYNCRONOUSThe C callback function is called on the main Visual FoxPro thread.
For all Winapi functions e.g. EnumWindows.
CALLBACK_ASYNCRONOUS_POSTThe C callback function is called on a seperate thread, for 3rd party C DLL's that raise
events (call the C function). The callback itself is send asyncronously to the
main Visual FoxPro thread, it does not block the 3rd party thread, the event is processed when FoxPro is in a READ EVENTS state.
CALLBACK_ASYNCRONOUS_SENDThe C callback function is called on a seperate thread, for 3rd party C DLL's that raise
events (call the C function). The callback is send syncronously to the main Visual FoxPro thread,
the thread resumes after the FoxPro function returns.
CALLBACK_CDECLThe C function uses the cdecl calling convention, by default the callback function is created with the stdcall calling convention.

Note

If you specify CALLBACK_ASYNCRONOUS_POST several callbacks can interlap each other,
to circumvent this set _VFP.AutoYield to .F. - either for the
whole application or inside the FoxPro function that is used as the callback function e.g.

FUNCTION YourCallbackFunction(lnParameter1, lnParameter2)
_VFP.AutoYield = .F.
&& do your stuff ...
_VFP.AutoYield = .T.
ENDFUNC

Return Value

A pointer (numeric) to the created callback function.

Remarks

To emulate callbacks on an object the library has to make a copy of the object into a public variable.
The name of the variable is auto generated by the following scheme:

Copy code
lcVarName = "__VFP2C_CBO_" + ALLTRIM(STR(returnvalue))

This workaround is necessary cause the FoxPro LCK doesn't provide an API function to call methods of an object.
The public variable is automatically released when you unbind the message.
Although a copy of the object is made the internal object reference count is not incremented, the public copy doesn't affect your object's lifetime (scope).
The only thing you have to consider is that your own variables doesn't conflict with the above naming scheme which is very unlikely.

See Also

Reference

AsyncWaitForObject
BindEventsEx
CancelWaitForObject
CreatePublicShadowObjReference
DestroyCallbackFunc
ReleasePublicShadowObjReference
UnbindEventsEx