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:


//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>
    public interface IPhpDotNet
        string Info();

        object Eval(string sCSCode);
    /// <summary>
    /// COM Events interface
    /// </summary>
    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.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");
            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;

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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: