Windows下php扩展开发c++动态库

来源:互联网 发布:网站美工要学什么 编辑:程序博客网 时间:2024/06/05 03:45

PHP扩展开发,从零了解到初步完成一个小项目,经过三天的仔细研究,现整理如下

一、需求介绍

PHP扩展开发,调用自己之前的c++动态库,完成功能

二、项目之前

系统:windows xp 

开发工具:vs 2008

web环境:apache2.4  PHP5.3.29-VC9-ts-x86 aphach和PHP 环境之前已经搭建完成

PHP源码:去官网http://www.php.net/downloads.php 下载稳定版本的php源码包(因为要编译扩展库,必须要php的源码才能编译),将源码解压到如:d:\php_src 目录下。本示例用的是PHP5.3.29。下载二进制包(如果已经安装了php环境,就可以不用下载),这里主要用到php二进制包中的php5ts.lib,该文件位于php的dev目录。本示例使用的是php-5.3.29-Win32-VC9-x86二进制包。
配置源码:将源码中php_src/win32/build/config.w32.h.in文件拷贝一份到php_src/main/下,并重命名为:config.w32.h。

PHP二进制包

PHP源码包

三、创建项目

1、创建一个空的win32项目(注意:是Win32的 dll 项目工程,)。
2、配置工程属性:
(1)添加附加包含目录:在C/C++的选项中,添加附加包含目录。包含php源码中的几个目录。
如:D:\php_src;D:\php_src\main;D:\php_src\Zend;D:\php_src\TSRM;D:\php_src\win32;
(2)添加预处理器:ZEND_DEBUG=0;ZTS=1;ZEND_WIN32;PHP_WIN32;

(3)添加附加库:php5ts.lib(该库位于php二进制文间包中的dev目录)



四、编写源码示例

