June 2008 - Posts

I am Hiring - Developer Evangelist

I am looking for great people to join our team in Microsoft Indonesia. The Developer Evangelist (DE) is a member of the world wide team of Developer Platform Group within Microsoft Indonesia.  As a DE, you are Microsoft’s representative to the developer community helping to win the hearts and minds regarding the Microsoft platform. The DE is a technical role with the mission of engaging with the broad community of developers and driving excitement around developer related technologies. DE’s must be able to talk about technology intelligently and enthusiastically to Developers, Developer managers and senior management. Passion, curiosity, technical depth, and exceptional communication and presentation skills are required.

To make it simple, I am looking for DE who can:
- Write                 : Technical articles, books
- Speak                : Geeky presentation/demo of the truth about technologies
- Learn and share : Enjoy learn cool stuff (codes) and share with others

Responsibilities:
- Able to engage and build relationship with geeks
- Able to deliver articulate, effective, and audience-appropriate presentations/demos
- Participate in the online and offline developer community
- Convey a compelling and convincing case for Microsoft technologies
- Extend reach in the developer community (For example : PHP & Scientific programmers)
- Encourage participation of the developer community
- Maintain knowledge about versions of the same product and speak to future release

Qualifications:
- Unabated passion for technology and Microsoft, a keen eye for unobvious opportunities
- Strong understanding of security, reliability, scalability and platform management topics 
- Flexibility in regards to work schedule and travel
- Good marketing skills and business logic is a strong advantage (at least understanding)
- 5 – 8 years related experience : CLR, Visual Studio, .NET 3.x, Win32, C#, C++
- Unparalleled communication and negotiation skills
- Passion for technology and solid knowledge of enterprise IT requirements
- Solid understanding of the software development concepts, products and tools
- Can write codes, good in algorithm and debugging are big advantages.

Isn't that too much?? If you think you are capable and meet the requirements, I will be glad to talk to you. Send me email directly to : rismana at microsoft dot com. I offer equal opportunity for both male and female !  Send your CV soon. Deadline : July 15, 2008. I will read your CV and give you a call if I think you are meet 75% of requirements.

Ciao - RAM

Share this post: | | | |

Part 2 : Writing PHP Extension

In 1st Part of this article, we already warmed up by preparing your development environment and grab the concept of PHP internal (including IIS 7.0). In this part I will guide you to write your first PHP extension (the simplest one), with VC++ 2008 in Windows Vista. Continuing the previous two steps in Part 1, lets go to step 3.

3. Setup Build Environment with VC++ 2008
I assume you have installed IIS 7.0 and VC++ 2008. After you’ve got PHP 5 installed and configured for your server, download and extract the complete PHP 5 source code, also from www.php.net. DO NOT extract the source archive over top of your existing binary installation. This article’s  my PHP 5.2.6 binaries installed to C:\PHP, and its source codes extracted to D:\PHP on Windows\PHP-DEV\PHP-5.2.6.

In VC++ 2008, create a new Win32 project using the project template, name it FirstPHPExt. Select DLL app type in the VC++ Wizard and click finish.  Open your C++ Project Property form and do the following changes in:

Configuration Properties > General
- Character Set                                 --> Use Multi-Byte Character Set

Configuration Properties > C/C++ > General
- Debug Information Format             --> Program Database (/Zi)
- Detect 64-bit Portability Issues     --> No
- Additional Include Directories        --> Root, Zend, TSRM, Regex, and Main

01

Configuration Properties > C/C++ > Preprocessor

Add : ZEND_DEBUG = 0; ZTS =1; ZEND_WIN32; PHP_WIN32

02

Note: Do not be tempted to change ZEND_DEBUG to 1, even for Debug build, this will prevent our extension from being loaded into the pre-built PHP binaries installed on your system.

Configuration Properties > Linker > General
- Additional Include Directories        --> C:\PHP\Dev (where php5ts.lib located)
- Output Files                                     --> $(OutDir)\MyPHP-Custom.dll

03

Configuration Properties > Linker > Input
- Additional Dependencies                 --> php5ts.lib

Ok, now we are ready to write extension codes.

4. Writing PHP Extension Codes

