Saturday 7 December 2013

PHP to use .Net (C#) Assembly (Complete & Simple Tutorial)

After reading this section of PHP document  I thought interaction between PHP and .Net would be just a piece of cake, but actually it depends. It took me considerable time to get it work, eventually, on my Win7 64-bit and VS2010. So this tutorial came up with detailed steps and error handling experiences.
Basically there are two classes to help use a .Net dll in PHP:
  • DOTNET
  • COM
Actually they both use COM interoperability, I cannot see big difference except the latter can use .Net 4 assembly.
So here we go…

Prerequisite

You need PHP 5.5.6 installed on windows. There are versions for X86 and X64, whichever you like. And a tool to create C# library -  I have Visual Studio 2010 installed.

How to use DOTNET class

1. Create a new project as class library in VS2010.



Here I set the project name as RegPhpDemo

2. Make your assembly as COM component

Go to properties of your project. Make it COM visible.



Warning: Make sure your Target .Net framework is NOT .Net 4!!

This is very important. Use .Net 2 or 3.5!  
Currently PHP’s DOTNET class can only find the assembly in
%windir%\assemly\
However, from .Net 4 Microsoft changed GAC directory to
%windir%\Microsoft.NET\assembly\GAC_<PlatformTarget>\
Where PHP wouldn’t load from!

3. Write any public class, here I created a new class file

4. Install your DLL into GAC

Go to your project’s properties then choose signing tab.
You can create a new key file or choose an existing one, whatever you like.
Then build the project, locate the dll afterwards, and run command to install it into GAC:
> gacutil /f -i RegPhpDemo.dll
Here /f simply forces the dll to be reinstalled.

5. Create PHP script to Test

Please note detailed information of your DLL must be provided, including Name, Version, Culture and PublicKeyToken.
Hint: Getting the stupidly long string can be an easy job for gacutil.exe:
>gacutil -l RegPhpDemo
The output would have something like following.


So just copy the string into code.
Finally run above script. We get


How to use COM class



This is an alternative to DOTNET class. The benefit is .Net 4 is available again!

Build DLL

Just follow Step from 1 to 4 in DOTNET class example above, but set the target platform to .Net 4 (.Net 2 and .Net 3.5 are fine of course but her ewe use .Net 4 as the bonus).

Register DLL

Build your project and register the DLL as COM component:
>regasm RegPhpDemo.dll
Warning: X86 or X64 means different! On windows X64, you may have multiple versions of regasm.exe. When register your COM component, they can inject information into individual entries. For 32bit regasm, it writes into Wow6432Node section in registry while 64bit regasm won’t. PHP x86 will be searching Wow6432Node while x64 will act as usual.
To keep it simple, just follow the table below.
PHP
Regasm to be used
64bit
%windir%\Microsoft.NET\Framework\v4.0.30319\regasm
32bit
%windir%\Microsoft.NET\Framework64\v4.0.30319\RegAsm.exe

Create an new PHP script using COM class

Then run it to get output same as before!

Summary

DOTNET class

Pros

·         The DLL can be used by either PHP X86 or X64 version on Windows at same time.

Cons

·      Cannot use .Net 4 as your DLL’s target framework.


COM class

Pros

·         DLL can use .Net 4

Cons


·      PHP X86 and X64 version both needs registration.

No comments :

Post a Comment