1、添加源文件如:Main.cpp和源文件的头文件Main.h。其中文件的内容主要参考了在linux下编写php扩展库,自动生成的文件的内容(cygwin 可以帮助实现搭建好扩展的骨架,我没试验过,不过没有cygwin也没关系,直接拷贝下面的代码


Main.h文件内容:


#ifndef PHP_TEST_MAIN_H#define PHP_TEST_MAIN_Hextern zend_module_entry PHPTest_module_entry;          // PHPTest  是该示例的工程名字, PHPTest_module_entry是php扩展库的入口声明#define phpext_PHPTest_ptr &PHPTest_module_entry#ifdef PHP_WIN32#define PHP_PHPTest_API __declspec(dllexport)#elif defined(__GNUC__) && __GNUC__ >= 4#define PHP_PHPTest_API __attribute__ ((visibility("default")))#else#define PHP_PHPTest_API#endif#ifdef ZTS#include "TSRM.h"#endifPHP_MINIT_FUNCTION(PHPTest);PHP_MSHUTDOWN_FUNCTION(PHPTest);PHP_RINIT_FUNCTION(PHPTest);PHP_RSHUTDOWN_FUNCTION(PHPTest);PHP_MINFO_FUNCTION(PHPTest);// PHP_FUNCTION  用于定义要导出给php调用的函数名称,这里我们定义了3个函数:init_module,test_module, close_module// PHP_FUNCTION  只用来声明函数的名称,置于函数的参数将在cpp中定义PHP_FUNCTION(init_module);PHP_FUNCTION(test_module);PHP_FUNCTION(close_module);/*   Declare any global variables you may need between the BEGINand END macros here:     ZEND_BEGIN_MODULE_GLOBALS(CSVirusAnalyse)long  global_value;char *global_string;ZEND_END_MODULE_GLOBALS(CSVirusAnalyse)*//* In every utility function you add that needs to use variables    in php_CSVirusAnalyse_globals, call TSRMLS_FETCH(); after declaring other    variables used by that function, or better yet, pass in TSRMLS_CC   after the last function argument and declare your utility function   with TSRMLS_DC after the last declared argument.  Always refer to   the globals in your function as CSGAVIRUSANALYSIS_G(variable).  You are    encouraged to rename these macros something shorter, see   examples in any other php module directory.*/#ifdef ZTS#define PHPTEST_G(v) TSRMG(PHPTest_globals_id, zend_PHPTest_globals *, v)#else#define PHPTEST_G(v) (PHPTest_globals.v)#endif#endif/* PHP_TEST_MAIN_H*/








编译Mian.cpp文件:

// 声明以下的宏定义解决在编译过程中会发生:error C2466: 不能分配常量大小为0 的数组的错误。#ifdef PHP_WIN32  #define _STATIC_ASSERT(expr) typedef char __static_assert_t[ (expr)?(expr):1 ]  #else  #define _STATIC_ASSERT(expr) typedef char __static_assert_t[ (expr) ]  #endif  // #include "XXXXX.h" 在以下包含头文件的前面包含要用到的c++ 的stl的头文件,或者你自己写的C++的头文件。#include <string>using namespace std;extern "C"{#include "zend_config.w32.h"#include "php.h"#include "ext/standard/info.h"#include "Main.h"}// 声明了扩展库的导出函数列表zend_function_entry PHPTest_functions[] = {        PHP_FE(init_module, NULL)     PHP_FE(test_module, NULL)     PHP_FE(close_module, NULL)     PHP_FE_END}; zend_module_entry PHPTest_module_entry = {#if ZEND_MODULE_API_NO >= 20010901    STANDARD_MODULE_HEADER,#endif    "PHPTest",    PHPTest_functions,    PHP_MINIT(PHPTest),    PHP_MSHUTDOWN(PHPTest),    PHP_RINIT(PHPTest), /* Replace with NULL if there's nothing to do at request start */    PHP_RSHUTDOWN(PHPTest), /* Replace with NULL if there's nothing to do at request end */    PHP_MINFO(PHPTest),#if ZEND_MODULE_API_NO >= 20010901    "0.1", /* Replace with version number for your extension */#endif    STANDARD_MODULE_PROPERTIES};ZEND_GET_MODULE(PHPTest); PHP_MINIT_FUNCTION(PHPTest){    /* If you have INI entries, uncomment these lines     REGISTER_INI_ENTRIES();    */    return SUCCESS;}PHP_MSHUTDOWN_FUNCTION(PHPTest){    /* uncomment this line if you have INI entries    UNREGISTER_INI_ENTRIES();    */    return SUCCESS;}PHP_RINIT_FUNCTION(PHPTest){    return SUCCESS;}PHP_RSHUTDOWN_FUNCTION(PHPTest){    return SUCCESS;}PHP_MINFO_FUNCTION(PHPTest){    php_info_print_table_start();    php_info_print_table_header(2, "PHPTest support", "enabled");    php_info_print_table_end();    /* Remove comments if you have entries in php.ini    DISPLAY_INI_ENTRIES();    */}// 以下是php导出函数的实现,比如string init_module(string content)PHP_FUNCTION(init_module){    char *content = NULL;   //     int argc = ZEND_NUM_ARGS();    int content_len;    // 这句话便是导出传入参数     if (zend_parse_parameters(argc TSRMLS_CC, "s", &content, &content_len) == FAILURE)         return;    if(content)    {        //  这里只是为了测试,直接把传入值返回去。        string strRet = content;        // 返回值        RETURN_STRING((char*)strRet.c_str(), 1);    }    else    {        php_error(E_WARNING, "init_module: content is NULL");    }}// 以下是int test_module(string content)函数的实现PHP_FUNCTION(test_module){    char *content = NULL;    int argc = ZEND_NUM_ARGS();    int content_len;    if (zend_parse_parameters(argc TSRMLS_CC, "s", &content, &content_len) == FAILURE)         return;    if(content)    {        int nRet = content_len;        RETURN_LONG(nRet);    }    else    {        php_error(E_WARNING, "test_module: &content is NULL");    }}// 以下是 void close_module()函数的实现PHP_FUNCTION(close_module){    if (zend_parse_parameters_none() == FAILURE) {        return;    }    php_printf("close_module successfully\n"); }


  ok,编写完以上的文件后,编译一下,将生成的dll文件,拷贝到正常工作的php 的ext文件夹下,并在php.ini上配置,在extension=php_zip.dll后面添加extension=PHPTest.dll。然后重启Apache。

编写php测试 代码

<?phpecho init_module('test init');echo'<br>';//输出: test initecho test_module('test_module');echo'<br>';close_module();?>
出现问题


由于生成的PHPTest.dll 与PHP安装环境不一致导致,解决方法(非常重要)

为了解决这个问题走了很多弯路,开始以为是PHP源码版本的问题,下载了很多个版本都没成功,浪费了很多时间

解决很简单:在php_src\main\config.w32.h文件中增加 #define PHP_COMPILER_ID "VC9"用VC9编译

运行结果:

test init
11
close_module successfully 



五、注意

1、注意你的头文件的包含的顺序。

将你的头文件以及Windows和C++的头文件包含在php头文件的前面

#include "xxxx.h"  //  你的头文件extern "C"{#include "zend_config.w32.h"#include "php.h"#include "ext/standard/info.h"#include "Main.h"}

2.可能遇到error C2466: 不能分配常量大小为0 的数组
解决方法:

在vc的 c:\program files\microsoft visual studio 8\vc\include\malloc.h 文件中找到:#define _STATIC_ASSERT(expr) typedef char __static_assert_t[ (expr) ]将这一行改为:#ifdef PHP_WIN32#define _STATIC_ASSERT(expr) typedef char __static_assert_t[ (expr)?(expr):1 ]#else#define _STATIC_ASSERT(expr) typedef char __static_assert_t[ (expr) ]#endif
或者直接在你的cpp文件中定义也可以。


2. 如果遇到2019连接错误,那么通常是没有删除预处理定义中的宏LIBZEND_EXPORTS,



六、遇到问题


提示找不到phptest.dll,是因为在这个是项目中,在编辑PHPTest.dll的main.cpp文件中调用了之前自己写的动态库,如何方法之前的identify.dll库也成了一个问题,首先在PHPtest工程中添加动态链接库identify.lib,添加方法同php5ts.lib

然后把identify.dll拷贝到aphach安装目录下bin文件夹内C:\Apache24\bin 即可完成动态库的调用。



0 0
原创粉丝点击