php-ffi
php-ffi copied to clipboard
Closure for typedefs function pointers
Hi, i am trying to call IUP toolkit lib click_cb callback function which has following def
typedef int (*IFnii)(Ihandle*, int, int);
in php
function click_cb (FFI\CData $ih, FFI\CData $l, FFI\CData $c) {
echo $lin = FFI::cast('int',$l)->cdata;
echo $col = FFI:cast('int', $c)->cdata;
};
Fatal error: Uncaught FFI\Exception: Attempt to assign an invalid callback, insufficient number of arguments
I can't guess what is wrong in your code, if you don't show it. In the following example the assignment works without problems. (I hope, you use ext/ffi bundled into PHP-7.4 or above. This git repo is outdated).
<?php
$ffi = FFI::cdef("
typedef int (*IFnii)(void*, int, int);
IFnii zend_printf;
");
function click_cb(FFI\CData $ih, FFI\CData $l, FFI\CData $c) {
echo $lin = FFI::cast('int',$l)->cdata;
echo $col = FFI::cast('int', $c)->cdata;
};
$ffi->zend_printf = "click_cb";
($ffi->zend_printf)("%d %d\n", 1, 2);