Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在C中是否可以使用dart函数作为回调函数?_Dart_Ffi - Fatal编程技术网

在C中是否可以使用dart函数作为回调函数?

在C中是否可以使用dart函数作为回调函数?,dart,ffi,Dart,Ffi,是否可以使用ffi将指向ofdart函数的指针传递到C模块(即*.so库)中,并直接从*.so库访问此dart函数并通过回拨方式使用它?非常感谢您提供的信息。这就是我需要的 省道: intmyplus(inta,intb){ 打印(“dart.myPlus.a=$a,b=$b.$a+b=>${a+b}”); 返回a+b; } main(){ ... 指针 指针=ffi.pointer.fromfonment(myPlus,0); 打印(指针); 指针 p17=动态查找(“ApplyTo42

是否可以使用ffi将指向ofdart函数的指针传递到C模块(即*.so库)中,并直接从*.so库访问此dart函数并通过回拨方式使用它?

非常感谢您提供的信息。这就是我需要的

省道:

intmyplus(inta,intb){
打印(“dart.myPlus.a=$a,b=$b.$a+b=>${a+b}”);
返回a+b;
}
main(){
...   
指针
指针=ffi.pointer.fromfonment(myPlus,0);
打印(指针);
指针
p17=动态查找(“ApplyTo42和74”);
ApplyTo42和74键入ApplyTo42和74=p17.asFunction();
int result=applyto42和74(指针);
打印(“结果=>$result”);
}
c:

#包括
#包括
typedef intptr_t(*IntptrBinOp)(intptr_t a,intptr_t b);
intptr_t应用程序42和74(IntptrBinOp binop){
printf(“applyto42和74()\n”);
intptr_t retval=binop(42,74);
printf(“返回%lu\n”,retval);
返回返回;
}
结果:

指针>:地址=0x7fb4acd98000
ApplyTo42和74()
省道.myPlus.a=42,b=74。42+74 => 116
返回116
结果=>116

您看过吗:?也许吧。我能在某处找到使用此函数的示例吗?github中的问题提供了类似的示例代码:
int myPlus(int a, int b) {
  print("dart.myPlus.a=$a,b=$b. $a+$b => ${a + b}");
  return a + b;
}

main() {
  ...   
  ffi.Pointer<ffi.NativeFunction<NativeIntptrBinOp>>
    pointer = ffi.Pointer.fromFunction(myPlus, 0);
  print(pointer);

  ffi.Pointer<ffi.NativeFunction<NativeApplyTo42And74Type>>
    p17 = dylib.lookup("ApplyTo42And74");
  ApplyTo42And74Type applyTo42And74 = p17.asFunction();
  int result = applyTo42And74(pointer);
  print("result => $result");
}
#include <stdio.h>
#include <stdint.h>

typedef intptr_t (*IntptrBinOp)(intptr_t a, intptr_t b);

intptr_t ApplyTo42And74(IntptrBinOp binop) {
  printf("ApplyTo42And74()\n");
  intptr_t retval = binop(42, 74);
  printf("returning %lu\n", retval);
  return retval;
}
Pointer<NativeFunction<(IntPtr, IntPtr) => IntPtr>>: address=0x7fb4acd98000
ApplyTo42And74()
dart.myPlus.a=42,b=74. 42+74 => 116
returning 116
result => 116