473,654 Members | 3,068 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1559
Hi Charles Law,

You could use a Singleton for this:

Imports System
Imports System.IO
Imports System.Collecti ons
Imports System.Runtime. Serialization.F ormatters.Binar y
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(B yVal info As SerializationIn fo, _
ByVal context As StreamingContex t) _
Implements ISerializable.G etObjectData

' Instead of serializing this object, we will
' serialize a SingletonSerial izationHelp instead.
info.SetType(Ge tType(Singleton SerializationHe lper))
' 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 SingletonSerial izationHelper
Implements IObjectReferenc e
' This object has no fields (although it could).

' GetRealObject is called after this object is deserialized.
Public Function GetRealObject(B yVal context As StreamingContex t) As
Object Implements IObjectReferenc e.GetRealObject
' When deserialiing this object, return a reference to
' the Singleton object instead.
Return Singleton.GetSi ngleton()
End Function
End Class
Class App
<STAThread()> Shared Sub Main()
Dim fs As New FileStream("Dat aFile.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.GetS ingleton(),
Singleton.GetSi ngleton()}

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

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

' Deserialize the array elements.
fs.Position = 0
Dim a2() As Singleton = DirectCast(form atter.Deseriali ze(fs),
Singleton())

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

' This displays "True".
Console.WriteLi ne("Do all array elements refer to the same object?
" & _
Object.Referenc eEquals(a1(0), a2(0)))
Catch e As SerializationEx ception
Console.WriteLi ne("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******** ******@TK2MSFTN GP15.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.co m> wrote in message
news:O8******** ******@TK2MSFTN GP12.phx.gbl...
Hi Charles Law,

You could use a Singleton for this:

Imports System
Imports System.IO
Imports System.Collecti ons
Imports System.Runtime. Serialization.F ormatters.Binar y
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(B yVal info As SerializationIn fo, _
ByVal context As StreamingContex t) _
Implements ISerializable.G etObjectData

' Instead of serializing this object, we will
' serialize a SingletonSerial izationHelp instead.
info.SetType(Ge tType(Singleton SerializationHe lper))
' 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 SingletonSerial izationHelper
Implements IObjectReferenc e
' This object has no fields (although it could).

' GetRealObject is called after this object is deserialized.
Public Function GetRealObject(B yVal context As StreamingContex t) As
Object Implements IObjectReferenc e.GetRealObject
' When deserialiing this object, return a reference to
' the Singleton object instead.
Return Singleton.GetSi ngleton()
End Function
End Class
Class App
<STAThread()> Shared Sub Main()
Dim fs As New FileStream("Dat aFile.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.GetS ingleton(),
Singleton.GetSi ngleton()}

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

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

' Deserialize the array elements.
fs.Position = 0
Dim a2() As Singleton = DirectCast(form atter.Deseriali ze(fs),
Singleton())

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

' This displays "True".
Console.WriteLi ne("Do all array elements refer to the same object?
" & _
Object.Referenc eEquals(a1(0), a2(0)))
Catch e As SerializationEx ception
Console.WriteLi ne("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******** ******@TK2MSFTN GP15.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******** ********@TK2MSF TNGP12.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******** ******@TK2MSFTN GP09.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******** ******@TK2MSFTN GP12.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******** ******@TK2MSFTN GP15.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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
3730
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: class DB { var $db; var $host; var $un;
5
2976
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 be tested on different servers. Without using the keyword "Shared", how can I share these data to the other 5 classes? Thanks for any ideas! Cesar
21
4063
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 A { std::vector<B*> Bs; public:
3
1981
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 to list and another process will be a GUI that will view objects in the list. I want this all to happen in real-time (e.g once a processes adds an object to the list the GUI will see it.) What would be the best way to accomplish this. Some of...
7
4874
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 processes. but how to do it in .net? -- ISAMUSETU
1
1321
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
5476
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 array of data. All four classes are the same except for the number of elements in their arrays and the data. I have a MainClass (the form), which processes all of these arrays. The MainClass makes use of third party objects to do this. There...
4
3139
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
8606
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 plan is to delivere different report.dlls with the main app. But it is essentially importent, that the reports and the app use the same settings.
0
8285
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8814
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8706
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8591
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6160
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4149
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4293
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1915
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1592
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.