title: Cannot create an object from a active x component
body: <p>I have a problem with an active x component I wrote in C#. Despite I've registered the dll with regasm I'm currently not able to create an active x object...</p>
  
  <p>Here is my code from the class:</p>
  
  <pre><code>using System;
  using System.Collections;
  using System.Globalization;
  using System.IO;
  using System.Reflection;
  using System.Runtime.InteropServices;
  using System.Diagnostics;
  using System.Security.Policy;
  using System.Security;
  using Ch.XXX.System6.Modules.Logger;
  
  namespace Gadget.Interop
  {
      /// &lt;summary&gt;
      /// GadgetAdapter is the starting point for loading and unloading .NET assemblies
      /// from javascript or any COM-based environment.
      /// &lt;/summary&gt;
      [ComVisible(true)]
      [ClassInterface(ClassInterfaceType.None)]
      [Guid("21A72577-3D2D-4664-93F7-1BE02AE68240")]
      [ProgId("GadgetInterop.GadgetAdapter")]
      [ComDefaultInterface(typeof(IGadgetInterop))]
      public class GadgetAdapter : IGadgetInterop, IDisposable, IObjectSafety
      {
          [DllImport("kernel32.dll")]
          static extern IntPtr GetModuleHandle(string module);
          [DllImport("kernel32.dll")]
          [return: MarshalAs(UnmanagedType.Bool)]
          static extern bool FreeLibrary(IntPtr handle);
          [DllImport("kernel32.dll")]
          public extern static bool GetModuleHandleExA(int dwFlags, string ModuleName, ref IntPtr phModule);
          [DllImport("kernel32.dll")]
          static extern IntPtr LoadLibrary(string lpFileName);
  
          /// &lt;summary&gt;
          /// Gadget.Interop.dll Name for FreeLibrary Function
          /// &lt;/summary&gt;
          private const string DLLNAME = "Gadget.Interop.dll";
  
          /// &lt;summary&gt;
          /// Parameters for the constructor
          /// &lt;/summary&gt;
          private ArrayList paramList = new ArrayList(); // Array list to  hold constructor parameters
  
          /// &lt;summary&gt;
          /// Zusätzliche AppDomain
          /// &lt;/summary&gt;
          private AppDomain appDomain;
  
  
          /// &lt;summary&gt;
          /// Standard Konstruktor
          /// &lt;/summary&gt;
          public GadgetAdapter() { }
  
          #region IGadgetInterop Members
          /// &lt;summary&gt;
          /// Adds an object to be passed to a class' constructor.  Values must be passed to 
          /// this method in the same order of the constructor's arguments.
          /// &lt;/summary&gt;
          /// &lt;param name="parameter"&gt;Constructor agrument value&lt;/param&gt;
          public void AddConstructorParam(object parameter)
          {                
              paramList.Add(parameter);
          }
  
          /// &lt;summary&gt;
          /// Creates an instance of the specified type.  Constructor parameters are used when
          /// calling this method if they exist, but are forcibly cleared after object creation.
          /// &lt;/summary&gt; 
          /// &lt;param name="assemblyFullPath"&gt;Full path to and name of the assembly to load&lt;/param&gt;
          /// &lt;param name="className"&gt;Full namespace and class name of the type to create&lt;/param&gt;
          /// &lt;returns&gt;Instance of an object represented by the className arg ument&lt;/returns&gt;
          public object LoadType(string assemblyFullPath, string className)
          {
              try
              {
                  ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType.Name);
  
                  log.Info("Finally we arrived our C# code");
                  return LoadTypeWithParams(assemblyFullPath, className, false);
              }
              catch (Exception ex)
              {
                  // Javascript-friendly exception
                  throw new Exception("Finally we arrived c# code" + ex.Message);
              }
          }
          /// &lt;summary&gt;
          /// Creates an instance of the specified type. 
          /// &lt;param name="preserveParams"&gt;Clears the constructor parameters if false&lt;/param&gt;
          /// &lt;returns&gt;Instance of an object represented by the className argument&lt;/returns&gt;
          public object LoadTypeWithParams(string assemblyFullPath, string className, bool preserveParams)
          {
              //Load Gadget.Interop.dll over the Gadget.Interop.dll
              LoadLibrary(DLLNAME);
  
              //Setup extra AppDomain
              AppDomainSetup setup = new AppDomainSetup();
              setup.ApplicationBase = AppDomain.CurrentDomain.BaseDirectory;
  
              setup = AppDomain.CurrentDomain.SetupInformation;
              setup.PrivateBinPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
              setup.PrivateBinPathProbe = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
              setup.ApplicationName = "GadgetAdapter-Loader";
              setup.ShadowCopyFiles = "true";
  
              appDomain = AppDomain.CreateDomain(
                 /*Generate a dynamic name*/
                "GadgetAdapter - Assembly" + Path.GetRandomFileName(), null, setup);
  
              try
              {
                  //Set security
                  object[] hostEvidence = { new Zone(SecurityZone.MyComputer) };
  
                  //Create an Evidence based on the Security Zone
                  //Evidence intEvidence = new Evidence(hostEvidence, null);
  
                  //Load an assembly in an own application domain
                  return (appDomain.CreateInstanceFromAndUnwrap(
                       assemblyFullPath,
                       className, true, BindingFlags.CreateInstance | BindingFlags.Instance | BindingFlags.Public,
                       null, paramList.ToArray(), CultureInfo.CurrentCulture, null));
              }
              catch (Exception ex)
              {
                  //Eintrag ins EventLog schreiben
                  string sSource;
                  string sLog;
                  string sEvent;
  
                  sSource = "XXX Sidebar Gadget";
                  sLog = "Sidebar Gadget";
                  sEvent = "Unhandled Exception in Gadget.Interop.GadgetAdapter";
  
                  //Eventlog Eintrag schreiben
                  if (!EventLog.SourceExists(sSource))
                      EventLog.CreateEventSource(sSource, sLog);
                  EventLog.WriteEntry(sSource, sEvent + Environment.NewLine + ex.Message + Environment.NewLine + ex.InnerException,
                      EventLogEntryType.Error);
  
                  throw new Exception(ex.InnerException.ToString());
              }
          }
  
          /// &lt;summary&gt;
          /// Call the object's Dispose method and sets the object to null;
          /// &lt;/summary&gt;
          /// &lt;param name="typeToUnload"&gt;Type implementing IDisposable to be destroyed&lt;/param&gt;
          public void UnloadType(object typeToUnload)
          {
              try
              {
                  //Verfiy Unloadtype
                  if (typeToUnload != null &amp;&amp; typeToUnload is IDisposable)
                  {
                      //Dispose
                      (typeToUnload as IDisposable).Dispose();
  
                      if (appDomain != null)
                      {
                          //Unload ApplicationDomain
                          AppDomain.Unload(appDomain);
                          appDomain = null;
                      }
                  }
  
                  //Set instance to null
                  typeToUnload = null;                
              } 
              catch (Exception exception)
              {
                  //Eintrag ins Eventlog schreiben
                  string sSource;
                  string sLog;
                  string sEvent;
  
                  sSource = "XXX Sidebar Gadget";
                  sLog = "Sidebar Gadget";
                  sEvent = "Unhandled Exception in Gadget.Interop.GadgetAdapter";
  
                  //Eventlog Eintrag schreiben
                  if (!EventLog.SourceExists(sSource))
                      EventLog.CreateEventSource(sSource, sLog);
                  EventLog.WriteEntry(sSource, sEvent + Environment.NewLine + exception.Message + Environment.NewLine + exception.InnerException,
                      EventLogEntryType.Error);
              } 
          }
  
          #endregion
  
          #region IDisposable Members 
  
          /// &lt;summary&gt;
          /// Dispose Methode for the GadgetAdapter
          /// &lt;/summary&gt;
          public void Dispose()
          {
              //Clear constructor parameters
              this.paramList.Clear();
              paramList = null;
  
              try
              {
                  //Unload the GadgetAdapter over Kernel32 functions
                  IntPtr hMod = IntPtr.Zero;
                  if (GetModuleHandleExA(0, DLLNAME, ref hMod))
                  {
                      bool s = false;
                      do
                      {
                          s = FreeLibrary(hMod);
                      } while (s);
                  }
              }
              finally
              {
                  GC.SuppressFinalize(this);
              }
  
          }
  
          #region IObjectSafety Members
          public enum ObjectSafetyOptions
          {
              INTERFACESAFE_FOR_UNTRUSTED_CALLER = 0x00000001,
              INTERFACESAFE_FOR_UNTRUSTED_DATA = 0x00000002,
              INTERFACE_USES_DISPEX = 0x00000004,
              INTERFACE_USES_SECURITY_MANAGER = 0x00000008
          };
  
          public int GetInterfaceSafetyOptions(ref Guid riid, out int pdwSupportedOptions, out int pdwEnabledOptions)
          {
              ObjectSafetyOptions m_options = ObjectSafetyOptions.INTERFACESAFE_FOR_UNTRUSTED_CALLER | ObjectSafetyOptions.INTERFACESAFE_FOR_UNTRUSTED_DATA;
              pdwSupportedOptions = (int)m_options;
              pdwEnabledOptions = (int)m_options;
              return 0;
          }
          public int SetInterfaceSafetyOptions(ref Guid riid, int dwOptionSetMask, int dwEnabledOptions)
          {
              return 0;
          }
          #endregion
  
          #endregion
      }
  }
  </code></pre>
  
  <p>And here is the interface:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Runtime.InteropServices;
  using System.Text;
  
  namespace Gadget.Interop
  {
      /// &lt;summary&gt;
      /// Interface required COM registration.  This interface outlines methods
      /// used to create and destroy managed .NET types
      /// &lt;/summary&gt;
      [ComVisible(true)]
      [InterfaceType(ComInterfaceType.InterfaceIsDual)]
      [Guid("618ACBAF-B4BC-4165-8689-A0B7D7115B05")]
      public interface IGadgetInterop
      {
          object LoadType(string assemblyFullPath, string className);
          object LoadTypeWithParams(string assemblyFullPath, string className, bool preserveParams);
          void AddConstructorParam(object parameter);
          void UnloadType(object typeToUnload);
      }
  }
  </code></pre>
  
  <p>When I try a simple CreateObject in a vb script</p>
  
  <pre><code>Set wshShell2 = CreateObject("GadgetInterop.GadgetAdapter")
  </code></pre>
  
  <p>I also tried:</p>
  
  <pre><code>Set wshShell2 = CreateObject("Gadget.Interop.GadgetAdapter")
  </code></pre>
  
  <p>I get the error: 800A01AD ActiveX component can not create an object</p>
  
  <p>Anybody else had the same problem?</p>
  
  <p>EDIT: I also see the component in the ole viewer...</p>
  
  <p>Thanks</p>