{
    "content": [
        {
            "type": "text",
            "text": "# ffi_call (man)\n\n## NAME\n\nfficall — Invoke a foreign function.\n\n## DESCRIPTION\n\nThe 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 retrieved\nfrom the pointers in the avalue array. The return value from fn is placed in storage pointed to\nby rvalue.  cif contains information describing the data types, sizes and alignments of the ar‐\nguments to and return value from fn, and must be initialized with ffiprepcif before it is\nused with fficall.\n\n## Sections\n\n- **NAME**\n- **SYNOPSIS** (1 subsections)\n- **DESCRIPTION**\n- **EXAMPLES**\n- **SEE ALSO**\n\nUse structuredContent.sections for detailed options, examples, and full documentation.\n"
        }
    ],
    "structuredContent": {
        "command": "ffi_call",
        "section": "",
        "mode": "man",
        "summary": "fficall — Invoke a foreign function.",
        "synopsis": "",
        "tldr_summary": null,
        "tldr_examples": [],
        "tldr_source": null,
        "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"
            }
        ],
        "section_outline": [
            {
                "name": "NAME",
                "lines": 2,
                "subsections": []
            },
            {
                "name": "SYNOPSIS",
                "lines": 1,
                "subsections": [
                    {
                        "name": "#include <ffi.h>",
                        "lines": 3
                    }
                ]
            },
            {
                "name": "DESCRIPTION",
                "lines": 11,
                "subsections": []
            },
            {
                "name": "EXAMPLES",
                "lines": 55,
                "subsections": []
            },
            {
                "name": "SEE ALSO",
                "lines": 3,
                "subsections": []
            }
        ],
        "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 retrieved\nfrom the pointers in the avalue array. The return value from fn is placed in storage pointed to\nby rvalue.  cif contains information describing the data types, sizes and alignments of the ar‐\nguments to and return value from fn, and must be initialized with ffiprepcif before it is\nused 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",
                "subsections": []
            }
        }
    }
}