Saar Carmi

A .NET Blog

<November 2008>
SuMoTuWeThFrSa
2627282930311
2345678
9101112131415
16171819202122
23242526272829
30123456


Navigation

Subscriptions

Post Categories



Monday, February 27, 2006 - Posts

Calling from .Net 1.1 to .Net 2.0

Hi

A process cannot load both .net frameworks 1.1 & 2.0.

Therefore, to call 2.0 object from a 1.1 object, you must have a cross process call. One of the ways (but not the only) to do so, is to host the 2.0 object within COM+ process and expose it as a COM object. To do so, your object must derive Serviced Component and be marked as ComVisible.

Here is a simple example.

Save this as SimpleClient.cs

using System;
using System.Reflection;

public class SimpleClient
{
 public static void Main()
 {
  Type t = Type.GetTypeFromProgID ("MyObject");
  System.Diagnostics.Debug.Assert (t != null);
 
  object o = Activator.CreateInstance(t);
  Console.WriteLine (Environment.Version.ToString());
  t.InvokeMember ("Increment", BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Instance, null, o, new object[0]);
  
 }
}

Save this as MyObject.cs

using System;
using System.Reflection;
using System.EnterpriseServices;
using System.Runtime.InteropServices;

[assembly: ApplicationActivationAttribute(ActivationOption.Server)]
[assembly: ApplicationName ("MyObject")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: ApplicationAccessControl(false)]

[System.Runtime.InteropServices.ComVisible (true)]
[Guid("65D66FCF-997E-4737-896D-60EEC0AF0E88")]
public class MyObject : ServicedComponent
{
 public void Increment ()
 {
  
  System.Windows.Forms.MessageBox.Show("COM CALL - " + Environment.Version.ToString());
 }
}

Create an snk file using the command "sn -k snk.snk"

Complie & register both source files with the following command lines:

"c:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\csc" SimpleClient.cs
"c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\csc" /t:library MyObject.cs /keyfile:snk.snk
"c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\regsvcs" MyObject.dll

 

 

posted Monday, February 27, 2006 2:49 PM by saarc with 0 Comments




Powered by Dot Net Junkies, by Telligent Systems