title: PHP DOTNET hell
body: <p>I'm quite a newbie in PHP and today I discovered <a href="http://php.net/manual/en/class.dotnet.php" rel="nofollow">DOTNET</a> class.<br>
  So I studied manual, surfed the web to find some example and finally wrote my test app:</p>
  
  <ol>
  <li>Created a new DLL using Framework 4.0 Client Profile</li>
  <li>Signed the assembly with a strong name key</li>
  <li>Marked assembly as COM-Visible</li>
  </ol>
  
  <p>This is the test code I wrote</p>
  
  <pre><code>using System;
  
  namespace CSharpCOM
  {
      public class CSharpCOMClass
      {
          public string Base64(string s)
          {
              return Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(s));
          }
      }
  }
  </code></pre>
  
  <p>I compiled the assembly and then registered in GAC (<code>gacutil /if fullpath\CSharpCOM.dll</code>).<br>
  If I use <code>gacutil /l CSharpCOM</code> I see</p>
  
  <blockquote>
    <p>La cache di assembly globale contiene gli assembly seguenti:<br>
    csharpcom, Version=1.0.0.0, Culture=neutral,
    PublicKeyToken=beb607ae770f5750, processorArchitecture=MSIL</p>
    
    <p>Numero di elementi = 1</p>
  </blockquote>
  
  <p>So everything seems ok.<br>
  Then wrote this basic php:</p>
  
  <pre><code>&lt;?php
  try{
      $csclass = new DOTNET("CSharpCOM, Version=1.0.0.0, Culture=neutral, " .
                            "PublicKeyToken=beb607ae770f5750",
                            "CSharpCOM.CSharpCOMClass");
      echo $csclass-&gt;Base64("Test string"),"\n";
  } catch (Exception $e) {
      echo 'Caught exception: ',  $e-&gt;getMessage(), "\n";
  }
  ?&gt;
  </code></pre>
  
  <p>Whatever I try, loading page hosted in Apache (<code>http://localhost/test01/dotnet.php</code>) I always get </p>
  
  <blockquote>
    <p>Caught exception: Failed to instantiate .Net object [CreateInstance]
    [0x80070002] Impossibile trovare il file specificato.<br>
    Translation could be: unable to find specified file</p>
  </blockquote>
  
  <p>Just another thing: using some example (a very basic one <a href="http://geekswithblogs.net/tkokke/archive/2009/04/24/how-to-use-.net-assemblies-in-php.aspx" rel="nofollow">here</a>) I read that my assembly (when registered) should be found on <code>%windir%\assembly</code>, but I'm only able to find it in <code>%windi%\Microsoft.NET\assembly\GAC_MSIL\CSharpCOM\v4.0_1.0.0.0__beb607ae770f5750</code>: is this correct? Why don't I have it on first directory?</p>
  
  <p>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?</p>
  
  <p>Last note: I tried it on Windows XP Professional SP3 32bit and on Windows Seven Enterprise 64bit</p>
  
  <p><strong>UPDATE:</strong><br>
  This works: </p>
  
  <pre><code>$form = new DOTNET('System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089', 'System.Windows.Forms.Form');
  </code></pre>
  
  <p>but this one does not:  </p>
  
  <pre><code>$form = new DOTNET('System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089', 'System.Windows.Forms.Form');`  
  </code></pre>
  
  <p>Is it possible that PHP can load only framework 2.0 assemblies?</p>