By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,573 Members | 1,223 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,573 IT Pros & Developers. It's quick & easy.

How to Share Class Properties Across Processes

P: n/a
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
Nov 21 '05 #1
Share this Question
Share on Google+
11 Replies


P: n/a
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

"Charles Law" <bl***@nowhere.com> wrote in message
news:ec**************@TK2MSFTNGP15.phx.gbl...
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

Nov 21 '05 #2

P: n/a
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
"Pipo" <Pi**@nobody.com> wrote in message
news:O8**************@TK2MSFTNGP12.phx.gbl...
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

"Charles Law" <bl***@nowhere.com> wrote in message
news:ec**************@TK2MSFTNGP15.phx.gbl...
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


Nov 21 '05 #3

P: n/a
Charles,

Are you inventing your own style of remoting?

Cor

Nov 21 '05 #4

P: n/a
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
"Cor Ligthert" <no************@planet.nl> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Charles,

Are you inventing your own style of remoting?

Cor

Nov 21 '05 #5

P: n/a
Charles,

You want a quick and dirty one. (registry)

I did not write it :-)

Cor
Nov 21 '05 #6

P: n/a
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
"Cor Ligthert" <no************@planet.nl> wrote in message
news:OB**************@TK2MSFTNGP09.phx.gbl... Charles,

You want a quick and dirty one. (registry)

I did not write it :-)

Cor

Nov 21 '05 #7

P: n/a
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

Nov 21 '05 #8

P: n/a
Cor

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

Charles
"Cor Ligthert" <no************@planet.nl> wrote in message
news:OI**************@TK2MSFTNGP12.phx.gbl...
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

Nov 21 '05 #9

P: n/a
Am I to assume that this is not possible?

Charles
"Charles Law" <bl***@nowhere.com> wrote in message
news:ec**************@TK2MSFTNGP15.phx.gbl...
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

Nov 21 '05 #10

P: n/a
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
Nov 21 '05 #11

P: n/a
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
"Cor Ligthert" <no************@planet.nl> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
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

Nov 21 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.