Home Contact

Timmy Kokke

…just sorting my bubbles…

News




Timmy Kokke's Blog

↑ Grab this Headline Animator

Timmy Kokke at Blogged

Twitter












Tag Cloud


Archives

Post Categories

Image Galleries

Silverlight

Syndication:

How to use .Net assemblies in PHP

 

Introduction

For my entry in the WinPHP Challenge I need to use some .Net assemblies I wrote a while ago. It wasn’t clear to me how this can be done. Here’s an example on how to do this. In short: First we create an assembly in visual studio, than we sign it, add it to the Global Assembly Cache or GAC and access it using PHP from there.

 

Details

Inside visual studio, create a new project. For the purpose of explanation I named the project DotNetTest.

 NewProject

Add the following method to the newly created Class1 class. Make sure the class is declared public.

public class Class1
{
    public string SayHello()
    {
        return "Hello from .NET";
    }
}

Now, go ahead an see if this compiles by clicking Build->Build Solution in the menu, or by hitting Ctrl+Alt+B on your keyboard. It may come as no surprise that it  does… ;)

 

To be able to use it thru the GAC the assembly has to be signed. Signing can by done in visual studio. Go to the project properties by right-clicking the project and click Properties all the way at the bottom. Go to the Signing tab. Check the Sign the assembly checkbox and select <New…> from the dropdown list beneath that. Give the key file a nice name, like DotNetTestKey and uncheck Protect my key file with a password, because security isn’t an issue in this demo. Click Ok to close the window and finish the signing.

CreateStrongNameKeyPHP uses COM, even for .NET, we have to make sure the assembly is Com Visible. Go to the Application tab in the Properties windows and click the Assembly Information button. Check the box next to Make assembly COM-Visible and click Ok.

MakeComVisibleBuild the solution again to make sure the assembly we’re about to add to the GAC is signed and configured correctly.

 

Next, we have to register the assembly in the GAC. The easiest way to do this is thru the command-line. Visual studio provides a short cut to the command prompt by right-clicking on the project in the solution explorer and click Open Command Prompt. Go to the debug folder by typing cd bin/debug. Enter the following command to install the assembly into the GAC:

gacutil -i DotNetTest.dll

 

 

 

The utility should tell you that the assembly as successfully been added to the cache.

 

Last, take your favorite PHP editor and create a new PHP file with the code below.

<?php
$class1 = new DOTNET("DotNetTest,"
                    ."Version=1.0.0.0,"
                    ."Culture=neutral,"
                    ."PublicKeyToken=????????????????"
                    ,"DotNetTest.Class1");
echo($class1->SayHello());
?>

The DOTNET class instantiates a class from a .Net assembly. The full assembly name must be provided to the constructor at the place of the ‘???’ in the example. This can be found by going to C:\Windows\Assembly using the Windows Explorer or by opening the .dll file in a tool like Reflector.

Place the file at a location accessible thru the browser and go there. If everything went well, it should say “Hello from .Net


Shout it


Feedback

# re: How to use .Net assemblies in PHP

Cool! I didn't know that could be done. Thanks for sharing! 4/24/2009 3:25 PM | Josh

# re: How to use .Net assemblies in PHP

Hello!
I'm battling to interface an assembly that's already compiled and registerd with a PHP script. For testing, I've installed a ZEND server (PHP 5.3.0) but the DOTNET class doesn't seem to work, even with the example given in php.net:

<?php
$console = new DOTNET("mscorlib", "System.Console");
$varible = $console->ReadLine();
$console->WriteLine( "Varible: " . $varible );
$console->WriteLine();

unset( $varible );
unset( $console );
exit();
?>

Do you have any clue?
Thank you very much! 8/27/2009 10:22 PM | Rafael

# re: How to use .Net assemblies in PHP

I'm sorry, I do not have any exprience with Zend. But when working with the default .NET classes I did get errors very often. The DOTNET class doesn't seem to be fully/correctly implemented yet. I sure hope this will improve in the future. 9/2/2009 12:07 PM | Timmy Kokke

# re: How to use .Net assemblies in PHP

I was searching on google from past 6-7 hours for "How to use .Net DLLs(as per you => 'assemblies') in PHP" I got some articles BUT those were mentioned about only Windows Environment!

How do I use .Net DLLs in PHP under Linux Environment.
I am using Centos, PHP 5.2

Need Help 9/17/2009 8:25 AM | Amit A More

# re: How to use .Net assemblies in PHP

Hey i have real problem.
What if i dont want to use the bin/debug directory to it? It didn't work when i wanted to recompile again AND copy the new dll to another map.

How could it be solved? 1/16/2010 1:06 PM | Andrew

# re: How to use .Net assemblies in PHP

A lot of people are familiar with PHP's functions, and though Javascript functions are often quite similar, some functions may be missing or addressed differently. The JavaScript implementations should be as compliant with the PHP versions as possible, a good indication is that the PHP function manual could also apply to the Javascript version. 5/21/2010 11:37 AM | jeu de poker en ligne

Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: