context: //
  //  This file is part of the OpenLink Software Virtuoso Open-Source (VOS)
  //  project.
  //
  //  Copyright (C) 1998-2014 OpenLink Software
  //
  //  This project is free software; you can redistribute it and/or modify it
  //  under the terms of the GNU General Public License as published by the
  //  Free Software Foundation; only version 2 of the License, dated June 1991.
  //
  //  This program is distributed in the hope that it will be useful, but
  //  WITHOUT ANY WARRANTY; without even the implied warranty of
  //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  //  General Public License for more details.
  //
  //  You should have received a copy of the GNU General Public License along
  //  with this program; if not, write to the Free Software Foundation, Inc.,
  //  51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  //
  //
  using System;
  using System.Runtime.Remoting;
  using System.Collections;
  using System.Reflection;
  using System.Security;
  using System.Text;
  using System.IO;
  using System.Runtime.InteropServices;
  using System.Runtime.Serialization;
  using System.Runtime.Serialization.Formatters.Binary;
  using System.Runtime.Serialization.Formatters.Soap;
  using System.Runtime.CompilerServices;
  using System.Security.Permissions;
  using System.Reflection.Emit;
  using System.Globalization;
  using System.Threading;
  using System.Management;
  using Microsoft.Win32;
  using System.Web;
  using System.Web.Hosting;
  using System.Xml.Serialization;
  
  using System.CodeDom;
  using System.CodeDom.Compiler;
  using Microsoft.CSharp;
  using System.Diagnostics;
  using System.Collections.Specialized;
  
  
  [assembly:AssemblyVersionAttribute("1.0.2.1")]
  [assembly:AssemblyCompanyAttribute("OpenLink Software")]
  [assembly:AssemblyKeyFileAttribute("virtkey.snk")]
  
  [ClassInterface(ClassInterfaceType.AutoDual)]
  public class VInvoke
  {
  
    private ResolveEventHandler hndlr;
    private static StringCollection assemblies;
  //private static Hashtable assem_perm_list;
    public VInvoke() {
      hndlr = new ResolveEventHandler (MyResolveEventHandler);
      assemblies = new StringCollection();
  //  assem_perm_list = new Hashtable ();
    }
  
    #region public interface
    public Object[] call_method_asm (Int32 asem_type, Int32 sec_unrestricted, String assem, String atype,
        String method, Object[] vparams, IntPtr [] oparams)
      {
        Object [] ret = new Object[2];
  #if !MONO
        try
  	{
  #endif
  	  Type type = get_type (asem_type, assem, atype);
  
  	  vparams = change_ptr (vparams, oparams);
  
  	  /* Get the methods from the type */
  	  MethodInfo[] methods = type.GetMethods();
  
  	  if(methods == null)
  	    {
  	      throw new Exception ("No methods Found in " + type.FullName);
  	    }
  
  	  StringBuilder failureExcuses = new StringBuilder();
  
  	  try
  	    {
  	      foreach(MethodInfo m in methods)
  		{
  		  if (m.Name == method && CheckArgs (m, vparams))
  		    {
  #if !MONO
  		      if (sec_unrestricted == 0)
  			{
  			  ZoneIdentityPermission zip = new ZoneIdentityPermission(SecurityZone.NoZone);
  			  zip.PermitOnly();
  			}
  #endif
  		      return res_to_ptr (m.Invoke (null, vparams));
  		    }
  		}
  	    }
  	  catch (TargetInvocationException e)
  	    {
  	      throw e.InnerException;
  	    }
  
  	  throw new Exception ("No method " + method + " called");
  #if !MONO
  	}
        catch (Exception e)
  	{
  	  ret[0] = 0;
  	  ret[1] = e.Message;
  	  return ret;
  	}
  #endif
      }
  
  
    public Object[] get_copy (IntPtr in_gc)
      {
        if (in_gc == (IntPtr) 0)
  	throw new Exception ("object handle not supposed to be 0");
        GCHandle real_instance;
        Object [] ret = new Object[2];
        real_instance = (GCHandle) (in_gc);
        Object new_instance = real_instance.Target;
  
        GCHandle to_save = GCHandle.Alloc (new_instance, GCHandleType.Normal);
  
        ret[0]=6;
        ret[1]=(int)(IntPtr) to_save;
        if (in_gc == (IntPtr) to_save)
  	throw new Exception ("copied object handle is the same as the original");
  
        return ret;
      }
  
  
  
    public Object[] obj_serialize (IntPtr in_gc)
      {
        GCHandle real_instance;
        Object new_instance;
  
        if (in_gc == (IntPtr) 0)
  	throw new Exception ("object handle not supposed to be 0");
        real_instance = (GCHandle) (in_gc);
        new_instance = real_instance.Target;
        return res_to_ptr (obj_serialize_int (new_instance, new BinaryFormatter()));
      }
  
  
    public Object[] obj_serialize_soap (IntPtr in_gc)
      {
        GCHandle real_instance;
        Object new_instance;
        if (in_gc == (IntPtr) 0)
  	throw new Exception ("object handle not supposed to be 0");
        real_instance = (GCHandle) (in_gc);
        new_instance = real_instance.Target;
        return res_to_ptr (obj_serialize_int (new_instance, new SoapFormatter()));
      }
  
  
  
    public Object[] obj_deserialize (byte [] in_bytes, Int32 asem_type, String assem, String atype)
      {
        IFormatter fmt;
        if (in_bytes != null && in_bytes.Length > 2 && in_bytes[0] == 0x3c && in_bytes[1] == 0x53)
  	fmt = new SoapFormatter();
        else
  	fmt = new BinaryFormatter();
        return obj_deserialize_int (in_bytes, asem_type, assem, atype, fmt);
      }
  
  
    public Object[] obj_deserialize_soap (byte [] in_bytes, Int32 asem_type, String assem, String atype)
      {
        return obj_deserialize_int (in_bytes, asem_type, assem, atype, new SoapFormatter());
      }
  
  
    public Object[] obj_deserialize_bin (byte [] in_bytes, Int32 asem_type, String assem, String atype)
      {
        return obj_deserialize_int (in_bytes, asem_type, assem, atype, new BinaryFormatter());
      }
  
  
    public static Object get_inst (IntPtr in_gc)
      {
        if (in_gc == (IntPtr) 0)
  	throw new Exception ("object handle not supposed to be 0");
        GCHandle real_instance;
        Object ret_instance;
  
        real_instance = (GCHandle) (in_gc);
  
        if (real_instance.IsAllocated)
  	{
  	  ret_instance = real_instance.Target;
  	}
        else
  	{
  	  throw new Exception ("Can't get instance of the Object");
  	}
  
        return ret_instance;
      }
  
  
    public Object[] call_ins (IntPtr instance, Int32 sec_unrestricted, String method,
        			    Object[] vparams, IntPtr [] oparams)
      {
        Object new_instance;
        Type instance_type;
        String assem_name;
        Object [] ret = new Object[2];
  
  #if !MONO
        try
  	{
  #endif
  	  new_instance = get_inst (instance);
  	  instance_type = new_instance.GetType();
  	  assem_name = instance_type.Assembly.FullName.ToString();
  	  int pos = assem_name.IndexOf (",");
  	  if (pos != -1)
  	    assem_name = assem_name.Substring (0, pos);
  
  	  vparams = change_ptr (vparams, oparams);
  	  MethodInfo[] methods = instance_type.GetMethods();
  	  /* TODO Change to temp.GetMethod (strin, Binding Flags, NULL, Type[], NULL) */
  	  try
  	    {
  	      foreach(MethodInfo m in methods)
  		{
  		  if (m.Name != method)
  		    {
  		      continue;
  		    }
  		  else
  		    {
  		      if (CheckArgs(m, vparams))
  			{
  #if !MONO
  			  if (sec_unrestricted == 0)
  			    {
  			      ZoneIdentityPermission zip = new ZoneIdentityPermission(SecurityZone.NoZone);
  			      zip.PermitOnly();
  			    }
  #endif
  			  /* ->  Ready  <- */
  			  return res_to_ptr (m.Invoke(new_instance, vparams));
  			}
  		      else
  			continue;
  		    }
  		}
  	    }
  	  catch (TargetInvocationException e)
  	    {
  	      throw e.InnerException;
  	    }
  
  	  throw new Exception ("Method " + method + " not found in call_ins");
  #if !MONO
  	}
        catch (Exception e)
  	{
  	  ret[0] = 0;
  	  ret[1] = e.Message;
  	  return ret;
  	}
  #endif
      }
  
  
  
    public Object[] get_prop (IntPtr instance, String prop_name)
      {
        Object [] ret = new Object[2];
        if (instance == (IntPtr) 0)
  	throw new Exception ("object handle not supposed to be 0");
  
        GCHandle real_instance;
        Object new_instance;
        Type instance_type;
        real_instance = (GCHandle) (instance);
        new_instance = real_instance.Target;
        instance_type = new_instance.GetType();
  
        FieldInfo[] field_infos = instance_type.GetFields(BindingFlags.NonPublic |
  	  BindingFlags.Instance | BindingFlags.Public);
        foreach(FieldInfo fi in field_infos)
  	{
  	  if (fi.Name == prop_name && !fi.IsStatic)
  	    {
  	      try
  		{
  		  Object ret_m = null;
  
  		  ret_m = fi.GetValue(new_instance);
  
  		  return res_to_ptr (ret_m);
  		}
  	      catch
  		{
  		  continue;
  		}
  	    }
  	}
  
        PropertyInfo[] props = instance_type.GetProperties(BindingFlags.NonPublic |
  	  BindingFlags.Instance | BindingFlags.Public);
        foreach(PropertyInfo p in props)
  	{
  	  if (p.Name == prop_name)
  	    {
  	      try
  		{
  		  Object ret_m = null;
  
  		  ret_m = p.GetValue(new_instance, null);
  
  		  return res_to_ptr (ret_m);
  		}
  	      catch
  		{
  		  continue;
  		}
  	    }
  	}
  
        throw new Exception ("Property " + prop_name + " not found");
      }
  
  
    public Object[] set_prop (IntPtr instance, String prop_name, Object[] vparams, IntPtr [] oparams)
      {
        Object [] ret = new Object[2];
        if (instance == (IntPtr) 0)
  	throw new Exception ("object handle not supposed to be 0");
  
        GCHandle real_instance = (GCHandle) (instance);;
        Object new_instance = real_instance.Target;
        Type instance_type = new_instance.GetType();
        int is_call = 0;
  
        vparams = change_ptr (vparams, oparams);
  
        FieldInfo[] field_infos = instance_type.GetFields(BindingFlags.NonPublic |
  	  BindingFlags.Instance | BindingFlags.Public);
  
        foreach(FieldInfo fi in field_infos)
  	{
  	  if (fi.Name == prop_name && !fi.IsStatic)
  	    {
  	      try
  		{
  		  fi.SetValue(new_instance, vparams[0]);
  		  is_call = 1;
  		}
  	      catch
  		{
  		  continue;
  		}
  	    }
  	}
  
        if (is_call != 1)
  	{
  
gt: 	  PropertyInfo[] prop_infos = instance_type.GetProperties();