views:

203

answers:

4

I have two different C# applications that are running at the same time.

I would like both of them to be able to access the same "instance" of a DLL (also in C#).

The DLL holds some data that I'd like to return to whichever of the two applications is asking for it.

My DLL is thread-safe so I was hoping this would be possible but I'm not sure how.

Any help or advice would be much appreciated.

A: 

A DLL has no instance, it is loaded in a host process. Reference the assembly in both applications and use its classes/methods.

If you want to avoid deploying the same assembly for both applications you could put it in the GAC.

Darin Dimitrov
+5  A: 

The process space will be different so, for example, global variables in the DLL will be specific to each separate process. It is possible that the code in memory will be shared (Windows typically uses reference counting to make that part more efficient).

If you are wanting to share information that is accessed in the DLL between the two processes, then it seems likely that it will be necessary to use some kind of IPC (interprocess communication) mechanism such as sockets, shared memory, pipes, etc.

Mark Wilkins
So you mean that both loaded DLLs will talk to each other through, say, named pipes?
Jelly Amma
@Jelly Amma: They could talk to each other through pipes if you implement it. It is not automatic if that is what you are asking.
Mark Wilkins
A: 

It's possible. You could install the DLL in the GAC (requires strong named assemblies) in order for both applications to have easy access to it.

Or stick it in a folder and have both apps search that folder for the dll.

MSDN support article

<configuration>
   <runtime>
      <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
         <dependentAssembly>
            <assemblyIdentity name="MyAssembly2"  culture="neutral" publicKeyToken="307041694a995978"/>
            <codeBase version="1.0.1524.23149" href="FILE://C:/Myassemblies/MyAssembly2.dll"/>
         </dependentAssembly>
      </assemblyBinding>
   </runtime>
</configuration>
Mikael Svenson
A: 

I don't know if this can be done in C# but in C++ you can also use Shared Memory sections if the info to share is not too complicated. You would simply need to synchronize access to this ressource using for example a mutex

A good article on the subject : http://www.codeproject.com/KB/threads/SharedMemory_IPC_Threads.aspx

Have fun

MLefrancois