Page 1 of 1

Creating a Php Extension in C++ Ever wanted to know how? Rate Topic: -----

#1 aaron1178  Icon User is offline

  • Dovakiin, Dragonborn
  • member icon

Reputation: 169
  • View blog
  • Posts: 1,297
  • Joined: 22-October 08

Posted 03 August 2010 - 03:19 AM

Creating a Php Extension in C++

Welcome, in this tutorial, i will explain how to write your own php extension.
I will explain in easy to understand steps, how you could write your own php extension.
This tutorial is set for intermediate C++ users.

Introduction

Quote

What is php, and how would i create a php extension?

Well, php is a widely used all purpose scripting language that is specifically suited for Web development. Some php knowledge is required for this tutorial, so if your new and do not wish to tackle this tutorial first up, the why not check some php tutorials out at these web sites.


In this tutorial we will be using the zend.h file.

What is Zend: The Zend Engine refers to Php's core. The Zend Engine is the heart of php. So without the Zend Engine, there would be no php :)
What is Php: Php stands for Php:HypertextPreProcessor. This refers to the COMPLETE Php system.

What You Will Need

In this tutorial, a few things are needed to follow it on through.

First up, for the purpose of this tutorial, i am using Microsoft Visual C++ Express 2008. Witch can be downloaded Here for Free. Though the time consumption downloading it is long, its worth the wait. :) If you wish to use another IDE for this tutorial, please feel free, though, this tutorial is written for Microsoft Visual C++ Express 2008.

The second thing you will need, is a working local server. I specifically use wamp server, witch can be downloaded Here for Free. I will explain how to setup wamp server and the server side to things later.

You will also need to download the Php 5.2.5 Version for wamp sever witch can be downloaded from the source forge web site Here

Also, another thing is, the actual php 5.2.14(Witch will suit php 5.2.5) source files that can be downloaded Here @ Php.net.

Ok, now on to setup and configure our server and source codes.


When you have downloaded the 3 Server files

  • Wamp Server
  • Wamp Server Php 5.2.5 Version
  • Php 5.2.14 Source files


You should have something that looks like this: Fig 01.

Installing Wamp Server

Please follow these instructions on how to install Wamp Server

  • Run WampServer.exe - A screen will pop up, click next like fig 02
  • A new screen will appear, click i accept if you accept else click i do not accept to agreement, then click next like fig 03
  • Then, when you are at the Install Directory part, just click Next, to let it install it to its usual directory, see fig 04
  • Next, when it asks you if you want a desktop shortcut ect, click both check boxes as shown in
    fig 05
  • Bearing the end, you will come to a screen with the install information, click "Install" to install wamp, as shown in fig 06
  • Once it has installed, you will be prompt with a message about your browser, just click "Ok/Yes".
  • Then, another screen will show specifying stuff about your, mail server and all, just click ok for defaults, as shown in fig 07
  • Finally, you have come to the last screen, click Ok to launch Wamp Server, as shown in fig 08


Ok, by now, you should of installed either Microsoft Visual C++ Express 2008 or your own IDE choice and a working local server.

Installing PHP 5.2.5 for wamp

This process is very much similar to the one above, except it only has 4 windows, just keep clicking next until it has done. :)
When the process is complete, it will show a screen like fig 07, just click next, then when it says "Launch Wamp Server" Click next.

Was that easy enough? Hope so, because the following Installation step, might be a bit difficult for you. -.-

Installing the Php 5.2.14 Source Codes
Ok, in this installation step, i will tell you how to install the Php 5.2.14 source codes.

Ok lets get started.

  • Ok, Now, when you have downloaded the source files, it should be in a zip file.
  • Extract them to the following directory: "C:\wamp\bin".
  • Once done, there should be a file called "php-5.2.14" with a lot of sub folders. This means you have installed the source codes correctly
  • That's it, your done


Now you are ready to make your own Php Extension in C++

Creating a Php Extension in C++
In this part of the tutorial we will
  • Creating Our Project
  • Setup Microsoft Visual C++ Express to our needs
  • Create our Php Extension
  • Make our Php Accessible in php
  • Testing Our Php Extension


Creating Our Project
In this sector, we will create and name our project.
In Microsoft Visual C++ Express 2008, Go to the file tab and click New->Project
Next, a screen will show up called "New Project".

In the "Project Types Window(The one to the left) click the little arrow in front of Visual C++.
This will expand a menu with the following contents.
  • CLR
  • Win32
  • General


