Using PHP with C# written libraries

Posted April 22nd, 2009 by Juozas

C#One of the biggest issue for me in WinPHP challenge (more info) is using C# written libraries in PHP. Actual PHP part is very easy to write, because only thing you need to do is to create COM object. However, making your dll’s visible by PHP is a bit tricky.

I followed this tutorial, which has most of required information. Firstly, you create your “Class library”, then create assembly key file and assign it to project assembly. After project is built, you register and add to global assembly cache your library. Probably it’s quite natural process for all .NET developers, but for PHP developer like me, registering something with whole system seemed quite odd (dll hell?).

visualstudio-1First problem was ComVisible. ComVisible is a keyword in assembly which is used to control types visibility: “If you need to access a type in this assembly from COM, set the ComVisible attribute to true on that type”. VisualStudio by default sets it to false, so all types are hidden and trying to register dll will result in this warning:

RegAsm : warning RA0000 : No types were registered

visualstudio-4Luckily, I find out about ComVisible property quite soon – it’s quite hard to know about it if you haven’t done anything serious with .NET. Nevertheless, after setting it to true, everything should work fine. Sometimes VisualStudio (2008 Professional Edition) refused to build project because of locked dll, so don’t forget to try to kill PHP-CGI process first (Ctrl+Alt+Del). I didn’t had any other problems.

visualstudio-3After some tests and failures I ended up with two simple C# classes and PHP caller. Class1 has two public variables (x, y) which are set by PHP in $math object. Method Class1.sum() creates new Math object with constructor parameters x and y, and calls getSum() which returns sum of x and y. Useless stuff, but it worked great to test if:

  • COM objects works
  • One class can call another (Class1 -> Math)
  • Types are correctly understood/converted

Working PHP code (phpclass2 is C# library namespace):

$math = new COM ("phpclass2.Class1");
$math->x = 100;
$math->y = 100;
 
echo "sum = " . $math->sum();
 
unset($math);

Which successfully outputted:

sum = 200

iis-1I tried this code with the same Web Platform I used in Windows Server 2008, but now with older IIS (v5, Windows XP) and it worked perfectly with all default settings. Also, IIS control panel in Server 2008 is 10 times better than in Windws Xp – in Xp-one I couldn’t even find how to add new virtual host (my bad, but in IIS 7 it’s way easier to do). And because I was installing it second time, it took me only few minutes.

Getting this to work was probably one of the biggest milestones – actual frontend code and Deepzoom C# library is not that tricky. After some days of working with PHP+Windows things are starting to get a shape – except of different OS GUI, web server works good in both Linux and Windows – clearly Microsoft is holding this competition to show it. And they are in the right way.

Trackbacks/Pingbacks

  1. schwobeseggl.de » Aus PHP C#-Klassen nutzen
  2. Juozas Kaziukenas’ Blog: Using PHP with C# written libraries : WebNetiques, LLC : Website Developers in Minneapolis, MN
  3. Juozas Kaziukenas’ Blog: Using PHP with C# written libraries : Dragonfly Networks
  4. Using PHP with C# written libraries | Juozas devBlog
  5. Dynamic Assemblies loading using Reflection | Juozas devBlog
  6. Creating Deep Zoom applications with C# | Juozas devBlog
  7. How to use outside libraries in PHP? | Juozas devBlog
  8. Juozas Kaziukenas’ Blog: How to use external libraries in PHP? | Webs Developer

Comments (1)

  1. Teal'c

    Wow, that is revolting. Making your library COM visible requires that you register the DLL in the GAC and registering something in the GAC should only be done when multiple applications need to access the same version of the same library. Even then, most software houses will deploy each app with it’s own copy of the DLL instead of using the GAC.

    If you had been using Mono and ASP.NET none of this would be needed at all.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">