php-ffi icon indicating copy to clipboard operation
php-ffi copied to clipboard

Closure for typedefs function pointers

Open ghostjat opened this issue 4 years ago • 1 comments

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

ghostjat avatar Nov 04 '21 04:11 ghostjat

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);

dstogov avatar Nov 08 '21 10:11 dstogov