GithubHelp home page GithubHelp logo

classicvalues / pecl-php-ffi Goto Github PK

View Code? Open in Web Editor NEW

This project forked from php/pecl-php-ffi

1.0 1.0 0.0 188 KB

Foreign Function Interface

Home Page: https://pecl.php.net/package/ffi

Makefile 0.02% M4 1.15% JavaScript 0.19% PHP 0.20% C 72.32% Yacc 1.20% Assembly 24.01% C++ 0.90%

pecl-php-ffi's Introduction

$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

pecl-php-ffi's People

Contributors

edinkad avatar helly25 avatar johannes avatar pierrejoye avatar wez avatar zsuraski avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.