2012年9月6日 星期四

How to use .Net assemblies in PHP

Quoted from Link

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.
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 theSign 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. ClickOk to close the window and finish the signing.
PHP uses COM, even for .NET, we have to make sure the assembly is Com Visible. Go to the Application tab in theProperties windows and click the Assembly Information button. Check the box next to Make assembly COM-Visible and click Ok.
Build 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

沒有留言:

張貼留言