Home > Uncategorized > .NET PHP Interop – Evaluate C# from PHP

.NET PHP Interop – Evaluate C# from PHP

Download: http://sites.google.com/site/appanalyzecomponent/phptodotnet

This code allows you to evaluate C# code in PHP.

Download the code above, import the DLL into the GAC, and create a PHP file as below:

<?php

//create an instance of the  ADO connection object
 $phpdotnet = new COM ("PhpDotNet.PhpDotNet")
  or die("Cannot start PhpDotNet.PhpDotNet");

 echo $phpdotnet->Info();
 echo ‘<br> ‘;
 echo ‘Windows folder is located at: ‘;
 echo $phpdotnet->Eval(‘Environment.GetEnvironmentVariable("SystemRoot")’);

?>

— See that Environment.GetEnvironmentVariable("SystemRoot") is C# code, not PHP.

using System.CodeDom.Compiler;
using System.Runtime.InteropServices;
using System.Text;

namespace PhpDotNet
{
    /// <summary>
    /// COM Interface
    /// </summary>
    [Guid("D5FA3365-D97F-49a9-A970-CDD5E0A3609F")]
    public interface IPhpDotNet
    {
        [DispId(1)]
        string Info();

        [DispId(2)]
        object Eval(string sCSCode);
    }
   
    /// <summary>
    /// COM Events interface
    /// </summary>
    [Guid("13F93C05-C751-4691-8888-BD72F13DCF04"),
    InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
    public interface IPhpDotNetEvents
    {
    }

    /// <summary>
    /// Implemintation
    /// </summary>
    public class PhpDotNet : IPhpDotNet
    {

        public string Info()
        {
            return "Php.NET is now working";
        }

        /// <summary>
        /// Evaluate custom c# Code.
        /// http://www.codeproject.com/KB/cs/evalcscode.aspx
        /// </summary>
        /// <param name="sCSCode"></param>
        /// <returns></returns>
        public object Eval(string sCSCode)
        {
            var provider = CodeDomProvider.CreateProvider("CSharp");
            var cp = new CompilerParameters();
            cp.ReferencedAssemblies.Add("system.dll");
            cp.ReferencedAssemblies.Add("system.xml.dll");
            cp.ReferencedAssemblies.Add("system.data.dll");
            cp.ReferencedAssemblies.Add("system.windows.forms.dll");
            cp.ReferencedAssemblies.Add("system.drawing.dll");
            cp.CompilerOptions = "/t:library";
            cp.GenerateInMemory = true;
            var sb = new StringBuilder("");
            sb.Append("using System;n");
            sb.Append("using System.Xml;n");
            sb.Append("using System.Data;n");
            sb.Append("using System.Data.SqlClient;n");
            sb.Append("using System.Windows.Forms;n");
            sb.Append("using System.Drawing;n");
            sb.Append("namespace CSCodeEvaler{ n");
            sb.Append("public class CSCodeEvaler{ n");
            sb.Append("public object EvalCode(){n");
            sb.Append("return " + sCSCode + "; n");
            sb.Append("} n");
            sb.Append("} n");
            sb.Append("}n");
            var cr = provider.CompileAssemblyFromSource(cp, sb.ToString());
            if (cr.Errors.Count > 0)
            {
                return cr.Errors[0].ErrorText;
            }
            var a = cr.CompiledAssembly;
            var o = a.CreateInstance("CSCodeEvaler.CSCodeEvaler");
            var t = o.GetType();
            var mi = t.GetMethod("EvalCode");
            var s = mi.Invoke(o, null);
            return s;
        }
    }
}

Advertisement
Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: