How to make a php extension with Visual Studio
Posted by: developer68 (---.pkbgcmtk01.res.dyn.suddenlink.net)
Date: June 21, 2016 04:53AM

I just thought I'd throw this out to the public about how to make a php extension so you can use custom functions in your php scripts. So here we go:

Download the php source library for the version that wamp has. [windows.php.net]

Step 1. Create a new project and select "General" under Visual C++ and the empty project option.
Give it a name and Click ok. Mine is Project1 so I will be using that.

Step 2. Right click the solution name and select properties. Select Dynamic Library (.dll) Under Project Defaults/Configuration Type in the Configuration Properties/General item. Be sure that you are changing the Debug config as Release is not used.

Step 3. Under VC++ Directiories add the following under the Include libraries:
C:\php-5.6.22-src
C:\php-5.6.22-src\main
C:\php-5.6.22-src\TSRM
C:\php-5.6.22-src\win32
C:\php-5.6.22-src\Zend
Library Directories needs C:\wamp\bin\php\php5.6.15\dev

Step 4. Under C/C++ Preprocessor add ZEND_DEBUG=0;ZTS=1;ZEND_WIN32;PHP_WIN32 to Preprocessor Definitions

Step 5. Under Linker Input add php5ts.lib

Click ok to save the Property settings for your solution.

Now lets add a code file for your function (Project1)

Under the source files folder right click and add Code. I called mine Project1.cpp and here is the source. You'll need to rename config.w32.h.in to config.w32.h and copy it to the directories where it is needed. Intellisense should tell you where it's looking and config.w32.h.in is in the C:\php-5.6.22-src\win32 directory.

// this needs to match the compiler version that wamp was compiled with
#define PHP_COMPILER_ID "VC11"

#pragma once
/* You must include config.w32.h first */
#include "win32/config.w32.h"

#include "php.h"

ZEND_FUNCTION(ReturnString);

zend_function_entry Project1_functions[] =
{
ZEND_FE(ReturnString, NULL)
{
NULL, NULL, NULL
}
};

zend_module_entry Project1_module_entry =
{
STANDARD_MODULE_HEADER,
"Project1 Module",
Project1_functions,
NULL, NULL, NULL, NULL, NULL,
NO_VERSION_YET, STANDARD_MODULE_PROPERTIES
};

ZEND_GET_MODULE(Project1)

ZEND_FUNCTION(ReturnString)
{
zval* value;

if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &value) == FAILURE)
{
RETURN_FALSE;
}

convert_to_string(value);

RETURN_STRING(Z_STRVAL_P(value), true);
}

Press F7 to compile and it should create Project1\Debug\Project1.dll. Copy Project1 to your ext directory C:\wamp\bin\php\php5.6.15\ext and add an extension=Project1.dll to your php.ini and you should be good to go. If it doesn't work double check your properties to make sure you didn't miss something. If wamp doesn't serve the page then there is something wrong in the cpp file.

The script is quite simple:

<?php
echo ReturnString("The Returning String" ) ;
?>

I hope this helps with making windows extensions for php.



Edited 1 time(s). Last edit at 06/21/2016 04:55AM by developer68.

Options: ReplyQuote
Re: How to make a php extension with Visual Studio
Posted by: SimonT (---.lightspeed.stlsmo.sbcglobal.net)
Date: June 21, 2016 05:08AM

Haven't tried it but if you were to install the PHP script on a hosting company server, the extension would not be available, and thus the function would not be available, and somehow I think they probably would not be willing to install your extension.

Might be fun to play with but for "production" use on the Internet, you'd have to go with whatever extensions the hosting company has installed or is willing to install.

And, of course, you'd need Visual Studio which is a bit pricey.

Options: ReplyQuote
Re: How to make a php extension with Visual Studio
Posted by: developer68 (---.pkbgcmtk01.res.dyn.suddenlink.net)
Date: June 21, 2016 05:14AM

Just something if you host your own wamp. Some companies might have their own legacy applications for interfacing with their web presentations. You just never know who might want to write something for their own stuff!



Edited 2 time(s). Last edit at 07/08/2016 07:32PM by developer68.

Options: ReplyQuote
Re: How to make a php extension with Visual Studio
Posted by: SimonT (---.lightspeed.stlsmo.sbcglobal.net)
Date: June 21, 2016 06:49AM

Understand completely.

Options: ReplyQuote
Re: How to make a php extension with Visual Studio
Posted by: developer68 (---.pkbgcmtk01.res.dyn.suddenlink.net)
Date: June 27, 2016 03:36AM

Oh these links may help for development.

[devzone.zend.com]

Visual Studio Express is a free IDE from Microsoft and is quite good for writing Windows Apps.

[www.visualstudio.com]

Options: ReplyQuote


Sorry, only registered users may post in this forum.