473,396 Members | 1,792 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to Share Class Properties Across Processes

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
11 1534
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
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
Charles,

Are you inventing your own style of remoting?

Cor

Nov 21 '05 #4
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
Charles,

You want a quick and dirty one. (registry)

I did not write it :-)

Cor
Nov 21 '05 #6
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Anthony Davis | last post by:
Hello all, Can someone please tell me if there is a way to declare variables within a class that can be shared among each new object created from this class? For instance I have the following: ...
5
by: cksj | last post by:
I'm working on a VB.Net DLL project. It has 6 classes. One of the classes has the properties for the database path, database name and server name. The purpose of this class is so that the DLL can...
21
by: Jon Slaughter | last post by:
I have a class that is basicaly duplicated throughout several files with only members names changing according to the class name yet with virtually the exact same coding going on. e.g. class...
3
by: kyle.tk | last post by:
So I have a central list of python objects that I want to be able to share between different process that are possibly on different computers on the network. Some of the processes will add objects...
7
by: isamusetu | last post by:
anybody knows how to share the dll between the process? I know there is a way to set the #pragma data_seg in the visual studio 6.0 C++, that can make the dll can be shared between the multiple...
1
by: mg | last post by:
I took the following steps to share a user control across applications but was unsuccessful. WebUserControl: <%@ Control Language="c#" AutoEventWireup="false" ClassName="WebUserControl1" %>...
3
by: Brett | last post by:
I have several classes that create arrays of data and have certain properties. Call them A thru D classes, which means there are four. I can call certain methods in each class and get back an...
4
by: Mike | last post by:
Class A public objX I want to create 2 or more instances of Class A and have the same value for objX in all instances. Instance1 of Class A Instance2 of Class A Instance3 of Class A
5
by: Rainer Queck | last post by:
Hello NG, Is it possible to share the settings of an application with a class libreary? In my case I have a application and a set of different reports (home made) put into a class library. The...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.