Quantcast
Channel: WampServer - WampServer English
Viewing all articles
Browse latest Browse all 3177

How to make a php extension with Visual Studio (3 replies)

$
0
0
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.

Viewing all articles
Browse latest Browse all 3177

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>