📜 ⬆️ ⬇️

PHP module is just

We recently published a wizard for VisualStudio, with which you can create an extension in a couple of mouse clicks. Now, with the help of it, we will write our first two extensions: "Hello, the world" and "get the icon out of exe."
Immediately I apologize for delaying the article very much, but life circumstances forced it to be done, but they are extremely respectful.




')
So, let's begin.
1. Downloading the “wizard” for VS 2008.
By reference from the topic VS wizard: PHP extension
Install it, it will happen automatically.

2. Download the necessary files for the assembly.
Only source PHP and binaries are needed. Download 5.2.11 version of both files.
Unzip php-5.2.11-Win32.zip in C: \ PHPDEV \ php-5.2.11-Win32 and php-5.2.11.tar.bz2 in C: \ PHPDEV \ php-5.2.11.

3. Run VS, create a new project.

And enter its name. Ways to tune do not have to;)

After that, we see the main window of the studio, see what is in the files.


4. We create functions.
As already noted, the skeleton is completely created, it remains only to write functions and register them.
The project has a test function, uncomment it.
For reference:
1) The function header should be in the h file. In the form of PHP_FUNCTION (function_name).
2) Definition - in the c file.
3) The function must be written in function_entry test_functions in the c file. In the form of PHP_FE (function_name, NULL).
How to write the function itself, I will tell later. For now we will be limited to this:
  PHP_FUNCTION (hello_world) {
     RETURN_STRING ("Hello World", 1);
 } 


5. Build and launch.
We collect in release. Gathered.
Create a directory C: \ PHPDEV \ test
Copy there php.exe and php5ts.dll from the directory.
Copy the assembled dll under the name test.dll.
Create php.ini:
extension_dir = .
extension = test.dll

Create test.php with the string <? = Hello_world ()?> And run it in the console.


6. Advanced feature creation.
We will understand how to take values ​​from functions and pass them.
The difficulty lies in the fact that the function accepts and returns various values ​​of different types.
Consider an example in which a string and an integer are received and a string is returned.
  PHP_FUNCTION (foo) {
     char * input;
     int inputLength;
     long multy;
     char * result;
     if (zend_parse_parameters (ZEND_NUM_ARGS () TSRMLS_CC, "sl", & input, & inputLength, & multy)) {
         return;
     }
     result = (char *) emalloc (strlen (input) + sizeof (long) + 3);
     sprintf (result, "% s,% ld", input, multy);
     RETURN_STRING (result, 1);
 } 

As you can see, the following constructions are used here:
zend_parse_parameters (ZEND_NUM_ARGS () TSRMLS_CC, format_string, addresses_for the received_values)
RETURN_;
Consider 2 tables, the first shows the accepted PHP-types and the corresponding formats and types of C.
In the second return values ​​with the corresponding constructions.
In order not to bother myself, I enclose photographs of tables from a book that I advise everyone to read.


Once again we look at the examples above and understand how simple it is.
By the way, I would like to draw your attention to the fact that memory allocation is conducted through e-analogues of c functions (emalloc, efree, erealloc), this is necessary in order for GC PHP to be able to “tidy up”.

7. A useful example. Get the icon out of exe.
Of course, you can write it in PHP, but there will be more work. And here already have the necessary headers.
Let's write the code in C (wrote the odept bl4de):
In the file pe.h, we see the use of pieces of code from the windows libraries: they will help us, but their direct connection is impossible, we are writing a cross-platform extension, aren't we? ;)
In pe.c we write the code. As is clear, we will wrap the function void _extract_ico (char * filename, char * filenameOut).
  PHP_FUNCTION (extract_ico) {
     char * filename;
     char * filenameOut;
     int len1, len2;
     if (zend_parse_parameters (ZEND_NUM_ARGS () TSRMLS_CC, "ss", & filename, & len1, & filenameOut, & len2)) {
         return;
     }
     _extract_ico (filename, filenameOut);
     RETURN_NULL ();
 } 

Nothing complicated, as we see. Simple and clear. Here such files turned out as a result: php_ico.c and php_ico.h .

8. Bonus: build under * nix
The bonus is in the presence of the config.m4 file, so to build you just need to run a series of commands:
phpize
./configure
make
Naturally in the system should be the appropriate packages.
For ubuntu 9.10 server this is done like this:
sudo apt-get install build-essential php5 php5-dev
We take the collected so-library and we register in extensions. Is done.
Yes, * nix is ​​much more convenient for a web programmer, among my friends there is a mix-up on Windows. Many prefer ubuntu or poppy =)

9. Literature and useful links.
1) George Schlossneigl. Professional PHP programming. You can buy at books.ru , there is cheaper than all of the popular stores IMHO. If you want to support me, then click this link =)
2) Extension Writing Part I: Introduction to PHP and Zend
3) PHP: internals: windows [PHP Wiki]

PS And next time I will tell you how to embed PHP in your C / C ++ application. But I will not promise that it will be "the other day."

Source: https://habr.com/ru/post/75388/


All Articles