classicvalues / pecl-php-ffi Goto Github PK
View Code? Open in Web Editor NEWThis project forked from php/pecl-php-ffi
Foreign Function Interface
Home Page: https://pecl.php.net/package/ffi
This project forked from php/pecl-php-ffi
Foreign Function Interface
Home Page: https://pecl.php.net/package/ffi
$Id$ Usage: First you need to declare the functions and types that you will be using: $win32_idl <<<EOD [lib='kernel32.dll'] DWORD GetTickCount(); [lib='user32.dll'] int MessageBoxA(int handle, char *text, char *caption, int type); EOD; Then bind those into an ffi context: $ffi = new ffi($win32_idl); And then use it: $count = $ffi->GetTickCount(); echo $ffi->MessageBoxA(0, "The tick count is " . $count, "Ticky Ticky", 1); Structures: =========== You can declare structures in your ffi IDL using C style syntax. Structure support is read-only for the moment. You can pass structures to functions and return them (these two examples work on linux): Passing structs: <?php $ffi = new ffi(<<<EOD struct timeval { long tv_sec; long tv_usec; }; struct timezone { int tz_minuteswest; int tz_dsttime; }; [lib='libc.so.6'] int gettimeofday(struct timeval *tv, struct timezone *tz); EOD ); $tv = new ffi_struct($ffi, "timeval"); $tz = new ffi_struct($ffi, "timezone"); var_dump( $ffi->gettimeofday($tv, $tz) ); printf("tv_sec=%d tv_usec=%d\n", $tv->tv_sec, $tv->tv_usec); ?> Returning Structs: <?php $ffi = new ffi(<<<EOD struct hostent { char *h_name; char **h_aliases; int h_addrtype; int h_length; char **h_addr_list; }; [lib='libc.so.6'] struct hostent *gethostbyname(char *name); EOD ); $he = $ffi->gethostbyname("localhost"); printf("h_length=%d h_name=%s\n", $he->h_length, $he->h_name); ?> Tips: For functions that expect to copy/store memory into a buffer, use str_repeat() to "allocate" room for that buffer. vim:tw=78:et
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.