Passing data across appdomains with MarshalByRefObject

Nathan W picture Nathan W · Nov 20, 2009 · Viewed 9.7k times · Source

I'm having a little trouble passing some data between two .NET appdomains and I'm hoping someone on here can help me.

Basically what I have is a main application (Main) which loads assembly A and B into it's main domain, then when I run a plugin(C) Main calls a create domain method on B which creates a new domain and loads C and a instance of B into it, so that C can only access B and not the others.

B contains a pointer to the IDispatch of Main but only it seems to get it after it is loaded into the new domain with C. What I am trying to do is send a copy of the pointer from the new domain instance of B and send it to A which is still running in the default domain.

Just for the record I control A,B and C but not Main

Sorry if this is a bit hard to understand I tried my best to explain it.

Code:

In A:

public class Tunnel : MarshalByRefObject
{
    public void SetPointer(int dispID)
    {
        IntPtr pointer = new IntPtr(dispID);
    }
}

In B:

//Call by Main after loading plug in but after A.dll is loaded.
public void CreateDomain()
{
  AppDomain maindomain= AppDomain.CurrentDomain;
  tunnel = (Tunnel)maindomain.CreateInstanceAndUnwrap(typeof(Tunnel).FullName,
                                                      typeof(Tunnel).FullName);

   AppDomain domain = base.CreateDomain(friendlyName, securityInfo, appDomainInfo);
   //Load assembly C (plug in) in domain.
   // C uses B so it loads a new instance of B into the domain also at the same time.

  // If I do this here it creates new instance of A but I need to use the one in
  // the main domain.
  //tunnel = (Tunnel)domain.CreateInstanceAndUnwrap(typeof(Tunnel).FullName,
                                                    typeof(Tunnel).FullName);
  tunnel.SetPointer(//Send data from B loaded in new domain.)

}

So at the end it looks something like this:

Default Domain:

  • Main.dll
  • A.dll
  • B.dll

Plug in Domain:

  • B.dll
  • C.dll

Answer

EMP picture EMP · Nov 20, 2009

In your code above you are calling

AppDomain.CurrentDomain.CreateInstanceAndUnwrap(...)

This is simply a round-about way of creating an object in the current domain, same as if you just called the constructor. You need to call that method on a remote domain, ie.

AppDomain domain = AppDomain.Create(...)
Tunnel tunnel = (Tunnel)domain.CreateInstanceAndUnwrap(...)

If you then call tunnel.SetPointer(...) that will run on the remote object.