views:

225

answers:

4

I have have an application which consists of an windows form project and a class library project. When it starts the executable sets a static value in the dll.

using MyClassLibrary;
namespace MyExeApplication
{
   public partial class MainForm : Form
   {
        Hashtable ht = null;
        void Form_Load(...)
        {
          ht = new Hashtable();
          ht.add("1", "Open");
          ht.add("2", "Close");
          Calculate.BasicValues = ht;
        }
        public Hashtable GetBasicValues()
        {
          return ht;
        }
   }

}     
namespace MyClassLibrary
{
   public class Calculate()
   {
       public static Hashtable BasicValues {get; set;}
   }
}

Now suppose the application is running in memory (the executable). My purpose is to create another standalone application and use the value BasicValues in the function Calculate in the class library.

using MyClassLibrary;
namespace TestApplication
{
  public partial class MainForm : Form
  {
     private void TestValueFromDll()
     {
          System.Windows.Forms.MessageBox.Show("Values of Hashtable");
          Hashtable ht = Calculate.BasicValues;
          //The hashtable is null and values are not there
          //The above will not work. Could I say something like
          //Get the running instance of MyExeApplication 
          //and invoke GetBasicValues() ?                
     }
  }
}

I guess it does not work because my TestApplication has copied the MyClassLibrary.dll to the bin folder where the executable TestApplication.exe is located. Thus a different dll was used (not the dll that was used by the first application MyExeApplication).

And my question is how can I solve this problem? Is it possible to use reflection and Get the instance of MyExeApplication and read the values from there? Is there any other way?

Thank you

A: 

If you build TestApplication, MyExeApplication, and MyClassLibrary to the same folder (setting the output folder in the projects' Properties menu), I'd imagine you can avoid having to use reflection. Also, I'm curious as to why you'd need to do something like this? Is BasicValues something that could be replaced by a SQL Table or even registry settings?

Good luck!

Pwninstein
+1  A: 

It is not a matter of where the DLL is located. It sounds like the real problem is that the DLL is loaded into the "MyExeApplication" appdomain and you need to get access from the "TestApplication" appdomain. And as it turns out, .NET Remoting was intended for communication across appdomains. There is a bunch of info about it over at MSDN.

McAravey
A: 

There's no way to do this, that I am aware of, short of attaching to the other process as a debugger.

Your best option is to have the first application store the Hashtable in some shared space. Either as a file on disk, in a database, with some form of shared memory, or pass it around by using remoting.

Which option you want to use will really depend on how the data is going to be used by each application.

Adam Ruth
A: 

Thank you all for the responses. I am currently using the registry as temporary storage to store values. I will read about remoting and application domains and will try to do that way.

The reason I want it, is that I have an exe application running in the background (minimized to windows tray). This exe app acts as an central API and generates some web login information which must be available to be accessed from any TestApplication that uses MyClassLibrary.dll.

Would it be good if I put all the code from my dll class to exe and then have only one executable which has everything?

Then in MyTestApplication I could add a reference to exe. But I would still have the same problem. Because if I want to create another application that integration with exe API, e.g. MyTestApplication2 and add a reference to exe would I still have the same problem?

I guess this is not possible from reflection. However in VB6 you could say something like object o = GetObject(,"MyExeApplication.Class") and then I could say o.GetBasicValues()).

I've been wanting something like the COM out of process server like you're talking about with VB 6. Unfortuntely, .NET doesn't have an equivalent mechanism. There is remoting, but it's not nearly as simple and easy to use.
Adam Ruth