Basic FFI usage

Before diving into the details of the FFI API, lets take a look at a few examples demonstrating the simplicity of the FFI API usage for regular tasks.

Hinweis:

Some of these examples require libc.so.6 and as such will not work on systems where it is not available.

Beispiel #1 Calling a function from shared library

<?php
// create FFI object, loading libc and exporting function printf()
$ffi FFI::cdef(
    
"int printf(const char *format, ...);"// this is a regular C declaration
    
"libc.so.6");
// call C's printf()
$ffi->printf("Hello %s!\n""world");
?>

Das oben gezeigte Beispiel erzeugt folgende Ausgabe:

Hello world!

Hinweis:

Note that some C functions need specific calling conventions, e.g. __fastcall, __stdcall or ,__vectorcall.

Beispiel #2 Calling a function, returning a structure through an argument

<?php
// create gettimeofday() binding
$ffi FFI::cdef("
    typedef unsigned int time_t;
    typedef unsigned int suseconds_t;
 
    struct timeval {
        time_t      tv_sec;
        suseconds_t tv_usec;
    };
 
    struct timezone {
        int tz_minuteswest;
        int tz_dsttime;
    };
 
    int gettimeofday(struct timeval *tv, struct timezone *tz);    
"
"libc.so.6");
// create C data structures
$tv $ffi->new("struct timeval");
$tz $ffi->new("struct timezone");
// call C's gettimeofday()
var_dump($ffi->gettimeofday(FFI::addr($tv), FFI::addr($tz)));
// access field of C data structure
var_dump($tv->tv_sec);
// print the whole C data structure
var_dump($tz);
?>

Das oben gezeigte Beispiel erzeugt eine ähnliche Ausgabe wie:

int(0)
int(1555946835)
object(FFI\CData:struct timezone)#3 (2) {
  ["tz_minuteswest"]=>
  int(0)
  ["tz_dsttime"]=>
  int(0)
}

Beispiel #3 Accessing existing C variables

<?php
// create FFI object, loading libc and exporting errno variable
$ffi FFI::cdef(
    
"int errno;"// this is a regular C declaration
    
"libc.so.6");
// print C's errno
var_dump($ffi->errno);
?>

Das oben gezeigte Beispiel erzeugt folgende Ausgabe:

int(0)

Beispiel #4 Creating and Modifying C variables

<?php
// create a new C int variable
$x FFI::new("int");
var_dump($x->cdata);

// simple assignment
$x->cdata 5;
var_dump($x->cdata);

// compound assignment
$x->cdata += 2;
var_dump($x->cdata);
?>

Das oben gezeigte Beispiel erzeugt folgende Ausgabe:

int(0)
int(5)
int(7)

Beispiel #5 Working with C arrays

<?php
// create C data structure
$a FFI::new("long[1024]");
// work with it like with a regular PHP array
for ($i 0$i count($a); $i++) {
    
$a[$i] = $i;
}
var_dump($a[25]);
$sum 0;
foreach (
$a as $n) {
    
$sum += $n;
}
var_dump($sum);
var_dump(count($a));
var_dump(FFI::sizeof($a));
?>

Das oben gezeigte Beispiel erzeugt folgende Ausgabe:

int(25)
int(523776)
int(1024)
int(8192)

Beispiel #6 Working with C enums

<?php
$a 
FFI::cdef('typedef enum _zend_ffi_symbol_kind {
    ZEND_FFI_SYM_TYPE,
    ZEND_FFI_SYM_CONST = 2,
    ZEND_FFI_SYM_VAR,
    ZEND_FFI_SYM_FUNC
} zend_ffi_symbol_kind;
'
);
var_dump($a->ZEND_FFI_SYM_TYPE);
var_dump($a->ZEND_FFI_SYM_CONST);
var_dump($a->ZEND_FFI_SYM_VAR);
?>

Das oben gezeigte Beispiel erzeugt folgende Ausgabe:

int(0)
int(2)
int(3)

Hier Kannst Du einen Kommentar verfassen


Bitte gib mindestens 10 Zeichen ein.
Wird geladen... Bitte warte.
* Pflichtangabe
Es sind noch keine Kommentare vorhanden.

PHP cURL-Tutorial: Verwendung von cURL zum Durchführen von HTTP-Anfragen

cURL ist eine leistungsstarke PHP-Erweiterung, die es Ihnen ermöglicht, mit verschiedenen Servern über verschiedene Protokolle wie HTTP, HTTPS, FTP und mehr zu kommunizieren. ...

TheMax

Autor : TheMax
Kategorie: PHP-Tutorials

Midjourney Tutorial - Anleitung für Anfänger

Über Midjourney, dem Tool zur Erstellung digitaler Bilder mithilfe von künstlicher Intelligenz, gibt es ein informatives Video mit dem Titel "Midjourney Tutorial auf Deutsch - Anleitung für Anfänger" ...

Mike94

Autor : Mike94
Kategorie: KI Tutorials

Grundlagen von Views in MySQL

Views in einer MySQL-Datenbank bieten die Möglichkeit, eine virtuelle Tabelle basierend auf dem Ergebnis einer SQL-Abfrage zu erstellen. ...

admin

Autor : admin
Kategorie: mySQL-Tutorials

Tutorial veröffentlichen

Tutorial veröffentlichen

Teile Dein Wissen mit anderen Entwicklern weltweit

Du bist Profi in deinem Bereich und möchtest dein Wissen teilen, dann melde dich jetzt an und teile es mit unserer PHP-Community

mehr erfahren

Tutorial veröffentlichen

SoundCloud to MP3

SoundCloud (https://soundcloudto.com/) to MP3 converters have gained popularity as convenient tools for downloading and converting audio from Soun ...

Geschrieben von soundcloudto am 19.07.2024 04:10:40
Forum: User pages
Suche Netzwerktechnik Großhändler

I'm currently seeking reliable wholesalers specializing in network technology. Please provide details on your range of products, pricing, and any ...

Geschrieben von dussan77 am 12.07.2024 16:54:48
Forum: Ankündigungen
Getting Database Connection Failed Error in PHP Project

Hi everyone, I’m currently working on an AI and ML-based web application using PHP, and I've hit a roadblock. I’m encountering a “Database ...

Geschrieben von Gast am 27.06.2024 11:31:32
Forum: Off-Topic Diskussionen
How to overcome Safari's iframe cookie block?

Hello I think your cookies are set with SameSite=None; Secure to allow cross-site usage. To implement a custom proxy on the server side to handle ...

Geschrieben von Gast am 27.06.2024 11:04:46
Forum: HTML, JavaScript, AJAX, jQuery, CSS, Bootstrap, LESS