Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Shared Memory possible?

Status
Not open for further replies.

jfradley

Programmer
Jul 7, 2003
14
0
0
US
I've been looking all over the net to see if there is a way to have shared memory. So, to different exe's have access to the same data. I haven't been able to find anything.

What I want to use it for is I have a Windows service (with no GUI) that basically builds a in-memory DB. I want to write another program the can access the data without message passing.

Any ideas?
 
you can do it easily, if the programs that access this database are all run through another program. they can run as seperate applications, but this will allow you to pass a pointer to you database to them

The database in memory needs to be an instance of a database object, so start by writing a class for this. you need a class to contain this instance, and every instance of you database handler (by which i mean a program that wantsaccess to the database); which will prbably be the application class.

your database handler class wants aconstructor than recieves an object of your database type, so that you can run your handler from your application with something like:

Application.Run(new programThatDidlesWithTheData(myDataBase));

-----------

however, if you want to run whatever programs will be accessing the database directly from the OS, then you can't pass pointers to instances. if you're in that situation then i suggest you find a decent memory manager that allows you to read and write to ram as a stream. then you can write your program much as you would if sharing a file on disk.
 
With the little I know about .NET I would think sharing memory could be accomplished using the CreateFileMapping etc. API's through the DllImport attribute. Perhaps you can find some article(s) on MSDN by searching for DllImport CreateFileMapping?

-pete
 
In Windows, shared memory generally refers to the system page file, and there are a number of Win32 APIs that you use to program with this.

The practice of programming with shared memory is generally relegated to IPC tasks, and you need to consider synchronization and locking (IPC mutexes are also provided by Win32 APIs).

I have done IPC code in an unmanaged DLL with VC++ (which can easily utilize Win32 APIs). I used the DllImport attributes in C# to access these features. I guess you could also DllImport the system DLLs that provide the API calls, and use them directly in C#.

Look up these Win32 APIs:

CreateFileMapping
MapViewOfFile
CreateMutex
WaitForSingleObject

This ought to get you started with IPC programming.
 
Thanks both. I think I found an alternate solution. In my service I'm going to register my object with RegisterWellKnownServiceType(). The object I well inherit from MarshalByRefObject. In the service I will also create an instance of this object. Then the client will get the same instance of the object by calling Activator.GetObject().

It seems to work in prilimary testing.
 
I had noticed your note regarding the use of an instance of a class held by a service - and then accessing this single instance via remoting. I have constructed some code of this type, and it seems to lead to a possible solution. Have you found this method to work well for your needs? The resources I wish to manage are the comm ports. So I want the NTService to have threads that handle the ports (and various additional threads required for comm events etc.). So I then have the issue of the best way the NTService communicates with the single instance being hosted by the service. Do you have any suggestions or insights here?
 
If all your threads are spawned from the same process, you could have your single instance class be a static member that parent process. Then all the other threads can access and manipulate the static data also.
 
From what i read about .NET Remoting i don't see the connection to "Shared Memory". Did i miss something?

-pete
 
Remoting lets you access a registered objects methods, which intern lets you reference the objects data. So, it's like a shared object between 2 processes.

I mentioned "shared memory" initially because thats the only way I knew how (from working w/ C in QNX). It looks like by using remoting I can achieve the same functionality as having shared memory.

 
>> So, it's like a shared object between 2 processes.
>> I can achieve the same functionality as having shared memory.

The concept of the term "Shared Memory" would not generally be considered equivalent to marshalling data between processes, would it? So you were just interested in IPC not limited to shared memory?


-pete
 
No, I didn't really care if the data was physically mapped in the same place for both processes. I just wanted it to be the same for both processes.

I'm new to windows development, shared memory was in my mind the only solution to do this.

Initially, I didn't want to use IPC because of my experience with it in QNX. Remoting is easier then QNX IPC. Using your suggestion of file mapping would probably be the fastest running solution but I didn't want to maintain unmanaged code if I didn't have. If speed becomes an issue (which it might) I'll probably have to try it.
 
Very well written post jfradley. You should consider writing a FAQ about remoting after your project. There are none as yet in this forum and it seems it should prove a well used solution in .NET.

-pete
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top