We will write a simple PHP extension to calculate the double up value of an inputted parameter. We will use this method in PHP user-space like this:

<?php
      $value = 14;
      $result = DoubleUp($value);
      print "Calling DoubleUp($value) returned $result";
?>

Start with modifying stdafx.h header file to include PHP Extension headers.

#pragma once
/* PHP Zend Extension headers */
/* include zend win32 config first */
#include "zend_config.w32.h"
/* include standard header */
#include "php.h"

Then add the following codes to FirstPHPExt.cpp file:

#include "stdafx.h"

/* declaration of functions to be exported */
ZEND_FUNCTION(DoubleUp);

/* compiled function list so Zend knows what's in this module */
zend_function_entry FirstPHPExtModule_functions[] = {
    ZEND_FE(DoubleUp, NULL)
    {NULL, NULL, NULL}
};

/* compiled module information */
zend_module_entry FirstPHPExtModule_module_entry = {
    STANDARD_MODULE_HEADER,
    "FirstPHPExt Module",
    FirstPHPExtModule_functions,
    NULL, NULL, NULL, NULL, NULL,
    NO_VERSION_YET, STANDARD_MODULE_PROPERTIES
};

/* implement standard "stub" routine to introduce ourselves to Zend */
ZEND_GET_MODULE(FirstPHPExtModule)

/* DoubleUp function */
/* This method takes 1 parameter, a long value, returns the value multiplied by 2 */
ZEND_FUNCTION(DoubleUp){
    long paramValue = 0;
    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &paramValue) == FAILURE) {
        RETURN_STRING("Bad parameters!", true);
    }
    paramValue *= 2;
    RETURN_LONG(paramValue);
}

If you compile your project now, you will get the following error:

Error C3163: '_vsnprintf': attributes inconsistent with previous declaration   

Don't be panic, this is a common error in compiling old C/C++ codes in VC++ 2008 (I found it when I compiled Squid and Winpcap with VC++ 2008). In our case, zend_config.w32.h contains a re-definition of snprintf and vsnprintf that already defined in stdio.h. Change zend_config.w32.h then you will not see that error again:

#if not defined(_MSC_VER) || defined(__MINGW32__)
   #define snprintf _snprintf
   #define vsnprintf _vsnprintf
#endif

After the building the project, we get MyPHP-Custom.dll file (depends to Output file configuration) that is ready to deploy.

4. Deploying the Extension

Place the resulted DLL (MyPHP-Custom.dll) to PHP extension directory, in this case  C:\PHP\ext. If want to attach a debugger to this extension, you will also want to copy the .pdb files. After that, please configure PHP to load your extension DLL by modifying PHP.ini file, add  "extension=MyPHP-Custom.dll". Once you’ve edited and saved php.ini, restart your Application Pool in IIS 7.0 then browse index.php web page to test the result. If you do not already have index page, just create one using the following text, and save it to one of your virtual directories:

<?php
      phpinfo();
?>

Now browse the index.php and see that new extension should now be seen in the listing. If you are not lucky enough, please do some troubleshot :).

05 

Then the latest step is to test our extension (calling DoubleUp). Just modify the index.php as below:

 06

I perfectly get the right result in my browser :

07

Great... Now we understand how to build simple PHP extension using VC++ 2008 in Windows Vista. I will cover PHP extension debugging in other part of my article. What is left for the Part 3 of this article is explanation of many Zend functions appear in our codes. I will focus on Zend Framework in the Part 3 of this posting. I hope you will enjoy C++ codes like I did !


Hope this helps !!

Ciao - RAM

Share this post: | | | |

Part 1 : Writing PHP Extension

php-win

My friend Rama Yurindra from Zend told me that he wants to drive PHP (on Windows) together with us. Everybody knows that PHP already works on IIS since from beginning, so what make it is so attractive to me? The new FastCGI is cool, but I believe we can do more attractive stuffs to value PHP developers in Windows world.

