extern "C" int _declspec(dllexport) add(int a ,int b){
return a+b;
}
然后复制到php/ext/路径下,在php.ini中加入了extension=dll75.dll,重启Apache,
用DreamWeaver新建了文件index.php,放到E:\xampp\htdocs路径下.
<?php
$a=2;
$b=3;
$c=0;
$c=add($a,$b);
echo $c;
?>
在浏览器下查看,结果
Fatal error: Call to undefined function add() in E:\xampp\htdocs\index.php
php中该如何调用dll?
困惑多日,还请大虾赐教!小弟不胜感激!
17 个解决方案
#1
这种DLL不是你这样写的,你这样调用肯定不行。
#2
我小白,请指教,DLL该如何写才能让php调用呢
#3
准备:
1、php源码包和windows下的二进制包,以及安装Visual C++,并把Microsoft Visual Studio\Common\MSDev98\Bin的绝对路径添加到windows环境变量
2、解压源码包到d:\php_src
3、进入d:\php_src\ext目录,复制skeleton文件夹,并重命名为要开发扩展的名字,本例为“linvo”
4、把二进制包中dev目录下的php5ts.lib文件,拷入新建的linvo目录
5、编辑linvo目录中的php_skeleton.h、skeleton.c、skeleton.dsp这三个文件,替换内容中所有extname为linvo,EXTNAME为LINVO。(严格区分大小写)
编码:
6、编辑php_skeleton.h文件(头文件)
在PHP_FUNCTION(confirm_linvo_compiled);下面编写
PHP_FUNCTION(hello);
声明一个hello函数
7、编辑skeleton.c文件(主文件)
在PHP_FE(confirm_linvo_compiled, NULL) 下面编写
PHP_FE(hello, NULL)
这是函数入口,下面该写函数主体了
找到PHP_FUNCTION(confirm_test_compiled)函数,该函数是测试函数,在该函数后面新写一个函数
PHP_FUNCTION(hello)
{
char *arg = NULL;
int arg_len, len;
char *strg;
/* 接收参数 */
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arg_len) == FAILURE) {
return;
}
len = spprintf(&strg, 0, "Hello,%s", arg);
RETURN_STRINGL(strg, len, 0);
}
复制代码
编译:
8、运行cmd命令行,进入d:\php_src\ext\linvo目录
9、输入 msdev linvo.dsp /MAKE "linvo - Win32 Release_TS"
10、如果没有错误,则在php_src目录下会生成一个Release_TS文件夹,里面就是编译好的php_linvo.dll扩展
使用:
11、将其拷入运行环境中的php扩展目录ext
12、编辑php.ini添加extension=php_linvo.dll,重启apache
13、在php文件中执行如下语句
echo hello('Linvo');
将输出
Hello,Linvo
14、通过echo phpinfo();也可看到扩展已加载的信息
1、php源码包和windows下的二进制包,以及安装Visual C++,并把Microsoft Visual Studio\Common\MSDev98\Bin的绝对路径添加到windows环境变量
2、解压源码包到d:\php_src
3、进入d:\php_src\ext目录,复制skeleton文件夹,并重命名为要开发扩展的名字,本例为“linvo”
4、把二进制包中dev目录下的php5ts.lib文件,拷入新建的linvo目录
5、编辑linvo目录中的php_skeleton.h、skeleton.c、skeleton.dsp这三个文件,替换内容中所有extname为linvo,EXTNAME为LINVO。(严格区分大小写)
编码:
6、编辑php_skeleton.h文件(头文件)
在PHP_FUNCTION(confirm_linvo_compiled);下面编写
PHP_FUNCTION(hello);
声明一个hello函数
7、编辑skeleton.c文件(主文件)
在PHP_FE(confirm_linvo_compiled, NULL) 下面编写
PHP_FE(hello, NULL)
这是函数入口,下面该写函数主体了
找到PHP_FUNCTION(confirm_test_compiled)函数,该函数是测试函数,在该函数后面新写一个函数
PHP_FUNCTION(hello)
{
char *arg = NULL;
int arg_len, len;
char *strg;
/* 接收参数 */
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arg_len) == FAILURE) {
return;
}
len = spprintf(&strg, 0, "Hello,%s", arg);
RETURN_STRINGL(strg, len, 0);
}
复制代码
编译:
8、运行cmd命令行,进入d:\php_src\ext\linvo目录
9、输入 msdev linvo.dsp /MAKE "linvo - Win32 Release_TS"
10、如果没有错误,则在php_src目录下会生成一个Release_TS文件夹,里面就是编译好的php_linvo.dll扩展
使用:
11、将其拷入运行环境中的php扩展目录ext
12、编辑php.ini添加extension=php_linvo.dll,重启apache
13、在php文件中执行如下语句
echo hello('Linvo');
将输出
Hello,Linvo
14、通过echo phpinfo();也可看到扩展已加载的信息
#4
你提到的"ext目录,复制skeleton文件夹"我这里找不到
这是用Zend Studio吗?我只装了xampp和用DW写php
#5
求高人指点啊
#6
需要符合PHP的规范才可以。
#7
由php调用dll的规范是什么?请明示
#8
#3 已经给出的开发 php 扩展的概要说明和方法步骤,更详细的文档请到 www.php.net
#9
http://www.php.net/manual/en/faq.com.php#faq.com.q1
I have built a DLL to calculate something. Is there any way to run this DLL under PHP ?
If this is a simple DLL there is no way yet to run it from PHP. If the DLL contains a COM server you may be able to access it if it implements the IDispatch interface.
I have built a DLL to calculate something. Is there any way to run this DLL under PHP ?
If this is a simple DLL there is no way yet to run it from PHP. If the DLL contains a COM server you may be able to access it if it implements the IDispatch interface.
#10
好吧,难得你这么好学,祝你成功!————
This article describes the steps to create a custom PHP extension DLL for the Windows platform.
The Zend API documentation that comes with PHP 5 on Windows (see php_manual_en.chm) does
a good job explaining how to write extension methods, parse method parameters, and return
values. But there is not currently a good step-by-step tutorial on how to get your first extension
project up and running on Windows. The aim of this article is to fill that gap.
Prerequisites
Visual Studio 2005
You can alternately use the free Visual C++ Express Edition or the VC++ 8 compiler in the
Windows SDK v6.0 if you’re a makefile master. Visual Studio 2003 (VC++ 7) will
probably work just fine, but some of the project configuration steps will be different than
what is explained here
•
A web server
For this article I used Sambar Server 7.0. Any HTTP server that can run the PHP 5.x ISAPI
extension (php5isapi.dll) will do
•
PHP 5 binaries, installed and configured for your server
I used PHP 5.2.4. Using the windows installer package (.msi file) makes configuration
easier
•
PHP 5 source code
DONT PANIC! You do not need to build PHP from source, just get the source that matches
your binary version. You will need an extraction utility like WinRAR or ZipGenius that can
handle .tar archives.
•
Conventions
Italics denote file paths and names. C:\Server\PHP\dev
Screenshot icons provide links to images showing how dialogs are configured.
Document icons provide links to sample code files.
Configuring the Environment
I will not discuss installing and running the HTTP web server. If you do not already have IIS
available on your machine, I recommend the Sambar Server.
If you do not already have PHP 5 installed, download it from php.net. If you download and run the
MSI installer package, it will configure your web server.
After you’ve got PHP 5 installed and configured for your server, download and extract the
complete PHP 5 source code, also from php.net. Caution: Do NOT extract the source archive
w 页码,1/20(W)
http://blog.slickedit.com/2007/09/creating-a-php-5-extension-with-visual-c-2005/ 2011-05-03
over top of your existing binary installation. This article’s example setup includes the PHP 5
binaries installed to C:\Server\PHP5, and the source code extracted to C:\Server\PHP5Src.
Creating the Project
In Visual Studio 2005, create a new Visual C++ Win32 project using the project template. For
this example, I named the project CustomExt. When the Win32 Application Wizard appears, click
Application Settings (on the left) and select DLL as the application type. Click Finish to
create the project.
I recommend following along step-by-step, but you can also download the complete project source
(zipped). You’ll still need to follow the instructions for changing the directories to match your
system.
Change Default C++ Options
Bring up the Project Properties dialog, and make sure the Debug configuration is active. Under
Configuration Properties > General, change the Character Set to “Use Multi-Byte Character
Set”.
Then under Configuration Properties > C/C++ > Code Generation, change the following
options:
• Enable String Pooling to “Yes (/GF)”
• Enable Minimal Rebuild to “No”
• Basic Runtime Checks to “Default”
• Runtime Library to “Multi-threaded Debug (/MTd)”
Under Configuration Properties > C/C++ > General, change the following:
• Debug Information Format to “Program Database (/Zi)”
• Detect 64-bit Portability Issues to “No”
Be sure to click Apply to save the settings.
Set the INCLUDE Paths
Still on the Project Properties dialog unders Configuration Properties > C/C++ > General, in
the Additional Include Directories, add the following paths, replacing C:\Server\PHP5Src with the
location on your machine where you extracted the source code.
C:/Server/PHP5Src/main
C:/Server/PHP5Src/Zend
C:/Server/PHP5Src/TSRM
C:/Server/PHP5Src/regex
C:/Server/PHP5Src
Again, be sure to click Apply to save the settings as you go along.
w 页码,2/20(W)
http://blog.slickedit.com/2007/09/creating-a-php-5-extension-with-visual-c-2005/ 2011-05-03
Set the Preprocessor Definitions
On the Project Properties dialog, under Configuration Properties > C/C++ > Preprocessor, add
the following definitions:
ZEND_DEBUG=0
ZTS=1
ZEND_WIN32
PHP_WIN32
Note: Do not be tempted to change ZEND_DEBUG to 1, even for this Debug build, as this will
prevent your extension from being loaded into the pre-built PHP binaries installed on your system.
Set the Linker Options
On the Project Properties dialog, under Configuration Properties > Linker > General add the
path to the C:\Server\PHP\dev directory to the Additional Library Directories. This is the
directory underneath your installed PHP binaries (not the extracted source). This directory should
contain php5ts.lib.
Under Configuration Properties > Linker > Input, add php5ts.lib to the Additional
Dependecies.
By convention, php extensions start with php_, so under Configuration Properties > Linker >
General, for the Output File option, I changed the output name to php_custom_ext.dll. This is
entirely optional.
The Extension Code
Replace the contents of stdafx.h with the following, or download it :
#pragma once
/* PHP Extension headers */
/* include zend win32 config first */
#include "zend_config.w32.h"
/* include standard header */
#include "php.h"
Replace the contents of ProjectName.cpp (in this example CustomExt.cpp) with the following (or
download it ):
#include "stdafx.h"/* declaration of functions to be exported */
ZEND_FUNCTION(DoubleUp);
/* compiled function list so Zend knows what's in this module */
zend_function_entry CustomExtModule_functions[] = {
ZEND_FE(DoubleUp, NULL)
{NULL, NULL, NULL}
};
/* compiled module information */
zend_module_entry CustomExtModule_module_entry = {
STANDARD_MODULE_HEADER,
"CustomExt Module",
CustomExtModule_functions,
w 页码,3/20(W)
http://blog.slickedit.com/2007/09/creating-a-php-5-extension-with-visual-c-2005/ 2011-05-03
NULL, NULL, NULL, NULL, NULL,
NO_VERSION_YET, STANDARD_MODULE_PROPERTIES
};
/* implement standard "stub" routine to introduce ourselves to Zend */
ZEND_GET_MODULE(CustomExtModule)
/* DoubleUp function */
/* This method takes 1 parameter, a long value, returns
the value multiplied by 2 */
ZEND_FUNCTION(DoubleUp){
long theValue = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
"l", &theValue) == FAILURE){
RETURN_STRING("Bad parameters!", true);
}
theValue *= 2;
RETURN_LONG(theValue);
}
You should now be able to build the project without any errors. Be careful when renaming
CustomExtModule or DoubleUp, and you must replace every instance in the file, or the Zend
macros will yield nearly indecipherable compiler errors.
Enable the extension
The first step is to locate the \ext directory beneath your PHP 5 binaries (not the directory where
you extracted the PHP source code). On my sample system this is C:\Server\PHP\ext. Copy the
DLL that you just built to this directory. If you are going to attach a debugger to your extension,
you will also want to copy the .pdb files.
The second step is to configure PHP to load your extension DLL. You do this by modifying
PHP.ini, adding a
1 extension=php_ext_name.dll
line to the Dynamic Extensions section, like the following.
;;;;;;;;;;;;;;;;;;;;;;
; Dynamic Extensions ;
;;;;;;;;;;;;;;;;;;;;;;
[CustomExt]
extension=php_custom_ext.dll
Verify that the extension can be loaded
Once you’ve edited and saved php.ini, you may need to restart the HTTP server in order to pick up
the new module. After the server is restarted, browse to the phpinfo.php web page. If you do not
already have a phpinfo.php page, create one using the following text, and save it to one of your
virtual directories:
<?php
phpinfo();
?>
Now browse to http://localhost/virtualpath/phpinfo.php, and look for the Additional Modules
section. Your new extension should now be seen in the listing. If your extension is not listed, you
may need to resave php.ini and restart the server. Also, make sure that the php.ini file that your
server is using is the same ini file that you just edited. On most servers, php.ini is located in the
w 页码,4/20(W)
http://blog.slickedit.com/2007/09/creating-a-php-5-extension-with-visual-c-2005/ 2011-05-03
PHP 5 installation directory, but your server may be different. To make certain, look at the
information at the top of the page for the Loaded Configuration File entry, which shows the full
path to the ini file PHP is currently using.
Write a test page
Copy the following code to testext.php (or download it ) , save it under one of your web server’
s virtual directories.
<?php
$value = 14;
$result = DoubleUp($value);
print "Calling DoubleUp($value) returned $result";
?>
You can now browse to http://localhost/virtualpath/testext.php.
This article describes the steps to create a custom PHP extension DLL for the Windows platform.
The Zend API documentation that comes with PHP 5 on Windows (see php_manual_en.chm) does
a good job explaining how to write extension methods, parse method parameters, and return
values. But there is not currently a good step-by-step tutorial on how to get your first extension
project up and running on Windows. The aim of this article is to fill that gap.
Prerequisites
Visual Studio 2005
You can alternately use the free Visual C++ Express Edition or the VC++ 8 compiler in the
Windows SDK v6.0 if you’re a makefile master. Visual Studio 2003 (VC++ 7) will
probably work just fine, but some of the project configuration steps will be different than
what is explained here
•
A web server
For this article I used Sambar Server 7.0. Any HTTP server that can run the PHP 5.x ISAPI
extension (php5isapi.dll) will do
•
PHP 5 binaries, installed and configured for your server
I used PHP 5.2.4. Using the windows installer package (.msi file) makes configuration
easier
•
PHP 5 source code
DONT PANIC! You do not need to build PHP from source, just get the source that matches
your binary version. You will need an extraction utility like WinRAR or ZipGenius that can
handle .tar archives.
•
Conventions
Italics denote file paths and names. C:\Server\PHP\dev
Screenshot icons provide links to images showing how dialogs are configured.
Document icons provide links to sample code files.
Configuring the Environment
I will not discuss installing and running the HTTP web server. If you do not already have IIS
available on your machine, I recommend the Sambar Server.
If you do not already have PHP 5 installed, download it from php.net. If you download and run the
MSI installer package, it will configure your web server.
After you’ve got PHP 5 installed and configured for your server, download and extract the
complete PHP 5 source code, also from php.net. Caution: Do NOT extract the source archive
w 页码,1/20(W)
http://blog.slickedit.com/2007/09/creating-a-php-5-extension-with-visual-c-2005/ 2011-05-03
over top of your existing binary installation. This article’s example setup includes the PHP 5
binaries installed to C:\Server\PHP5, and the source code extracted to C:\Server\PHP5Src.
Creating the Project
In Visual Studio 2005, create a new Visual C++ Win32 project using the project template. For
this example, I named the project CustomExt. When the Win32 Application Wizard appears, click
Application Settings (on the left) and select DLL as the application type. Click Finish to
create the project.
I recommend following along step-by-step, but you can also download the complete project source
(zipped). You’ll still need to follow the instructions for changing the directories to match your
system.
Change Default C++ Options
Bring up the Project Properties dialog, and make sure the Debug configuration is active. Under
Configuration Properties > General, change the Character Set to “Use Multi-Byte Character
Set”.
Then under Configuration Properties > C/C++ > Code Generation, change the following
options:
• Enable String Pooling to “Yes (/GF)”
• Enable Minimal Rebuild to “No”
• Basic Runtime Checks to “Default”
• Runtime Library to “Multi-threaded Debug (/MTd)”
Under Configuration Properties > C/C++ > General, change the following:
• Debug Information Format to “Program Database (/Zi)”
• Detect 64-bit Portability Issues to “No”
Be sure to click Apply to save the settings.
Set the INCLUDE Paths
Still on the Project Properties dialog unders Configuration Properties > C/C++ > General, in
the Additional Include Directories, add the following paths, replacing C:\Server\PHP5Src with the
location on your machine where you extracted the source code.
C:/Server/PHP5Src/main
C:/Server/PHP5Src/Zend
C:/Server/PHP5Src/TSRM
C:/Server/PHP5Src/regex
C:/Server/PHP5Src
Again, be sure to click Apply to save the settings as you go along.
w 页码,2/20(W)
http://blog.slickedit.com/2007/09/creating-a-php-5-extension-with-visual-c-2005/ 2011-05-03
Set the Preprocessor Definitions
On the Project Properties dialog, under Configuration Properties > C/C++ > Preprocessor, add
the following definitions:
ZEND_DEBUG=0
ZTS=1
ZEND_WIN32
PHP_WIN32
Note: Do not be tempted to change ZEND_DEBUG to 1, even for this Debug build, as this will
prevent your extension from being loaded into the pre-built PHP binaries installed on your system.
Set the Linker Options
On the Project Properties dialog, under Configuration Properties > Linker > General add the
path to the C:\Server\PHP\dev directory to the Additional Library Directories. This is the
directory underneath your installed PHP binaries (not the extracted source). This directory should
contain php5ts.lib.
Under Configuration Properties > Linker > Input, add php5ts.lib to the Additional
Dependecies.
By convention, php extensions start with php_, so under Configuration Properties > Linker >
General, for the Output File option, I changed the output name to php_custom_ext.dll. This is
entirely optional.
The Extension Code
Replace the contents of stdafx.h with the following, or download it :
#pragma once
/* PHP Extension headers */
/* include zend win32 config first */
#include "zend_config.w32.h"
/* include standard header */
#include "php.h"
Replace the contents of ProjectName.cpp (in this example CustomExt.cpp) with the following (or
download it ):
#include "stdafx.h"/* declaration of functions to be exported */
ZEND_FUNCTION(DoubleUp);
/* compiled function list so Zend knows what's in this module */
zend_function_entry CustomExtModule_functions[] = {
ZEND_FE(DoubleUp, NULL)
{NULL, NULL, NULL}
};
/* compiled module information */
zend_module_entry CustomExtModule_module_entry = {
STANDARD_MODULE_HEADER,
"CustomExt Module",
CustomExtModule_functions,
w 页码,3/20(W)
http://blog.slickedit.com/2007/09/creating-a-php-5-extension-with-visual-c-2005/ 2011-05-03
NULL, NULL, NULL, NULL, NULL,
NO_VERSION_YET, STANDARD_MODULE_PROPERTIES
};
/* implement standard "stub" routine to introduce ourselves to Zend */
ZEND_GET_MODULE(CustomExtModule)
/* DoubleUp function */
/* This method takes 1 parameter, a long value, returns
the value multiplied by 2 */
ZEND_FUNCTION(DoubleUp){
long theValue = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
"l", &theValue) == FAILURE){
RETURN_STRING("Bad parameters!", true);
}
theValue *= 2;
RETURN_LONG(theValue);
}
You should now be able to build the project without any errors. Be careful when renaming
CustomExtModule or DoubleUp, and you must replace every instance in the file, or the Zend
macros will yield nearly indecipherable compiler errors.
Enable the extension
The first step is to locate the \ext directory beneath your PHP 5 binaries (not the directory where
you extracted the PHP source code). On my sample system this is C:\Server\PHP\ext. Copy the
DLL that you just built to this directory. If you are going to attach a debugger to your extension,
you will also want to copy the .pdb files.
The second step is to configure PHP to load your extension DLL. You do this by modifying
PHP.ini, adding a
1 extension=php_ext_name.dll
line to the Dynamic Extensions section, like the following.
;;;;;;;;;;;;;;;;;;;;;;
; Dynamic Extensions ;
;;;;;;;;;;;;;;;;;;;;;;
[CustomExt]
extension=php_custom_ext.dll
Verify that the extension can be loaded
Once you’ve edited and saved php.ini, you may need to restart the HTTP server in order to pick up
the new module. After the server is restarted, browse to the phpinfo.php web page. If you do not
already have a phpinfo.php page, create one using the following text, and save it to one of your
virtual directories:
<?php
phpinfo();
?>
Now browse to http://localhost/virtualpath/phpinfo.php, and look for the Additional Modules
section. Your new extension should now be seen in the listing. If your extension is not listed, you
may need to resave php.ini and restart the server. Also, make sure that the php.ini file that your
server is using is the same ini file that you just edited. On most servers, php.ini is located in the
w 页码,4/20(W)
http://blog.slickedit.com/2007/09/creating-a-php-5-extension-with-visual-c-2005/ 2011-05-03
PHP 5 installation directory, but your server may be different. To make certain, look at the
information at the top of the page for the Loaded Configuration File entry, which shows the full
path to the ini file PHP is currently using.
Write a test page
Copy the following code to testext.php (or download it ) , save it under one of your web server’
s virtual directories.
<?php
$value = 14;
$result = DoubleUp($value);
print "Calling DoubleUp($value) returned $result";
?>
You can now browse to http://localhost/virtualpath/testext.php.
#11
楼主你太狠了,把.dll文件当成.php来使用了,不过真的能这样的话也太好了。
ps:楼主你真的很狠
ps:楼主你真的很狠
#12
本人新手,请问:该如何生成php可以调用的dll?
#13
[img=http://bbs.php100.com/attachment/Mon_1107/18_151635_56ffb3955a14e73.jpg?24][/img]
php自带的ext文件夹下的dll注册不成功,找不到入口点DllRegisterServer...怎么回事?
php自带的ext文件夹下的dll注册不成功,找不到入口点DllRegisterServer...怎么回事?
#14
#3和#10已经说了
php 的 dll 必须是 php 的专有格式。虽然 dll 后缀被使用,但其真正的名字是 php扩展
php 的 dll 必须是 php 的专有格式。虽然 dll 后缀被使用,但其真正的名字是 php扩展
#15
因为他不是 com 应用,所以不可能被注册
#16
哦,原来ext文件夹下的dll们不是com应用!
我想实现 用户上传图片,自动调用dll里的函数进行处理,返回个结果给客户端浏览器.
这个dll该怎么做?
1,做成和/ext里一样的php专用dll
2,做com应用的dll
是这样吗?
一般采用哪种方式?
#17
php源码包原来是tar.gz包,我按你的写的步骤做了一遍,在第9步之前,下载了个config.w32.h到/php_src/main目录
编译得到dll,复制到ext下,在php.ini里添加extension=php_linvo.dll,重启apache,还是没达到结果,提示
Call to undefined function hello()
崩溃了
#1
这种DLL不是你这样写的,你这样调用肯定不行。
#2
我小白,请指教,DLL该如何写才能让php调用呢
#3
准备:
1、php源码包和windows下的二进制包,以及安装Visual C++,并把Microsoft Visual Studio\Common\MSDev98\Bin的绝对路径添加到windows环境变量
2、解压源码包到d:\php_src
3、进入d:\php_src\ext目录,复制skeleton文件夹,并重命名为要开发扩展的名字,本例为“linvo”
4、把二进制包中dev目录下的php5ts.lib文件,拷入新建的linvo目录
5、编辑linvo目录中的php_skeleton.h、skeleton.c、skeleton.dsp这三个文件,替换内容中所有extname为linvo,EXTNAME为LINVO。(严格区分大小写)
编码:
6、编辑php_skeleton.h文件(头文件)
在PHP_FUNCTION(confirm_linvo_compiled);下面编写
PHP_FUNCTION(hello);
声明一个hello函数
7、编辑skeleton.c文件(主文件)
在PHP_FE(confirm_linvo_compiled, NULL) 下面编写
PHP_FE(hello, NULL)
这是函数入口,下面该写函数主体了
找到PHP_FUNCTION(confirm_test_compiled)函数,该函数是测试函数,在该函数后面新写一个函数
PHP_FUNCTION(hello)
{
char *arg = NULL;
int arg_len, len;
char *strg;
/* 接收参数 */
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arg_len) == FAILURE) {
return;
}
len = spprintf(&strg, 0, "Hello,%s", arg);
RETURN_STRINGL(strg, len, 0);
}
复制代码
编译:
8、运行cmd命令行,进入d:\php_src\ext\linvo目录
9、输入 msdev linvo.dsp /MAKE "linvo - Win32 Release_TS"
10、如果没有错误,则在php_src目录下会生成一个Release_TS文件夹,里面就是编译好的php_linvo.dll扩展
使用:
11、将其拷入运行环境中的php扩展目录ext
12、编辑php.ini添加extension=php_linvo.dll,重启apache
13、在php文件中执行如下语句
echo hello('Linvo');
将输出
Hello,Linvo
14、通过echo phpinfo();也可看到扩展已加载的信息
1、php源码包和windows下的二进制包,以及安装Visual C++,并把Microsoft Visual Studio\Common\MSDev98\Bin的绝对路径添加到windows环境变量
2、解压源码包到d:\php_src
3、进入d:\php_src\ext目录,复制skeleton文件夹,并重命名为要开发扩展的名字,本例为“linvo”
4、把二进制包中dev目录下的php5ts.lib文件,拷入新建的linvo目录
5、编辑linvo目录中的php_skeleton.h、skeleton.c、skeleton.dsp这三个文件,替换内容中所有extname为linvo,EXTNAME为LINVO。(严格区分大小写)
编码:
6、编辑php_skeleton.h文件(头文件)
在PHP_FUNCTION(confirm_linvo_compiled);下面编写
PHP_FUNCTION(hello);
声明一个hello函数
7、编辑skeleton.c文件(主文件)
在PHP_FE(confirm_linvo_compiled, NULL) 下面编写
PHP_FE(hello, NULL)
这是函数入口,下面该写函数主体了
找到PHP_FUNCTION(confirm_test_compiled)函数,该函数是测试函数,在该函数后面新写一个函数
PHP_FUNCTION(hello)
{
char *arg = NULL;
int arg_len, len;
char *strg;
/* 接收参数 */
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arg_len) == FAILURE) {
return;
}
len = spprintf(&strg, 0, "Hello,%s", arg);
RETURN_STRINGL(strg, len, 0);
}
复制代码
编译:
8、运行cmd命令行,进入d:\php_src\ext\linvo目录
9、输入 msdev linvo.dsp /MAKE "linvo - Win32 Release_TS"
10、如果没有错误,则在php_src目录下会生成一个Release_TS文件夹,里面就是编译好的php_linvo.dll扩展
使用:
11、将其拷入运行环境中的php扩展目录ext
12、编辑php.ini添加extension=php_linvo.dll,重启apache
13、在php文件中执行如下语句
echo hello('Linvo');
将输出
Hello,Linvo
14、通过echo phpinfo();也可看到扩展已加载的信息
#4
你提到的"ext目录,复制skeleton文件夹"我这里找不到
这是用Zend Studio吗?我只装了xampp和用DW写php
#5
求高人指点啊
#6
需要符合PHP的规范才可以。
#7
由php调用dll的规范是什么?请明示
#8
#3 已经给出的开发 php 扩展的概要说明和方法步骤,更详细的文档请到 www.php.net
#9
http://www.php.net/manual/en/faq.com.php#faq.com.q1
I have built a DLL to calculate something. Is there any way to run this DLL under PHP ?
If this is a simple DLL there is no way yet to run it from PHP. If the DLL contains a COM server you may be able to access it if it implements the IDispatch interface.
I have built a DLL to calculate something. Is there any way to run this DLL under PHP ?
If this is a simple DLL there is no way yet to run it from PHP. If the DLL contains a COM server you may be able to access it if it implements the IDispatch interface.
#10
好吧,难得你这么好学,祝你成功!————
This article describes the steps to create a custom PHP extension DLL for the Windows platform.
The Zend API documentation that comes with PHP 5 on Windows (see php_manual_en.chm) does
a good job explaining how to write extension methods, parse method parameters, and return
values. But there is not currently a good step-by-step tutorial on how to get your first extension
project up and running on Windows. The aim of this article is to fill that gap.
Prerequisites
Visual Studio 2005
You can alternately use the free Visual C++ Express Edition or the VC++ 8 compiler in the
Windows SDK v6.0 if you’re a makefile master. Visual Studio 2003 (VC++ 7) will
probably work just fine, but some of the project configuration steps will be different than
what is explained here
•
A web server
For this article I used Sambar Server 7.0. Any HTTP server that can run the PHP 5.x ISAPI
extension (php5isapi.dll) will do
•
PHP 5 binaries, installed and configured for your server
I used PHP 5.2.4. Using the windows installer package (.msi file) makes configuration
easier
•
PHP 5 source code
DONT PANIC! You do not need to build PHP from source, just get the source that matches
your binary version. You will need an extraction utility like WinRAR or ZipGenius that can
handle .tar archives.
•
Conventions
Italics denote file paths and names. C:\Server\PHP\dev
Screenshot icons provide links to images showing how dialogs are configured.
Document icons provide links to sample code files.
Configuring the Environment
I will not discuss installing and running the HTTP web server. If you do not already have IIS
available on your machine, I recommend the Sambar Server.
If you do not already have PHP 5 installed, download it from php.net. If you download and run the
MSI installer package, it will configure your web server.
After you’ve got PHP 5 installed and configured for your server, download and extract the
complete PHP 5 source code, also from php.net. Caution: Do NOT extract the source archive
w 页码,1/20(W)
http://blog.slickedit.com/2007/09/creating-a-php-5-extension-with-visual-c-2005/ 2011-05-03
over top of your existing binary installation. This article’s example setup includes the PHP 5
binaries installed to C:\Server\PHP5, and the source code extracted to C:\Server\PHP5Src.
Creating the Project
In Visual Studio 2005, create a new Visual C++ Win32 project using the project template. For
this example, I named the project CustomExt. When the Win32 Application Wizard appears, click
Application Settings (on the left) and select DLL as the application type. Click Finish to
create the project.
I recommend following along step-by-step, but you can also download the complete project source
(zipped). You’ll still need to follow the instructions for changing the directories to match your
system.
Change Default C++ Options
Bring up the Project Properties dialog, and make sure the Debug configuration is active. Under
Configuration Properties > General, change the Character Set to “Use Multi-Byte Character
Set”.
Then under Configuration Properties > C/C++ > Code Generation, change the following
options:
• Enable String Pooling to “Yes (/GF)”
• Enable Minimal Rebuild to “No”
• Basic Runtime Checks to “Default”
• Runtime Library to “Multi-threaded Debug (/MTd)”
Under Configuration Properties > C/C++ > General, change the following:
• Debug Information Format to “Program Database (/Zi)”
• Detect 64-bit Portability Issues to “No”
Be sure to click Apply to save the settings.
Set the INCLUDE Paths
Still on the Project Properties dialog unders Configuration Properties > C/C++ > General, in
the Additional Include Directories, add the following paths, replacing C:\Server\PHP5Src with the
location on your machine where you extracted the source code.
C:/Server/PHP5Src/main
C:/Server/PHP5Src/Zend
C:/Server/PHP5Src/TSRM
C:/Server/PHP5Src/regex
C:/Server/PHP5Src
Again, be sure to click Apply to save the settings as you go along.
w 页码,2/20(W)
http://blog.slickedit.com/2007/09/creating-a-php-5-extension-with-visual-c-2005/ 2011-05-03
Set the Preprocessor Definitions
On the Project Properties dialog, under Configuration Properties > C/C++ > Preprocessor, add
the following definitions:
ZEND_DEBUG=0
ZTS=1
ZEND_WIN32
PHP_WIN32
Note: Do not be tempted to change ZEND_DEBUG to 1, even for this Debug build, as this will
prevent your extension from being loaded into the pre-built PHP binaries installed on your system.
Set the Linker Options
On the Project Properties dialog, under Configuration Properties > Linker > General add the
path to the C:\Server\PHP\dev directory to the Additional Library Directories. This is the
directory underneath your installed PHP binaries (not the extracted source). This directory should
contain php5ts.lib.
Under Configuration Properties > Linker > Input, add php5ts.lib to the Additional
Dependecies.
By convention, php extensions start with php_, so under Configuration Properties > Linker >
General, for the Output File option, I changed the output name to php_custom_ext.dll. This is
entirely optional.
The Extension Code
Replace the contents of stdafx.h with the following, or download it :
#pragma once
/* PHP Extension headers */
/* include zend win32 config first */
#include "zend_config.w32.h"
/* include standard header */
#include "php.h"
Replace the contents of ProjectName.cpp (in this example CustomExt.cpp) with the following (or
download it ):
#include "stdafx.h"/* declaration of functions to be exported */
ZEND_FUNCTION(DoubleUp);
/* compiled function list so Zend knows what's in this module */
zend_function_entry CustomExtModule_functions[] = {
ZEND_FE(DoubleUp, NULL)
{NULL, NULL, NULL}
};
/* compiled module information */
zend_module_entry CustomExtModule_module_entry = {
STANDARD_MODULE_HEADER,
"CustomExt Module",
CustomExtModule_functions,
w 页码,3/20(W)
http://blog.slickedit.com/2007/09/creating-a-php-5-extension-with-visual-c-2005/ 2011-05-03
NULL, NULL, NULL, NULL, NULL,
NO_VERSION_YET, STANDARD_MODULE_PROPERTIES
};
/* implement standard "stub" routine to introduce ourselves to Zend */
ZEND_GET_MODULE(CustomExtModule)
/* DoubleUp function */
/* This method takes 1 parameter, a long value, returns
the value multiplied by 2 */
ZEND_FUNCTION(DoubleUp){
long theValue = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
"l", &theValue) == FAILURE){
RETURN_STRING("Bad parameters!", true);
}
theValue *= 2;
RETURN_LONG(theValue);
}
You should now be able to build the project without any errors. Be careful when renaming
CustomExtModule or DoubleUp, and you must replace every instance in the file, or the Zend
macros will yield nearly indecipherable compiler errors.
Enable the extension
The first step is to locate the \ext directory beneath your PHP 5 binaries (not the directory where
you extracted the PHP source code). On my sample system this is C:\Server\PHP\ext. Copy the
DLL that you just built to this directory. If you are going to attach a debugger to your extension,
you will also want to copy the .pdb files.
The second step is to configure PHP to load your extension DLL. You do this by modifying
PHP.ini, adding a
1 extension=php_ext_name.dll
line to the Dynamic Extensions section, like the following.
;;;;;;;;;;;;;;;;;;;;;;
; Dynamic Extensions ;
;;;;;;;;;;;;;;;;;;;;;;
[CustomExt]
extension=php_custom_ext.dll
Verify that the extension can be loaded
Once you’ve edited and saved php.ini, you may need to restart the HTTP server in order to pick up
the new module. After the server is restarted, browse to the phpinfo.php web page. If you do not
already have a phpinfo.php page, create one using the following text, and save it to one of your
virtual directories:
<?php
phpinfo();
?>
Now browse to http://localhost/virtualpath/phpinfo.php, and look for the Additional Modules
section. Your new extension should now be seen in the listing. If your extension is not listed, you
may need to resave php.ini and restart the server. Also, make sure that the php.ini file that your
server is using is the same ini file that you just edited. On most servers, php.ini is located in the
w 页码,4/20(W)
http://blog.slickedit.com/2007/09/creating-a-php-5-extension-with-visual-c-2005/ 2011-05-03
PHP 5 installation directory, but your server may be different. To make certain, look at the
information at the top of the page for the Loaded Configuration File entry, which shows the full
path to the ini file PHP is currently using.
Write a test page
Copy the following code to testext.php (or download it ) , save it under one of your web server’
s virtual directories.
<?php
$value = 14;
$result = DoubleUp($value);
print "Calling DoubleUp($value) returned $result";
?>
You can now browse to http://localhost/virtualpath/testext.php.
This article describes the steps to create a custom PHP extension DLL for the Windows platform.
The Zend API documentation that comes with PHP 5 on Windows (see php_manual_en.chm) does
a good job explaining how to write extension methods, parse method parameters, and return
values. But there is not currently a good step-by-step tutorial on how to get your first extension
project up and running on Windows. The aim of this article is to fill that gap.
Prerequisites
Visual Studio 2005
You can alternately use the free Visual C++ Express Edition or the VC++ 8 compiler in the
Windows SDK v6.0 if you’re a makefile master. Visual Studio 2003 (VC++ 7) will
probably work just fine, but some of the project configuration steps will be different than
what is explained here
•
A web server
For this article I used Sambar Server 7.0. Any HTTP server that can run the PHP 5.x ISAPI
extension (php5isapi.dll) will do
•
PHP 5 binaries, installed and configured for your server
I used PHP 5.2.4. Using the windows installer package (.msi file) makes configuration
easier
•
PHP 5 source code
DONT PANIC! You do not need to build PHP from source, just get the source that matches
your binary version. You will need an extraction utility like WinRAR or ZipGenius that can
handle .tar archives.
•
Conventions
Italics denote file paths and names. C:\Server\PHP\dev
Screenshot icons provide links to images showing how dialogs are configured.
Document icons provide links to sample code files.
Configuring the Environment
I will not discuss installing and running the HTTP web server. If you do not already have IIS
available on your machine, I recommend the Sambar Server.
If you do not already have PHP 5 installed, download it from php.net. If you download and run the
MSI installer package, it will configure your web server.
After you’ve got PHP 5 installed and configured for your server, download and extract the
complete PHP 5 source code, also from php.net. Caution: Do NOT extract the source archive
w 页码,1/20(W)
http://blog.slickedit.com/2007/09/creating-a-php-5-extension-with-visual-c-2005/ 2011-05-03
over top of your existing binary installation. This article’s example setup includes the PHP 5
binaries installed to C:\Server\PHP5, and the source code extracted to C:\Server\PHP5Src.
Creating the Project
In Visual Studio 2005, create a new Visual C++ Win32 project using the project template. For
this example, I named the project CustomExt. When the Win32 Application Wizard appears, click
Application Settings (on the left) and select DLL as the application type. Click Finish to
create the project.
I recommend following along step-by-step, but you can also download the complete project source
(zipped). You’ll still need to follow the instructions for changing the directories to match your
system.
Change Default C++ Options
Bring up the Project Properties dialog, and make sure the Debug configuration is active. Under
Configuration Properties > General, change the Character Set to “Use Multi-Byte Character
Set”.
Then under Configuration Properties > C/C++ > Code Generation, change the following
options:
• Enable String Pooling to “Yes (/GF)”
• Enable Minimal Rebuild to “No”
• Basic Runtime Checks to “Default”
• Runtime Library to “Multi-threaded Debug (/MTd)”
Under Configuration Properties > C/C++ > General, change the following:
• Debug Information Format to “Program Database (/Zi)”
• Detect 64-bit Portability Issues to “No”
Be sure to click Apply to save the settings.
Set the INCLUDE Paths
Still on the Project Properties dialog unders Configuration Properties > C/C++ > General, in
the Additional Include Directories, add the following paths, replacing C:\Server\PHP5Src with the
location on your machine where you extracted the source code.
C:/Server/PHP5Src/main
C:/Server/PHP5Src/Zend
C:/Server/PHP5Src/TSRM
C:/Server/PHP5Src/regex
C:/Server/PHP5Src
Again, be sure to click Apply to save the settings as you go along.
w 页码,2/20(W)
http://blog.slickedit.com/2007/09/creating-a-php-5-extension-with-visual-c-2005/ 2011-05-03
Set the Preprocessor Definitions
On the Project Properties dialog, under Configuration Properties > C/C++ > Preprocessor, add
the following definitions:
ZEND_DEBUG=0
ZTS=1
ZEND_WIN32
PHP_WIN32
Note: Do not be tempted to change ZEND_DEBUG to 1, even for this Debug build, as this will
prevent your extension from being loaded into the pre-built PHP binaries installed on your system.
Set the Linker Options
On the Project Properties dialog, under Configuration Properties > Linker > General add the
path to the C:\Server\PHP\dev directory to the Additional Library Directories. This is the
directory underneath your installed PHP binaries (not the extracted source). This directory should
contain php5ts.lib.
Under Configuration Properties > Linker > Input, add php5ts.lib to the Additional
Dependecies.
By convention, php extensions start with php_, so under Configuration Properties > Linker >
General, for the Output File option, I changed the output name to php_custom_ext.dll. This is
entirely optional.
The Extension Code
Replace the contents of stdafx.h with the following, or download it :
#pragma once
/* PHP Extension headers */
/* include zend win32 config first */
#include "zend_config.w32.h"
/* include standard header */
#include "php.h"
Replace the contents of ProjectName.cpp (in this example CustomExt.cpp) with the following (or
download it ):
#include "stdafx.h"/* declaration of functions to be exported */
ZEND_FUNCTION(DoubleUp);
/* compiled function list so Zend knows what's in this module */
zend_function_entry CustomExtModule_functions[] = {
ZEND_FE(DoubleUp, NULL)
{NULL, NULL, NULL}
};
/* compiled module information */
zend_module_entry CustomExtModule_module_entry = {
STANDARD_MODULE_HEADER,
"CustomExt Module",
CustomExtModule_functions,
w 页码,3/20(W)
http://blog.slickedit.com/2007/09/creating-a-php-5-extension-with-visual-c-2005/ 2011-05-03
NULL, NULL, NULL, NULL, NULL,
NO_VERSION_YET, STANDARD_MODULE_PROPERTIES
};
/* implement standard "stub" routine to introduce ourselves to Zend */
ZEND_GET_MODULE(CustomExtModule)
/* DoubleUp function */
/* This method takes 1 parameter, a long value, returns
the value multiplied by 2 */
ZEND_FUNCTION(DoubleUp){
long theValue = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
"l", &theValue) == FAILURE){
RETURN_STRING("Bad parameters!", true);
}
theValue *= 2;
RETURN_LONG(theValue);
}
You should now be able to build the project without any errors. Be careful when renaming
CustomExtModule or DoubleUp, and you must replace every instance in the file, or the Zend
macros will yield nearly indecipherable compiler errors.
Enable the extension
The first step is to locate the \ext directory beneath your PHP 5 binaries (not the directory where
you extracted the PHP source code). On my sample system this is C:\Server\PHP\ext. Copy the
DLL that you just built to this directory. If you are going to attach a debugger to your extension,
you will also want to copy the .pdb files.
The second step is to configure PHP to load your extension DLL. You do this by modifying
PHP.ini, adding a
1 extension=php_ext_name.dll
line to the Dynamic Extensions section, like the following.
;;;;;;;;;;;;;;;;;;;;;;
; Dynamic Extensions ;
;;;;;;;;;;;;;;;;;;;;;;
[CustomExt]
extension=php_custom_ext.dll
Verify that the extension can be loaded
Once you’ve edited and saved php.ini, you may need to restart the HTTP server in order to pick up
the new module. After the server is restarted, browse to the phpinfo.php web page. If you do not
already have a phpinfo.php page, create one using the following text, and save it to one of your
virtual directories:
<?php
phpinfo();
?>
Now browse to http://localhost/virtualpath/phpinfo.php, and look for the Additional Modules
section. Your new extension should now be seen in the listing. If your extension is not listed, you
may need to resave php.ini and restart the server. Also, make sure that the php.ini file that your
server is using is the same ini file that you just edited. On most servers, php.ini is located in the
w 页码,4/20(W)
http://blog.slickedit.com/2007/09/creating-a-php-5-extension-with-visual-c-2005/ 2011-05-03
PHP 5 installation directory, but your server may be different. To make certain, look at the
information at the top of the page for the Loaded Configuration File entry, which shows the full
path to the ini file PHP is currently using.
Write a test page
Copy the following code to testext.php (or download it ) , save it under one of your web server’
s virtual directories.
<?php
$value = 14;
$result = DoubleUp($value);
print "Calling DoubleUp($value) returned $result";
?>
You can now browse to http://localhost/virtualpath/testext.php.
#11
楼主你太狠了,把.dll文件当成.php来使用了,不过真的能这样的话也太好了。
ps:楼主你真的很狠
ps:楼主你真的很狠
#12
本人新手,请问:该如何生成php可以调用的dll?
#13
[img=http://bbs.php100.com/attachment/Mon_1107/18_151635_56ffb3955a14e73.jpg?24][/img]
php自带的ext文件夹下的dll注册不成功,找不到入口点DllRegisterServer...怎么回事?
php自带的ext文件夹下的dll注册不成功,找不到入口点DllRegisterServer...怎么回事?
#14
#3和#10已经说了
php 的 dll 必须是 php 的专有格式。虽然 dll 后缀被使用,但其真正的名字是 php扩展
php 的 dll 必须是 php 的专有格式。虽然 dll 后缀被使用,但其真正的名字是 php扩展
#15
因为他不是 com 应用,所以不可能被注册
#16
哦,原来ext文件夹下的dll们不是com应用!
我想实现 用户上传图片,自动调用dll里的函数进行处理,返回个结果给客户端浏览器.
这个dll该怎么做?
1,做成和/ext里一样的php专用dll
2,做com应用的dll
是这样吗?
一般采用哪种方式?
#17
php源码包原来是tar.gz包,我按你的写的步骤做了一遍,在第9步之前,下载了个config.w32.h到/php_src/main目录
编译得到dll,复制到ext下,在php.ini里添加extension=php_linvo.dll,重启apache,还是没达到结果,提示
Call to undefined function hello()
崩溃了