9

I'm using a C#.NET DLL with ASP.NET 2.0 and it's working now. I want to use the same DLL in PHP.

I'm a newbie in PHP; would someone please tell me how to use it in PHP or could you share some example?

hakre
  • 193,403
  • 52
  • 435
  • 836
muhammadanish
  • 437
  • 2
  • 6
  • 20
  • I don't know if you can use the same .dll. Anyway to add one you have to add this line to your php.ini extension=yourdll.dll – Aurelio De Rosa Oct 24 '11 at 10:59
  • Please consider this post: http://stackoverflow.com/questions/310821/how-to-call-asp-net-dll-file-from-a-php-script – Abdul Munim Oct 24 '11 at 11:02

3 Answers3

9

PHP has a built-in Windows-only extension called DOTNET that allows you to use .NET libraries in a PHP application.

Note that you'll need to make sure your assemblies are declared as COM visible:

[assembly: ComVisible(true)]

Here are two examples.

<?php
 $stack = new DOTNET("mscorlib", "System.Collections.Stack");
 $stack->Push(".Net");
 $stack->Push("Hello ");
 echo $stack->Pop() . $stack->Pop();
?>

Another example demonstrating functionality of DOTNET class:

<?php

$full_assembly_string = 'System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=a8425bc35256e463';
$full_class_name = 'System.Windows.Forms.Form';
$form = new DOTNET($full_assembly_string, $full_class_name);

// code to add buttons, menus, text, etc

$form->Show();

$form_event = '';
while($form_event !== 'close') {

  // handle form functions and events

  }
?> 
Bitterblue
  • 13,162
  • 17
  • 86
  • 124
foxy
  • 7,599
  • 2
  • 30
  • 34
  • It works well with .NET libraries. I couldn't ever load one of my own classes. Looks like ComVisible is not the only switch you have to pull to make that work. – Bitterblue Jul 26 '18 at 12:05
2

you are using the PHP Version 5.4.7 you should already have com_dotnet.dll if you dont have it you can download it on "download" and add to your ext/ path inside php directory.

Edit you php.ini file

extension=php_com_dotnet.dll
Guilherme Ferreira
  • 2,209
  • 21
  • 23
vaibhav
  • 21
  • 1
1

What you really need is this:

  • Enable COM-Visibility (I was wrong before)
  • Sign your assembly (give it a "stong name", use "sn.exe" to create public-private-keys)
  • Add your assembly to GAC (use "gacutil.exe")
  • When you change your assembly, before adding it to GAC again, make sure your AssemblyVersion (not FileVersion) changes/goes up (you can go down as well)

(you find those tools most probably in "C:\Program Files (x86)\Microsoft SDKs\Windows\ ...", just google for their correct use, easy enough)


That should do it.
In PHP (example):
<?php
    // use this kind of name, not path to dll or whatever
    // print your assemblly's full name in .NET and use that
    $name = "YourAssembly, Version=1.1.1.1, Culture=neutral, PublicKeyToken=fe6263478ac";
    $obj = new DOTNET($name, "YourNamespace.YourClass");
    echo "success\n";
?>

Oh yeah, vaibhav is right about "php.ini". I didn't have to edit it. It had the correct values (defaults maybe?).

Bitterblue
  • 13,162
  • 17
  • 86
  • 124