Microsoft-Zend collaboration is started by Sam Ramji who runs Open Source Lab at Microsoft. He has hundreds of physical and virtual servers running 40+ distributions of Linux, 12+ variant of Unix, and several versions of Windows. Sam Ramji leads some interesting research projects including testing interoperability of network protocols like IPSEC and IPv6 between Linux and and Windows technology, the user experience and technical capabilities of HPC projects like ROCKS and Ganglia. SQL Server team also started to provide SQL Server Driver for PHP. You can download it here.

I was thinking to enable huge # of PHP developers and Websites in Indonesia to take advantages from Microsoft investments on Web technologies. Let say, Windows Live, AJAX, Silverlight, Infocard, Windows HPC, etc. A lot of cool stuffs (Live ID, IM, Virtual Earth, Parallel/Concurrency stuffs) nowadays we can bring as extensions to PHP. Isn't that interesting ?? Of course yes ! And actually I had initial conversation with my friend Angus Logan to start work on PHP Extension for Windows Live.

My goal in this article is to encourage Indonesian developers to start working on non-LOB (line of business) applications and start playing in the international play ground. Yes, I really want to see that happen and PHP extension is just one work item in my plan. It means, we have to back to C++ native world again (this time not for molecular dynamic simulation). For you who want to follow my C++ journey, I will send you my collection of C++ How Do I videos in DVD if you put your shipping address in this blog as comment. Let start by writing simple PHP Extension first, then we can brainstorm more on writing extension for Windows Live.

1. Preparation for Your 1st PHP Extension

- Download and install Visual C++ 2008 (express edition is okay)
- Windows Vista Platform SDK (will be included in DVD)
- Windows Vista SP1 with IIS 7.0
- Install PHP 5.x.x in Windows Vista & IIS 7.0 (FastCGI/ISAPI/CGI)
- Download and extract PHP 5 Source Codes in PHP-DEV directory
- Download Zend Framework and Its documentation. I use Zend Framework 1.5.2.

Note : If you done with all of those steps, you are ready !

2. Understand PHP Internals

If you are not getting PHP internal knowledge (once you dive into a working extension it should make sense to understand), I recommend you read PHP manual first then consult to Sara Golemon and Black Schwendiman books. Some resources from Edin blogs and Sara article are also useful.

 PIC01    PIC02 

Every PHP developers should familiar with extensions. You can find the list of extensions in PHP.INI file. Most of PHP functions are part of the standard extension - not in the PHP core itself. The PHP source bundle comes with around 86 extensions, the PECL repository offers over 100 additional extensions.

How PHP Works in IIS ?

Lets think PHP as two core subsystems : Zend Engine (ZE) and PHP Core Engine (PCE). ZE handles parsing a PHP script into machine-readable tokens, and then executing those tokens within a process space. ZE also handles memory management, variable scope, and dispatching function calls. PCE handles communication with, and bindings to, the SAPI handlers (Server Application Programming Interface). PCE also provides a unified control layer for safe_mode and open_basedir checks, as well as the streams layer which associates file and network I/O with user-space functions like fopen(), fread(), and fwrite().

IIS 7.0 implemented an integrated pipeline architecture to manage all HTTP requests coming from kernel mode listerner (HTTP.SYS). The latest module before an HTTP request reach the SAPI handler is execute_handler who will dispatch HTTP request to its proper handler module. IIS 7.0 provides three SAPI handler modules for PHP: CGI (cgi.dll), ISAPI (isapi.dll) and FastCGI (iisfcgi.dll) modules. I strongly suggest you to understand internal architecture of each handler to match your hosting requirement. In IIS 7.0, you can check your IIS configuration file (applicationHost.config) to check whether you have all of those SAPI handler modules exist or not.

<add name="FastCgiModule" image="%windir%\System32\inetsrv\iisfcgi.dll" />
<add name="IsapiModule" image="%windir%\System32\inetsrv\isapi.dll" />
<add name="CgiModule" image="%windir%\System32\inetsrv\cgi.dll" />

PHPOPS    PHPDLL 

Each IIS SAPI module handler will communicate to its associated PHP Core Subsystems (php-win.exe, php-cgi.exe, php5isapi.dll) through TCP or named pipes. When a given IIS SAPI handler starts-up to handle HTTP request, PHP begins by initializing its core subsystems (ZE and PCE). Towards the end of this startup routine, it loads the code for each extension and calls their Module Initialization routine (MINIT). This gives each extension a chance to initialize internal variables, allocate resources, register resource handlers, and register its functions with ZE, so that if a script calls one of those functions, ZE knows which code to execute.

