{
    "mode": "man",
    "parameter": "ffi_call",
    "section": "3",
    "url": "https://www.chedong.com/phpMan.php/man/ffi_call/3/json",
    "generated": "2026-10-09T09:58:49Z",
    "synopsis": "",
    "sections": {
        "NAME": {
            "content": "fficall — Invoke a foreign function.\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "",
            "subsections": [
                {
                    "name": "#include <ffi.h>",
                    "content": "void\nfficall(fficif *cif, void (*fn)(void), void *rvalue, void avalue);\n"
                }
            ]
        },
        "DESCRIPTION": {
            "content": "The  fficall  function provides a simple mechanism for invoking a function without requiring\nknowledge of the function's interface at compile time.  fn is  called  with  the  values  re‐\ntrieved  from the pointers in the avalue array. The return value from fn is placed in storage\npointed to by rvalue.  cif contains information describing the data types, sizes  and  align‐\nments of the arguments to and return value from fn, and must be initialized with ffiprepcif\nbefore it is used with fficall.\n\nrvalue  must point to storage that is sizeof(ffiarg) or larger for non-floating point types.\nFor smaller-sized return value types, the ffiarg or ffisarg integral type must be  used  to\nhold the return value.\n",
            "subsections": []
        },
        "EXAMPLES": {
            "content": "#include <ffi.h>\n#include <stdio.h>\n\nunsigned char\nfoo(unsigned int, float);\n\nint\nmain(int argc, const char argv)\n{\nfficif cif;\nffitype *argtypes[2];\nvoid *argvalues[2];\nffistatus status;\n\n// Because the return value from foo() is smaller than sizeof(long), it\n// must be passed as ffiarg or ffisarg.\nffiarg result;\n\n// Specify the data type of each argument. Available types are defined\n// in <ffi/ffi.h>.\nargtypes[0] = &ffitypeuint;\nargtypes[1] = &ffitypefloat;\n\n// Prepare the fficif structure.\nif ((status = ffiprepcif(&cif, FFIDEFAULTABI,\n2, &ffitypeuint8, argtypes)) != FFIOK)\n{\n// Handle the ffistatus error.\n}\n\n// Specify the values of each argument.\nunsigned int arg1 = 42;\nfloat arg2 = 5.1;\n\nargvalues[0] = &arg1;\nargvalues[1] = &arg2;\n\n// Invoke the function.\nfficall(&cif, FFIFN(foo), &result, argvalues);\n\n// The ffiarg 'result' now contains the unsigned char returned from foo(),\n// which can be accessed by a typecast.\nprintf(\"result is %hhu\", (unsigned char)result);\n\nreturn 0;\n}\n\n// The target function.\nunsigned char\nfoo(unsigned int x, float y)\n{\nunsigned char result = x - y;\nreturn result;\n}\n",
            "subsections": []
        },
        "SEE ALSO": {
            "content": "ffi(3), ffiprepcif(3)\n\nFebruary 15, 2008                              fficall(3)",
            "subsections": []
        }
    },
    "summary": "fficall — Invoke a foreign function.",
    "flags": [],
    "examples": [
        "#include <ffi.h>",
        "#include <stdio.h>",
        "unsigned char",
        "foo(unsigned int, float);",
        "int",
        "main(int argc, const char argv)",
        "fficif cif;",
        "ffitype *argtypes[2];",
        "void *argvalues[2];",
        "ffistatus status;",
        "// Because the return value from foo() is smaller than sizeof(long), it",
        "// must be passed as ffiarg or ffisarg.",
        "ffiarg result;",
        "// Specify the data type of each argument. Available types are defined",
        "// in <ffi/ffi.h>.",
        "argtypes[0] = &ffitypeuint;",
        "argtypes[1] = &ffitypefloat;",
        "// Prepare the fficif structure.",
        "if ((status = ffiprepcif(&cif, FFIDEFAULTABI,",
        "2, &ffitypeuint8, argtypes)) != FFIOK)",
        "// Handle the ffistatus error.",
        "// Specify the values of each argument.",
        "unsigned int arg1 = 42;",
        "float arg2 = 5.1;",
        "argvalues[0] = &arg1;",
        "argvalues[1] = &arg2;",
        "// Invoke the function.",
        "fficall(&cif, FFIFN(foo), &result, argvalues);",
        "// The ffiarg 'result' now contains the unsigned char returned from foo(),",
        "// which can be accessed by a typecast.",
        "printf(\"result is %hhu\", (unsigned char)result);",
        "return 0;",
        "// The target function.",
        "unsigned char",
        "foo(unsigned int x, float y)",
        "unsigned char result = x - y;",
        "return result;"
    ],
    "see_also": [
        {
            "name": "ffi",
            "section": "3",
            "url": "https://www.chedong.com/phpMan.php/man/ffi/3/json"
        },
        {
            "name": "ffiprepcif",
            "section": "3",
            "url": "https://www.chedong.com/phpMan.php/man/ffiprepcif/3/json"
        }
    ]
}