Next, click the "General" tab. To the right, in the "Templates" Window, click "Win32 Project".
We are hiking through this tutorial :)
Now, below, you will see some fields.
Enter a name into the name field, for the purpose of this tutorial, we will use "tutorial".

Click ok, this will bring up a screen called "
Win32 Application Wizard - Tutorial", click next.

By now you should see a window with "Application Settings". Stay on the page and under "Application Type" click "DLL", then click finish.

Now, following the finish event, a window should open in the text editor called "Tutorial.cpp", which should contain this piece of code:
// tutorial.cpp : Defines the exported functions for the DLL application.
//
#include "stdafx.h"



Now, to set up our IDE "Microsoft Visual C++ Express 2008"

Setting Up Microsoft Visual C++ Express 2008 To Our Needs
Ok, there is a little bit of stuff to take care of before we create our php extension, the following will allow us to compile our extensions.

This will tell the compiler, where to find all of the zend functions.

Ok, lets get moving, shall we!

Ok, to start, navigate to the "Solution Explorer"(To The Left).
Right Click on your project, and navigate to "Properties".
Once "Properties" has been clicked, a window will open called "tutorial - Properties Page".
Navigate to "Configuration Properties -> C/C++". In the window to the right, you should see 8 fields.
The first field should be called "Additional Include Directories" click on the field, and a button to the right with a few dots "..." should pop up. Click it.

Next, another window should open called "Additional Include Directories. to the right, there are a bunch of buttons. You should see a button with a folder, click it or press "ctr->insert".

This should automatically add an entry to the box. Click on the entry and click the browse button "...".
Navigate to the following directory:
"C:\wamp\bin\php-5.2.14" - Where you extracted the php 5.2.14 source code to.

Then repeat the

Quote

You should see a button with a folder, click it or press "ctr->insert".

This should automatically add an entry to the box. Click on the entry and click the browse button "...".
Navigate to the following directory:
process with the following directories:
  • C:\wamp\bin\php-5.2.14
  • C:\wamp\bin\php-5.2.14\TSRM
  • C:\wamp\bin\php-5.2.14\Zend
  • C:\wamp\bin\php-5.2.14\Regex
  • C:\wamp\bin\php-5.2.14\Main


Then click "Ok".
This should bring you back to the "Configuration Properties -> C/C++".

Now, to set up some processor stuff.

We need to navigate to "Configuration Properties -> C/C++ -> Preprocessor". To the right you should see a field called "Preprocessor Definitions". Click the Browse button "..." button, this will open a window called "Preprocessor Definitions", do you see this line "TUTORIAL_EXPORTS"? add this directly beneath it.
PHP_WIN32
ZEND_WIN32
ZTS=1
ZEND_DEBUG=0


Now it should look like this:
WIN32
_DEBUG
_WINDOWS
_USRDLL
TUTORIAL_EXPORTS
PHP_WIN32
ZEND_WIN32
ZTS=1
ZEND_DEBUG=0



Press ok and you should come back to the "Configuration Properties -> C/C++ -> Preprocessor" area.

Hard work :)

Its all worth it though.

Now, Navigate to "Linker -> General" and find the field called "Additional Library Directories". Add the following directory to the field "C:\wamp\bin\php\php-5.2.5\dev".

Next, Navigate to "Configuration Properties -> Linker -> Input", do you see a field called "Additional Dependencies", add this to the field "php5ts.lib"

Now, Navigate to "Configuration Properties -> Linker -> Command Line" and in the box on the bottom add this to the field "/FORCE:MULTIPLE"

One last thing :)

Now, Navigate to "Configuration Properties -> Linker -> General" and add the following to the output field "$(OutDir)\tutorial.dll"

Now click apply, then ok. This is it for this part of the tutorial, next, we will write our php extension.

Creating a Php Extension in C++

Ok, now we get to the fun part of this tutorial :)

First up, i will display the full code of our php extension.

The extension will make a function called "Tutorial" and it will be called in php by using the following function(s):

tutorial();

or
tutorial();



#include "stdafx.h"

ZEND_FUNCTION(tutorial);

zend_function_entry tutorial_functions[] = {
ZEND_FE(tutorial, NULL)
{NULL, NULL, NULL}
};

zend_module_entry tutorial_module_entry = {
STANDARD_MODULE_HEADER,
"Tutorial Info",
tutorial_functions,
NULL, NULL, NULL, NULL, NULL,
"1.0 - Alpha",
STANDARD_MODULE_PROPERTIES
};

ZEND_GET_MODULE(tutorial);

ZEND_FUNCTION(tutorial)
{

bool useHtml = false;

if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &useHtml) == FAILURE)
{
RETURN_STRING("Missing Parameter", true);
}

