How to Share Class Properties Across Processes

C

Charles Law

I have just been asked how to share functions and properties between two
running applications. For example, I have App1 and App2 both running on the
same machine. App1 uses a DLL (perhaps) that contains function SetProp().
When App1 calls it, a property in the DLL is set to "abc". App2 calls a
function GetProp(), in the same DLL. GetProp() should return "abc".

This sounds like a simple thing to do, but making the DLL variable shared
and making both applications load the same DLL does not work. On reflection,
I can see why that wouldn't (shouldn't) work, but how can it be done;
preferably without having to jump through security hoops.

TIA

Charles
 
P

Pipo

Hi Charles Law,

You could use a Singleton for this:

Imports System
Imports System.IO
Imports System.Collections
Imports System.Runtime.Serialization.Formatters.Binary
Imports System.Runtime.Serialization


' There should be only one instance of this type per AppDomain.
<Serializable()> Public NotInheritable Class Singleton
Implements ISerializable

' This is the one instance of this type.
Private Shared ReadOnly theOneObject As New Singleton

' Here are the instance fields.
Public someString As String
Public someNumber As Int32

' Private constructor allowing this type to construct the Singleton.
Private Sub New()
' Do whatever is necessary to initialize the Singleton.
someString = "This is a string field"
someNumber = 123
End Sub

' A method returning a reference to the Singleton.
Public Shared Function GetSingleton() As Singleton
Return theOneObject
End Function

' A method called when serializing a Singleton.
Private Sub GetObjectData(ByVal info As SerializationInfo, _
ByVal context As StreamingContext) _
Implements ISerializable.GetObjectData

' Instead of serializing this object, we will
' serialize a SingletonSerializationHelp instead.
info.SetType(GetType(SingletonSerializationHelper))
' No other values need to be added.
End Sub

' Note: ISerializable's special constructor is not necessary
' because it is never called.
End Class


<Serializable()> Friend NotInheritable Class SingletonSerializationHelper
Implements IObjectReference
' This object has no fields (although it could).

' GetRealObject is called after this object is deserialized.
Public Function GetRealObject(ByVal context As StreamingContext) As
Object Implements IObjectReference.GetRealObject
' When deserialiing this object, return a reference to
' the Singleton object instead.
Return Singleton.GetSingleton()
End Function
End Class


Class App
<STAThread()> Shared Sub Main()
Dim fs As New FileStream("DataFile.dat", FileMode.Create)

Try
' Construct a BinaryFormatter and use it
' to serialize the data to the stream.
Dim formatter As New BinaryFormatter

' Create an array with multiple elements refering to
' the one Singleton object.
Dim a1() As Singleton = {Singleton.GetSingleton(),
Singleton.GetSingleton()}

' This displays "True".
Console.WriteLine("Do both array elements refer to the same object?
" & _
Object.ReferenceEquals(a1(0), a1(1)))

' Serialize the array elements.
formatter.Serialize(fs, a1)

' Deserialize the array elements.
fs.Position = 0
Dim a2() As Singleton = DirectCast(formatter.Deserialize(fs),
Singleton())

' This displays "True".
Console.WriteLine("Do both array elements refer to the same object?
" & _
Object.ReferenceEquals(a2(0), a2(1)))

' This displays "True".
Console.WriteLine("Do all array elements refer to the same object?
" & _
Object.ReferenceEquals(a1(0), a2(0)))
Catch e As SerializationException
Console.WriteLine("Failed to serialize. Reason: " & e.Message)
Throw
Finally
fs.Close()
End Try
End Sub
End Class
 
C

Charles Law

Hi Pipo

Thanks for the quick response. Your example appears to operate within a
single app domain, whereas I need this to operate across app domains. I have
tried you example in my scenario, but when I set someString in the first
application, it does not appear set in my second application.

Charles
 
C

Charles Law

Hi Cor

Hmm ... I nearly mentioned remoting in my original post, but I didn't want
to steer people down a particular route. I realise that this is one option,
but perhaps it is a bit overkill? The principle of what I am trying to do
seems quite straight forward, but maybe remoting is the sledge hammer to my
nut? Just a thought. I really only need this to operate across app domains
on a single machine, so security is not my main problem. Also, might
remoting be a bit slow for repeated property access?

Charles
 
C

Cor Ligthert

Charles,

You want a quick and dirty one. (registry)

I did not write it :)

Cor
 
C

Charles Law

Hi Cor
You want a quick and dirty one. (registry)

No, not really. In any case, the registry idea might not work if the user
does not have permission to modify the registry.

Charles
 
C

Cor Ligthert

Charles,

Not that it is the solution it is really quick and dirty,

However why this: "does not have permission to modify the registry". For me
he has that forever as long that it is his part. How many changes did you
think that there have been this morning on your registry?

Cor
 
C

Charles Law

Cor

Ok, perhaps that is true, but I would prefer to use a 'built-in' .NET way
rather than using the registry.

Charles
 
C

Cor Ligthert

Charles,
Am I to assume that this is not possible?
For me it seems that you forgot that the time of static program addressing
is far behind us. (I know better).

Now you need everytime to know where a program is to know its addresses and
than the values in that.

In addition, you lost in the sentence above "simple"

:)

Cor
 
C

Charles Law

Hi Cor

You are right, I mean simple, or perhaps straightforward. I'm off remoting
at the moment because it seems that tcp/ip needs to be installed, and it may
not be in my scenario.

I'm still hoping that someone will come up with a technique that works like
it used to do in the good old days.

Charles
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top