Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
3.25/5 (4 votes)
See more:
3 applications running at the same time.
Is there an easy way of accessing the same variables from any of them?
First thought is to use the Windows Registry or File.

Any thoughts?
Posted
Comments
Rob Branaghan 18-May-11 7:54am    
Why do 3 applications need the same variables?
Are they all part of one business function?
Simon_Whale 18-May-11 9:09am    
Can we assume that they are also accessing the same business logic? If so have you thought about using a common middle layer class library or even a web service depending on the scope of your project?
Stuart Nathan 18-May-11 13:04pm    
eg.
App1 loads AutoCad
App2 is loaded as internal process to AutoCad
App3 is a windows service keeping time.

I don't understand middle layer class?
JasonSelf 19-May-11 9:48am    
Could store the variables as var's in AutoCAD and retrieve them from there? Obviously this wouldn't work across multiple drawings.

Bad idea!

If you really want to do it, the is one IPC primitive designed specially for this case: Shared Memory. But this primitive is not available in .NET — I think, for a good reason.

One solution based on C++/CLI is shown here:
http://stackoverflow.com/questions/439787/how-to-implement-shared-memory-in-net[^].

I would suggest you use VB.NET and P/Invoke required Windows API instead: CreateFileMapping, MapViewOfFile, CopyMemory and CloseHandle. See:
http://msdn.microsoft.com/en-us/library/aa366551(v=vs.85).aspx[^].

Don't forget to interlock access to shared memory using the named Mutex (only named mutex can be used for IPC). See http://msdn.microsoft.com/en-us/library/system.threading.mutex.aspx[^].

One problem of both shared memory and mutex is that they will need system-unique names you will need to devise and guarantee uniqueness.

—SA
 
Share this answer
 
Comments
Stuart Nathan 19-May-11 6:14am    
Had a quick look, and although this seems the correct answer, it does look too complicated for me to get my head around. I also didn't get how you release the memory when the last app closes.
Sergey Alexandrovich Kryukov 20-May-11 15:07pm    
Complicated or not for you -- this is your problem, as well as further detail. I'm just curious: is that the reason of vote of "3" (it it was your vote)?
--SA
Sergey Alexandrovich Kryukov 20-May-11 15:08pm    
Bad architectural idea (as I say) cannot evoke good technical solutions anyway...
--SA
Stuart Nathan 20-May-11 18:39pm    
I don't understand why you are so rude. I am sitting here on my own, trying to learn how to programme properly, and one way is to ask questions. Normally people who answer questions tend to understand the level where people are - after all, if we were all as clever as you, there would be no need to ask our questions. All you do is to put me off returning to the site.

As for 3 or 5, frankly I don't care as I don't actually understand how it helps. Experts Exchange, which I used to use before it became so commercial, at least allowed you to use your points to ask further questions.
Sergey Alexandrovich Kryukov 21-May-11 0:06am    
Rude?! Enough for me...
--SA
you could use the SaveSetting / GetSetting method to write the values and retrieve the values from the registry.
SaveSetting("appname", "section", "key", "value")
GetSetting("appname", "section", "key")
Probably not the most elegant solution but it works and is easy.
 
Share this answer
 
v2
Comments
Stuart Nathan 18-May-11 13:02pm    
Yes I know. I had already mentioned that in my Q.
Any other ideas?
Sergey Alexandrovich Kryukov 18-May-11 15:35pm    
Well, real thing is shared memory -- specially designed for such things.
Please see my answer.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900