Geeks With Blogs

@Sorskoot
  • Sorskoot "Visual Studio 2013, will be available later this year, with a preview build publicly available at Build" http://t.co/SV28646Qr6 about 455 days ago

News


Timmy Kokke's Blog

↑ Grab this Headline Animator

Timmy Kokke at Blogged
Timmy Kokke …just sorting my bubbles…

 

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

Posted on Friday, April 24, 2009 2:32 PM WinPHP | Back to top


Comments on this post: How to use .Net assemblies in PHP

# re: How to use .Net assemblies in PHP
Requesting Gravatar...
Cool! I didn't know that could be done. Thanks for sharing!
Left by Josh on Apr 24, 2009 3:25 PM

# re: How to use .Net assemblies in PHP
Requesting Gravatar...
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!
Left by Rafael on Aug 27, 2009 10:22 PM

# re: How to use .Net assemblies in PHP
Requesting Gravatar...
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.
Left by Timmy Kokke on Sep 02, 2009 12:07 PM

# re: How to use .Net assemblies in PHP
Requesting Gravatar...
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
Left by Amit A More on Sep 17, 2009 8:25 AM

# re: How to use .Net assemblies in PHP
Requesting Gravatar...
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?
Left by Andrew on Jan 16, 2010 1:06 PM

# re: How to use .Net assemblies in PHP
Requesting Gravatar...
Thanks for this very good tutorial.
I did the same with using VB.NET in VS2008.
But PHP shows fatal error as "Class DOTNET not found...."

Can you help me on this ??

Thanks in advance
Left by Vikrant on Aug 15, 2011 7:37 AM

# re: How to use .Net assemblies in PHP
Requesting Gravatar...
Getting the Fatal error as "Class DOTNET not found..". Any help pls!
Left by Vinothkumar on Oct 01, 2012 9:03 AM

# re: How to use .Net assemblies in PHP
Requesting Gravatar...
Your getting the error: " Class DOTNET not found" because you don't have that extension in your php.ini; Go into your php.ini and load this extension:

extension=php_com_dotnet.dll

http://www.php.net/manual/en/com.installation.php
Left by Steffen on Nov 26, 2012 11:15 AM

# re: How to use .Net assemblies in PHP
Requesting Gravatar...
how to use .net plse telme information send me
Left by gsuresh on Dec 04, 2012 9:17 AM

# re: How to use .Net assemblies in PHP
Requesting Gravatar...
first... sorry for my english, i'm from brazil.
it's work!!! thanks.
But just work using .net framework 3.5, When I change the .net framework version of project to 4.0 in visual studio and try use the dll on php, it doesnt work. Because when i execute "gacutil -i dllname.dll" with the dll using framework 4.0... the assembly doesnt appers in C:\Windows\assembly, i dont now why =/.
Left by Fernando on Mar 04, 2013 8:01 PM

# re: How to use .Net assemblies in PHP
Requesting Gravatar...
Fernando, você faz toda a diferença neste post.
Left by André Cavalcante on Feb 01, 2014 11:10 PM

Your comment:
 (will show your gravatar)
 


Copyright © Timmy Kokke | Powered by: GeeksWithBlogs.net | Join free