PHP 8.3.21 Released!

Uso básico de FFI

Antes de profundizar en los detalles de la API FFI, echemos un vistazo a algunos ejemplos que demuestran la simplicidad de uso de la API FFI para tareas comunes.

Nota:

Algunos de estos ejemplos requieren libc.so.6 y por lo tanto no funcionarán en sistemas donde esta biblioteca no esté disponible.

Ejemplo #1 Llamada a una función desde una biblioteca compartida

<?php
// crea un objeto FFI, cargando la libc y exportando la función printf()
$ffi = FFI::cdef(
"int printf(const char *format, ...);", // Declaración C regular
"libc.so.6");
// llama a la función printf() de C
$ffi->printf("Hello %s!\n", "world");
?>

El resultado del ejemplo sería:

Hello world!

Nota:

Tenga en cuenta que algunas funciones C requieren convenciones de llamada específicas, por ejemplo __fastcall, __stdcall o __vectorcall.

Ejemplo #2 Llamada a una función, devolviendo una estructura a través de un argumento

<?php
// crea la ligadura gettimeofday()
$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");
// crea las estructuras de datos C
$tv = $ffi->new("struct timeval");
$tz = $ffi->new("struct timezone");
// llama a la función gettimeofday() de C
var_dump($ffi->gettimeofday(FFI::addr($tv), FFI::addr($tz)));
// accede a los campos de la estructura de datos C
var_dump($tv->tv_sec);
// imprime toda la estructura de datos C
var_dump($tz);
?>

El resultado del ejemplo sería algo similar a:

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

Ejemplo #3 Acceso a variables C existentes

<?php
// crea un objeto FFI, cargando la libc y exportando la variable errno
$ffi = FFI::cdef(
"int errno;", // Declaración C regular
"libc.so.6");
// imprime el valor errno de C
var_dump($ffi->errno);
?>

El resultado del ejemplo sería:

int(0)

Ejemplo #4 Creación y modificación de variables C

<?php
// crea una nueva variable C de tipo int
$x = FFI::new("int");
var_dump($x->cdata);

// asignación simple
$x->cdata = 5;
var_dump($x->cdata);

// asignación compuesta
$x->cdata += 2;
var_dump($x->cdata);
?>

El resultado del ejemplo sería:

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

Ejemplo #5 Trabajar con arrays C

<?php
// crea una estructura de datos en C
$a = FFI::new("long[1024]");
// modificación de la estructura como con un array PHP normal
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));
?>

El resultado del ejemplo sería:

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

Ejemplo #6 Trabajar con enums en C

<?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);
?>

El resultado del ejemplo sería:

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

add a note

User Contributed Notes 1 note

up
4
wowabbs+php at gmail dot com
4 years ago
<?
if(!dl("ffi")) // Load the extension
throw new Exception('Cound not load the FFI extension.');

function setWindowsDesktop($bmpFilePath)
{
define('SPI_SETDESKWALLPAPER' , 0x14);
define('SPIF_UPDATEINIFILE' , 0x1);
define('SPIF_SENDWININICHANGE' , 0x2);
assert(File_Exists($bmpFilePath));

// declare the Win32 API function used to change desktop settings.
$User32 = FFI::cdef(<<<'IDL'
int SystemParametersInfoA(int uAction, int uParam, char *lpvParam, int fuWinIni);
IDL, 'User32.dll');
$Kernel32 = FFI::cdef(<<<'IDL'
int GetLastError();
IDL, 'Kernel32.dll');
// call the Windows API to update the desktop background.
$Ret = $User32->SystemParametersInfoA(SPI_SETDESKWALLPAPER, 0, $bmpFilePath, SPIF_UPDATEINIFILE || SPIF_SENDWININICHANGE);
if ($Ret == 0)
{
$Error = $Kernel32->GetLastError();
throw new Exception("The call to the Windows API failed (error {$Error}).");
}
}

$Url='https://www.php.net//images/news/phpkonf_2015.png';
$Img=File_Get_Contents($Url);
File_Put_Contents($File=basename($Url), $Img);
setWindowsDesktop(realpath($File));
?>
To Top