libffi - 1
經由 libffi 進行最簡單的 function call - 無傳回值, 無參數
#include#include #include void say_hi() { printf("Hi\n"); } int main() { ffi_cif cif; ffi_status status; ffi_type * arg_types[] = { & ffi_type_void }; status = ffi_prep_cif( & cif, FFI_DEFAULT_ABI, 0, & ffi_type_void, arg_types); if(FFI_OK == status) { ffi_call(& cif, say_hi, NULL, NULL); } return 0; }
描述參數型態
ffi_type * arg_types[] = { & ffi_type_void };
將 abi, 傳回值型態, 參數型態及個數傳給 ffi_pre_cif(), 初始 cif 後就可以用它進行 function call 了
status = ffi_prep_cif( & cif, FFI_DEFAULT_ABI, 0, & ffi_type_void, arg_types);
將要被 call 的 function address, 接傳回值用的指標, 存放參數的指標傳給 ffi_call() 後 say_hi() 就會印出 "Hi" 啦
ffi_call(& cif, say_hi, NULL, NULL);