if (useHtml == true)
{
php_printf("<a href=\"http://studios.thompsonhosting.com\">Thompson Gaming Studios</a>");
}
else
{
php_printf("http://studios.thompsonhosting.com");
}
}





if you know C++, you probably understand most of it.

Now, see the
#include "stdafx.h"


Hover your mouse on over it and right click, click "Open Document "stdafx.h".

Now, replace the contents with this code:
//Php - Lite extension header file

#pragma once

//Includes the zend_config.w32 header file and the php header file
#include "zend_config.w32.h"
#include "php.h"  



Ok, we have to preform a little modification to the "zend.config.h" header file.

Right click, and click "Open Document "zend.config.h"".

Now scroll down to round about line "51" until you see a line that says "#define vsnprintf _vsnprintf".

Have you found it? Comment it our or remove it, this will have no affect on your extension.

Then,go to "Build -> Build Solution".

Make our Php Accessible in php

To make our php extension in php, we need to edit the "php.ini file.

First up, Open "Php.ini", scroll down to the extension part and add "extension=tutorial.dll"

Save the file and then restart all Wamp Servers Services.

And that's it, all that to enable our php extension :)

Next, we will test our php extension

Testing our own Php Extension

Ok, here, we will test our php extension we have just made.

Open the IDE of your choice that is able to open and edit php file. I use Adobe Dreamweaver CS5.

Create a new page called "test_extension.php". Add the following code to it.
<?php
echo "This link is using Html: ".tutorial(true)."<br />";
echo "This link is not using html: ".tutorial();
?>


That will produce

Quote

This link is using Html: Thompson Gaming Studios
This link is not using Html: http://studios.thompsonhosting.com


Save the file, and then go to your browser and type in the address "http://localhost/test_extension.php"

If the above worked, then you have a full functional php extension.

Conclusion

By the end of this tutorial, you should know how to create a Php Extension, add a function to the extension and making it work in php.

I hope this was easy to read, i hope you have gained something out of it

Thanks for reading

Thanks Aaron1178

Is This A Good Question/Topic? 1
  • +

Replies To: Creating a Php Extension in C++

#2 KunyRedjive  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 28-November 10

Posted 29 November 2010 - 12:28 AM

FREAKY TUTORIAL

This post has been edited by macosxnerd101: 29 November 2010 - 05:31 AM
Reason for edit:: Removed huge quote

Was This Post Helpful? 0
  • +
  • -

#3 aaron1178  Icon User is offline

  • Dovakiin, Dragonborn
  • member icon

Reputation: 169
  • View blog
  • Posts: 1,297
  • Joined: 22-October 08

Posted 29 November 2010 - 04:11 PM

Freaky? How, it explains how to create a php extension to use on a web server?
Was This Post Helpful? 0
  • +
  • -

#4 Guest_Piliskner*


Reputation:

Posted 02 December 2010 - 05:50 AM

Hello I have gone through this tutorial and there is one little mistake in

Quote

Now, Navigate to "Configuration Properties -> Linker -> General" and add the following to the output field "$(OutDir)\tutorial.dll"


you should not add that content to that field but instead overwrite whats in the field with this or it will fail on the dll file pathname.
Was This Post Helpful? 0

#5 aaron1178  Icon User is offline

  • Dovakiin, Dragonborn
  • member icon

Reputation: 169
  • View blog
  • Posts: 1,297
  • Joined: 22-October 08

Posted 12 August 2011 - 02:48 AM

This is an out of date tutorial, please do not use with Visual C++ express 2010. I am currently working on putting a tutorial together for creating a php extension in Visual C++ express 2010 :) I am working out some bugs at the moment and should begin to write it soon.
Was This Post Helpful? 0
  • +
  • -

#6 karimkhan_it  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 07-January 11

Posted 19 June 2013 - 08:32 AM

Fatal error: Call to undefined function tutorial() in D:\wamp\www\test.php on line

My project build successfully and I copied dll fine in approprite folder.

Even it is giving function not defined error and in case of phpinfo(); nothing about tutorial!
Was This Post Helpful? 0
  • +
  • -

#7 aaron1178  Icon User is offline

  • Dovakiin, Dragonborn
  • member icon

Reputation: 169
  • View blog
  • Posts: 1,297
  • Joined: 22-October 08

Posted 04 July 2013 - 06:52 PM

Outdated and Ill-Explained

I deem this tutorial unfit for active learning material. I have since written a newer more in-depth tutorial on the same subject which is available here:



All support will be provided on the page above.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1