Manual Pages for UNIX Darwin command on man ffi_call
MyWebUniversity

Manual Pages for UNIX Darwin command on man ffi_call

fficall(3) BSD Library Functions Manual fficall(3)

NAME

ffffiiccaallll - Invoke a foreign function.

SYNOPSIS

##iinncclluuddee <>

void ffffiiccaallll(fficif *cif, void (*fn)(void), void *rvalue, void **avalue);

DESCRIPTION

The ffffiiccaallll function provides a simple mechanism for invoking a function without requiring knowledge of the function's interface at compile time. fn is called with the values retrieved from the pointers in the avalue array. The return value from fn is placed in storage pointed to by rvalue. cif contains information describing the data types, sizes and

alignments of the arguments to and return value from fn, and must be ini-

tialized with ffffiipprreeppcciiff before it is used with ffffiiccaallll. rvalue must point to storage that is sizeof(long) or larger. For smaller return value sizes, the ffffiiaarrgg or ffffiissaarrgg integral type must be used to hold the return value. EEXXAAMMPPLLEESS

#define MACOSX // for fficonfig.h on Darwin

#include

#include

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 . 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

ffi(3), ffiprepcif(3) Darwin July 20, 2007 Darwin




Contact us      |      About us      |      Term of use      |       Copyright © 2000-2019 MyWebUniversity.com ™