Use C language to extend PHP, write PHP extension DLL

Source: Internet
Author: User
Tags php source code

Transfer from http://www.cnblogs.com/myths/archive/2011/11/28/2266593.html

Previously written PHP extension DLL, which is the use of the call system COM port implementation of the extension, and PHP can not really merge. Whim, the study of the source of PHP, the Internet to find some information, their own attempt to write an extension DLL, test no problem. Here's a note on how to extend this:

1, first from the www.php.net website download php source code, here to php-5.2.17 version for example, download and extract to E: Packing directory (the directory can be arbitrarily set).

2, download and install VC + + 6.0, because the PHP source code is written using the 6.0 version, so using this version of the compilation will not occur unexpectedly, other versions are not tested.

3. Add the Microsoft Visual studio\common\msdev98\bin Absolute Path in the VC + + 6.0 installation directory to the system environment variable.

4, enter the E:\php-5.2.17\ext directory, copy the skeleton folder, and rename it to develop the extension name, this example is "Myfun".

5. Rename skeleton.c to MYFUN.C,SKELETON.DSP for MYFUN.DSP

6, edit the Myfun directory Php_skeleton.h, MYFUN.C, myfun.dsp These three files, the replacement content of all Extname is myfun,extname for Myfun. (Must be strictly case-sensitive)

The following steps are entered into the encoding phase:

7. Open php_skeleton.h file (header file), find Php_function (confirm_myfun_compiled);, in Php_function (confirm_myfun_compiled); Write Php_function (Mb_messagebox) below, declare a mb_messagebox function, this function is only output JS popup an alert message box for testing.

8, the following definition function entrance, open the Myfun.c file, find Php_fe (confirm_myfun_compiled,null), write Php_fe below (mb_messagebox,null), here note, Php_ Fe is a macro defined, so there is no need to enclose the quotation marks.

9. Write the entity part of the function on the last side of the MYFUN.C:

Php_function (Mb_messagebox)
{
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, "<script>alert ('%s ') </script>", Arg); Here is the input JS code
Return_stringl (STRG, Len, 0);
}

By the time this code is written, the following starts to compile:

10. Start and run, enter CMD, open the Command Line window.

11, enter the Myfun directory, input msdev myfun.dsp/make "Myfun-win32 release_ts", return to compile.

Note: "Php5ts_debug.lib" or "php5ts.lib" is not found when the PHP executable code (not the PHP source code) to find "Php5ts.lib" or "php5ts_debug.lib" placed in the Myfun directory

12, if there is no error, under the E:\php-5.2.17 will generate a Release_ts folder, in which you can find php_myfun.dll files.

Now that the extension DLL has been developed, test it in PHP below:

13. Copy the Php_myfun.dll to the Ext folder in the original PHP directory.

14. Open the php.ini file and add the extension of the current DLL Extension=php_myfun.dll

15. Restart IIS or Apache, create a new file under the Site directory, and enter the following:

<?php
echo mb_messagebox ("Test PHP extension DLL by Ma ben");
?>

Browse to see the results, the following four photos:

To add an extension:

PHP Code:

Final effect:

Extended information in Phpinfo:

Use C language to extend PHP, write PHP extension DLL

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.