Convert a C# code to Php using the DOTNET class

1

I'm trying to convert a C# code to php using DOTNET, I never use C# but a lot of php, so, I'm starting with a basic things, to se if usefull do the conversion, then have the next C# piece of code.

using System.Text;
class AES
{
    private byte[] _key;
    private byte[] _iv;
    public AES()
    {
    }
    public AES(string keyBase, string iv)
    {
        _key = Encoding.ASCII.GetBytes(keyBase);
        _iv = Encoding.ASCII.GetBytes(iv);
    }
}

then trying to convert just this piece, so I do the next in php

<?php
class Ejemplo{
    public function FunctionName($value = '')
    {
        echo "Hello world!...";
        //var_dump(\System\Text\Encoding::GetEncoding());
        $stack = new DOTNET("mscorlib", "System.Text.Encoding");
        var_dump($stack);
   }
}
$eje = new Ejemplo();
$eje->FunctionName('nada');
?>

but in the line "$stack = new DOTNET("mscorlib", "System.Text.Encoding");"

Got this error "Fatal error: Uncaught exception 'com_exception' with message 'Failed to instantiate .Net object [CreateInstance] [0x80131513]",

then looking for the web, I see that need a full string with version and other things for dll refered to "System.Text.Encoding", my question is how to know the info for a dll, to can create a string like this

$csclass = new DOTNET("CSharpCOM, Version=1.0.0.0, Culture=neutral, PublicKeyToken=beb607ae770f5750", "CSharpCOM.CSharpCOMClass");

but for my

$stack = new DOTNET("mscorlib", "System.Text.Encoding");

Or if someone know another way to do this. Thanks everybody

c#
php
interop
asked on Stack Overflow Mar 15, 2016 by pipook • edited Mar 15, 2016 by NineBerry

2 Answers

1

For "mscorlib", you do not need to provide the full assembly name with the culture and version, since this is one of the core assemblies of .net containing the most basic functions and is a fixed part of the .net runtime.

System.Text.Encoding is an abstract class. That is why you cannot create an instance of it. In the example, you want to use System.Text.ASCIIEncoding. So, create an instance of that class directly:

$encoding = new DOTNET("mscorlib", "System.Text.ASCIIEncoding"); 
$bytes = $encoding->GetBytes("Hello World");

It should be noted that using .net assemblies from PHP with the help of "DOTNET" is limited, because it only allows you to instantiate classes. You cannot use static classes and enums, which means that some of the .net API is out of reach.

You can use the commercial NetPhp library instead.

Alternatively, write the code in C# with a spefic narrow interface to be used from php.

answered on Stack Overflow Mar 15, 2016 by NineBerry • edited Mar 15, 2016 by NineBerry
0

I believe all the info you need to define the .dll is available on the .dll itself. Have you bothered to find your mscorlib.dll and viewing it's file details?

You might be able to find it under the folder {Drive}/Windows/assembly. You may need to install an Msoft util to view the assemblies properly. Once you can view the assemblies via the Msoft tool you'll be able to grab the Public Key Token and version to use in

$stack = new DOTNET("mscorlib", "System.Text.Encoding");
answered on Stack Overflow Mar 15, 2016 by Erik • edited Mar 15, 2016 by Erik

User contributions licensed under CC BY-SA 3.0