PHPFCGI

Next, PHP waits for the SAPI handler module to request a page to be processed. In the case of the CGI, ISAPI or FastCGI handler module in IIS. This happens immediately and only once and all request are queued before being processed. No matter how the request comes in, PHP begins by asking ZE to setup an environment for the script to run in, then calls each extension's Request Initialization (RINIT) function. RINIT gives the extension a chance to set up specific environment variables, allocate request specific resources, or perform other tasks such as auditing. Once the request is initialized by RINIT, ZE takes over by translating the PHP script into tokens, and finally to opcodes which it can step through and execute. Should one of these opcodes requires an extension function to be called, ZE will bundle up the arguments for that function, and temporarily give over control until it completes.

After a script has finished executing, PHP calls the Request Shutdown (RSHUTDOWN) function of each extension to perform any last minute cleanup (such as saving session variables to disk). Next, ZE performs a cleanup process (known as garbage collection) which effectively performs an unset() on every variable used during the previous request.

IIS

Once completed, PHP waits for IIS SAPI handler module to either request another document or signal a shutdown. In the case of the CGI, there is no "next request" because each HTTP request invokes exactly one OS process, so the SAPI initiates a shutdown immediately. During shutdown, PHP again cycles through each extension calling their Module Shutdown (MSHUTDOWN) functions, and finally shuts down its own core subsystems. ISAPI and FastCGI has different mechanism in handling HTTP request. CGI is too costly in IIS because OS process creation in Windows is expensive comparing to thread creation. FastCGI improves the CGI mechanism by allowing reuse of an OS process, while ISAPI is loaded as extension to IIS worker process.

In order to avoid losing memory to poorly written extensions, PHP-ZE performs its own internal memory management using an additional flag that indicates persistence, lets called it Zend Memory Management (ZendMM) layer. This portion of ZendMM acts in much the same way the operating system would normally act, allocating memory to calling applications. The difference is that it is low enough in the process space to be request-aware so that when one request dies, it can perform the same action the OS would perform when a process dies. That is, it implicitly frees all the memory owned by that request.

PHPZE

A persistent allocation in ZendMM is a memory allocation that is meant to last for longer than a single page request. A non-persistent allocation, by contrast, is freed at the end of the request in which it was allocated, whether or not the free function is called. User-space PHP variables, for example, are allocated non-persistently because at the end of a request they're no longer useful.  While an extension may, in theory, rely on ZE to free non-persistent memory automatically at the end of each page request, this is not recommended. Memory allocations will remain un-reclaimed for longer periods of time, resources associated with that memory will be less likely to be shutdown properly, and it's just poor practice to make a mess without cleaning it up. 

So.... when we are going to write our 1st PHP extension? I am a fans of bottom-up learning and I think it is important for you to understand the internal of PHP before writing extension codes. We will start to write codes in Part 2.

Hope this helps !!

Ciao - RAM

Share this post: | | | |

Compiling PHP Source Codes in Vista with VC++ 2008

I tried to compile it in my "lovely" Vista machine following the guidance from this folk. Hopefully this can help you guys who are crazy enough (especially Sagi Arsyad) to jump in into deeper PHP exploration.

1. Preparation
- Visual Studio C++ 2008 (You can use free VC++ 2008 Express Edition, VCE++)
- Microsoft Windows SDK for Vista
- PHP Source Code from www.php.net (I am using PHP 5.2.6)

Ok, let start.....

First, just create your working directory, let say, D:/PHP-DEV then extract PHP source codes there. You need to have all libraries and header files of PHP and it's extensions that are not distributed together with PHP source package. We are lucky that Edin Kadribasic has collected it from many websites and share with us here. So, just download it and once you had the zip.zip file, copy it to your working directory and extract it there.

2. Compiling
Run Visual Studio 2008 Command Line Tools with Administrator right, then navigate to your PHP source codes folder. Run buildconf.bat then you will get message : "rebuilding configure.js". You can learn about it using the following command:

cscript /nologo configure.js --help

PHP

Lets try to use the following configuration:

cscript /nologo configure.js --without-xml --without-wddx --without-simplexml --without-dom --without-libxml --disable-zlib --without-sqlite --disable-odbc --disable-cgi --enable-cli --enable-debug --without-iconv

I got the following notification about bison.exe when I run it. Dont be panic, you should be patient enough with error messages when working with C++.. heheh. Just try to find bison.exe and flex.exe from zip.zip extracted folder, and copy it to your VC++ default path. In this case is : C:\Program Files\Microsoft Visual Studio 9.0\VC\BIN.

PHP02

Now, lets try to run your cscript again (blue color above), try find the <not found> files from the script results. In my case, I need to find where are : re2c.exe, zip.exe, lemon.exe, NewAPIs.h, and libjpeg.lib. You will not get everything just find in your first compilation (trust me). But at least when you have the following message, just take some 2 minutes break.

PHP03

What next ??? just run nmake !!!. Bingo!! It is started to compile. It will take sometime to compile in your machine, grab a glass of coffee because you will get many errors (don't be crazy because of it). Indeed, it is your job as developer to understand what it is.

Ok, if you want to know more, open the makefile generated by the script then evaluate its content (easy to understand).

# Generated by configure.js
PHP_VERSION_STRING=5.2.6
CL=cl.exe
LINK=C:\Program Files\Microsoft Visual Studio 9.0\VC\BIN\link.exe
NMAKE=nmake.exe
MAKE_LIB=lib.exe
BISON=bison.exe
FLEX=flex.exe
MC=C:\Program Files\Microsoft SDKs\Windows\v6.0\bin\mc.exe
MT=C:\Program Files\Microsoft SDKs\Windows\v6.0\bin\mt.exe
PHP_PREFIX=C:\php5\debug
..... bla bla bla

Change whatever do you want in your cscript options and generated makefile then run it again and again till you get now error. I can only say, Good Luck dude !!

3. Enjoy Your Weekend with C++!

I will post more about creating PHP extension with VC++ later ..... stay tuned !!


Hope this helps - RAM

Share this post: | | | |

WSS 3.0 in My Vista Machine

Finally, no more VPC to run WSS in Windows Server. Now I have WSS 3.0 SP1, Visual Studio 2008 Extension for WSS 3.0, WSS 3.0 SDK and Sharepoint Designer running well in my Vista machine. Not only that, I can configure the WSS to run in Farm model with SQL Express 2005. DNN, WSS and PHP can run together in my machine now :). Peace!

If you want to follow me to ready develop Sharepoint apps in Vista, do below tasks:

- Install Vista (+SP1)
- Install SQL Server 2005 Express
- Install WSS 3.0 SP1. Just follow this link from Jonas Nilsson.
- Install Visual Studio 2008.
- Install VS Extension for VS 2008, WSS SDK, and Sharepoint Admin Toolkit.
- Install Sharepoint Designer 2007.

My IIS 7.0 Looks like:
01

Sharepoint Central Administration:
02 

Visual Studio with WSS 3.0 Extension:
 05

Sharepoint Designer 2007 in Action:
04

Hope this helps !

Ciao - Risman Adnan

Share this post: | | | |

PHP Developer Day | Invitation

PHP again.... looks like PHP becomes hot topic !

This time, not only Microsoft and Zend will participate, some big names like Romi Satria Wahono, Luri Darmawan, Rama YurindraRiyogarta, will also be there. Please register soon phpdeveloperday@live.com and meet them in this great event.

PHP Day

My demo session will more specific web performance test with VS Team Tester 2008. I will show you how to test PHP applications with VSTT 2008 (Web recorded and load tests). I have Mambo, PHPBB, Wordpress, Joomla, and PHPNuke installed and run well with FastCGI in my Vista machine. Those applications could be our test targets, but single machine testing scenario will not be enough. Let see if I can have multi-machines.

For you who dont have VSTT, you can use WCAT tool, a FREE lightweight HTTP load generation tool from Microsoft.

See u - Risman Adnan

Share this post: | | | |