简体   繁体   中英

PHP DOTNET hell

I'm quite a newbie in PHP and today I discovered DOTNET class.
So I studied manual, surfed the web to find some example and finally wrote my test app:

  1. Created a new DLL using Framework 4.0 Client Profile
  2. Signed the assembly with a strong name key
  3. Marked assembly as COM-Visible

This is the test code I wrote

using System;

namespace CSharpCOM
{
    public class CSharpCOMClass
    {
        public string Base64(string s)
        {
            return Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(s));
        }
    }
}

I compiled the assembly and then registered in GAC ( gacutil /if fullpath\\CSharpCOM.dll ).
If I use gacutil /l CSharpCOM I see

La cache di assembly globale contiene gli assembly seguenti:
csharpcom, Version=1.0.0.0, Culture=neutral, PublicKeyToken=beb607ae770f5750, processorArchitecture=MSIL

Numero di elementi = 1

So everything seems ok.
Then wrote this basic php:

<?php
try{
    $csclass = new DOTNET("CSharpCOM, Version=1.0.0.0, Culture=neutral, " .
                          "PublicKeyToken=beb607ae770f5750",
                          "CSharpCOM.CSharpCOMClass");
    echo $csclass->Base64("Test string"),"\n";
} catch (Exception $e) {
    echo 'Caught exception: ',  $e->getMessage(), "\n";
}
?>

Whatever I try, loading page hosted in Apache ( http://localhost/test01/dotnet.php ) I always get

Caught exception: Failed to instantiate .Net object [CreateInstance] [0x80070002] Impossibile trovare il file specificato.
Translation could be: unable to find specified file

Just another thing: using some example (a very basic one here ) I read that my assembly (when registered) should be found on %windir%\\assembly , but I'm only able to find it in %windi%\\Microsoft.NET\\assembly\\GAC_MSIL\\CSharpCOM\\v4.0_1.0.0.0__beb607ae770f5750 : is this correct? Why don't I have it on first directory?

More: if I create another framework project and try to add a .NET reference I can't find my assembly: is this related to the fact I'm not able to load this assembly from PHP?

Last note: I tried it on Windows XP Professional SP3 32bit and on Windows Seven Enterprise 64bit

UPDATE:
This works:

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

but this one does not:

$form = new DOTNET('System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089', 'System.Windows.Forms.Form');`  

Is it possible that PHP can load only framework 2.0 assemblies?

According to this bug report the DOTNET class is not loading .NET 4.0 DLLs. If you're not using any of the new libraries in .NET 4.0 you can target your to .NET 3.5 or lower by opening the project properties and on the "Application" tab change the "Target framework" to ".NET Framework 3.5 Client Profile"

Now when you install your DLL into the GAC it will get installed into the CLR 2.0 GAC and should be able to be loaded using the DOTNET class in PHP.

There is a library out there called NetPhp ( https://github.com/david-garcia-garcia/netphp ) that will let you:

  • Use any .Net binaries (even without COM Visibility) and ANY version of the .Net framework including CLR4
  • Dump a PHP class model
  • Iterate over .Net collections directly from PHP PHP class model generation, to use PHP classes as if it where .Net Automatic propagation of .Net errors into native PHP exceptions that can be properly handled
  • Acces native enums and static methods
  • Use class constructors with parameters
  • Debug .Net and PHP code at the same time as if it was a single application.

There is a project with code samples here:

https://github.com/david-garcia-garcia/netphp-sample

This is what a piece of code with NetPhp looks like:

$datetime = $runtime->TypeFromName("System.DateTime");
$datetime->Instantiate();

echo $datetime->ToShortDateString()->Val(); // Outputs 01/01/0001

// We can only use Int32 from native PHP, so parse
// an Int64 that is equivalent to (long) in the DateTime constructor.
$ticks = $runtime->TypeFromName("System.Int64")->Parse('98566569856565656');
$ticks = \ms\System\netInt64::_Parse('98566569856565656');

$datetime->Instantiate($ticks);
echo $datetime->ToShortDateString()->Val(); // Outputs 07/05/0313

$now = $runtime->TypeFromName("System.DateTime")->Now;
echo $now->ToShortDateString()->Val(); // Outputs "23/10/2015"

$now = $runtime->TypeFromName("System.DateTime")->Now();
echo $now->ToShortDateString()->Val(); // Outputs "23/10/2015"

$timer = $runtime->TypeFromName("System.Timers.Timer")->Instantiate();
$timer->AutoReset(TRUE);
$timer->AutoReset = TRUE;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM