man-pages/man3/ffi_call.3.html
2021-03-31 01:06:50 +01:00

168 lines
3.3 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of ffi_call</TITLE>
</HEAD><BODY>
<H1>ffi_call</H1>
Section: C Library Functions (3)<BR><A HREF="#index">Index</A>
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
<BR>BSD mandoc<BR>
<A NAME="lbAB">&nbsp;</A>
<H2>NAME</H2>
<B>ffi_call</B>
- Invoke a foreign function.
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
In ffi.h
Ft void
Fo ffi_call
Fa ffi_cif *cif
Fa void (*fn)(void)
Fa void *rvalue
Fa void **avalue
Fc <A NAME="lbAD">&nbsp;</A>
<H2>DESCRIPTION</H2>
The
<B>ffi_call</B>
function provides a simple mechanism for invoking a function without
requiring knowledge of the function's interface at compile time.
Fa fn
is called with the values retrieved from the pointers in the
Fa avalue
array. The return value from
Fa fn
is placed in storage pointed to by
Fa rvalue .
Fa cif
contains information describing the data types, sizes and alignments of the
arguments to and return value from
Fa fn ,
and must be initialized with
<B>ffi_prep_cif</B>
before it is used with
<B>ffi_call</B>
<P>
Fa rvalue
must point to storage that is sizeof(ffi_arg) or larger for non-floating point
types. For smaller-sized return value types, the
<B>ffi_arg</B>
or
<B>ffi_sarg</B>
integral type must be used to hold
the return value.
<A NAME="lbAE">&nbsp;</A>
<H2>EXAMPLES</H2>
<PRE>
#include &lt;<A HREF="file:///usr/include/ffi.h">ffi.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/stdio.h">stdio.h</A>&gt;
unsigned char
foo(unsigned int, float);
int
main(int argc, const char **argv)
{
ffi_cif cif;
ffi_type *arg_types[2];
void *arg_values[2];
ffi_status status;
// Because the return value from foo() is smaller than sizeof(long), it
// must be passed as ffi_arg or ffi_sarg.
ffi_arg result;
// Specify the data type of each argument. Available types are defined
// in &lt;<A HREF="file:///usr/include/ffi/ffi.h">ffi/ffi.h</A>&gt;.
arg_types[0] = &amp;ffi_type_uint;
arg_types[1] = &amp;ffi_type_float;
// Prepare the ffi_cif structure.
if ((status = ffi_prep_cif(&amp;cif, FFI_DEFAULT_ABI,
2, &amp;ffi_type_uint8, arg_types)) != FFI_OK)
{
// Handle the ffi_status error.
}
// Specify the values of each argument.
unsigned int arg1 = 42;
float arg2 = 5.1;
arg_values[0] = &amp;arg1;
arg_values[1] = &amp;arg2;
// Invoke the function.
ffi_call(&amp;cif, FFI_FN(foo), &amp;result, arg_values);
// The ffi_arg 'result' now contains the unsigned char returned from foo(),
// which can be accessed by a typecast.
printf(&quot;result is %hhu&quot;, (unsigned char)result);
return 0;
}
// The target function.
unsigned char
foo(unsigned int x, float y)
{
unsigned char result = x - y;
return result;
}
</PRE>
<A NAME="lbAF">&nbsp;</A>
<H2>SEE ALSO</H2>
<A HREF="/cgi-bin/man/man2html?3+ffi">ffi</A>(3),
ffi_prep_cif3
<P>
<HR>
<A NAME="index">&nbsp;</A><H2>Index</H2>
<DL>
<DT id="1"><A HREF="#lbAB">NAME</A><DD>
<DT id="2"><A HREF="#lbAC">SYNOPSIS</A><DD>
<DT id="3"><A HREF="#lbAD">DESCRIPTION</A><DD>
<DT id="4"><A HREF="#lbAE">EXAMPLES</A><DD>
<DT id="5"><A HREF="#lbAF">SEE ALSO</A><DD>
</DL>
<HR>
This document was created by
<A HREF="/cgi-bin/man/man2html">man2html</A>,
using the manual pages.<BR>
Time: 00:05:42 GMT, March 31, 2021
</BODY>
</HTML>