<?

Class WinApi {
    public static $Base = array();
    protected static $Function = '';
    protected static $Struct = '';
    protected static $DllName = '';

    public static function DllName() {
        if(property_exists($DLL = get_called_class(), 'DllName') and !empty(static::$DllName))
            $DLL = static::$DllName;
        return $DLL;
    }

    public static function __callStatic($name, $arguments) {
        return call_user_func_array(
            array(array_key_exists(get_called_class(), self::$Base) ?
                    self::$Base[self::DllName()] :
                   (self::$Base[$DLL = self::DllName()] = new FFI(static::$Struct . PHP_EOL .
                        "[lib='". $DLL .".dll']" . PHP_EOL . preg_replace(array(
                        '/\bbool\b/', '/\bBOOL\b/', '/\bDWORD\b/', '/\bbyte\b/',
                        '/\binteger\b/', '/\bstring\b/', '/\buint\b/',
                        '/\bcardinal\b/', '/\bboolean\b/', '/\bshort\b/',
                        '/\bLPSTR\b/', '/\bLPCSTR\b/', '/\bLPCTSTR\b/',
                        '/\bLCID\b/', '/\bHWND\b/'
                    ), array(
                        'sint8', 'sint8', 'uint32', 'sint8', 'int', 'char *',
                        'uint32', 'uint32', 'sint8', 'sint8', 'char *',
                        'char *', 'char *', 'uint32', 'int'
                    ), static::$Function))),
                $name
            ),
        $arguments);
    }
    
    public static function NewFFIStruct($StructName) {
        return new FFIStruct(self::$Base[self::DllName()], $StructName);
    }
} 

?>