views:

60

answers:

4

Hi,

I need a way to store a value somewhere for temporarily by say Process A. Process A can exit the after storing the value in memory. After sometime Process B comes accesses the same location of memory and read the value. I need to store in memory, because I dont want the data to persistent across reboots. But as long as the system is up, it Independent of the Process the data must be accessible. I tried MailSlots and Temporary files in windows, both seem to have problem where the process reference count drops to zero , the entities dont persist in memory. What is a suitable mechanism for this in Windows preferably using Win32 API?

  • Ganesh
+4  A: 

Write a service that is started at boot time, and let it create some shared memory. This shared memory can then be filled by process A, and process B can read it afterwards.

If your system is rebooted, the shared memory is gone and you have a fresh, new piece of shared memory. Make sure that your service correctly 'initializes' the shared memory.

Patrick
I can do that... Does windows itself provide some shared service/Facility/API that lets me store the value , instead of me writing my shared service???
Ganesh
Not that I'm aware of.
Patrick
Windows provides two related services to achieve something similar - you can use the registry or the file system to store things after the process has gone away.
Stewart
But they are persistent... I just need volatile memory to store data
Ganesh
+1  A: 

Is there a reason why the data must be resident in memory when ProcessA quits as opposed to being stored somewhere on disk? I ask as you mention temporary files which should work unless ProcessA fails in an unexpected way.

Depending on your needs a nice way to provide shared/fast/atomic data is via the ESENT API.

Alex K.
A: 

Try the following. I can't say I know this works, but it seems reasonable.

Create a shared memory file in the global namespace using OpenFileMapping. Then call Duplicatehandle and for the target process handle use some process that will live longer than process A. You may be able to add the handle to winlogon.exe This should stop the shared memory from being destroyed when process A terminates. Then in process B you can look up the shared memory file.

Stephen Nutt
Duplicating handles into WinLogon is a risky idea. Just because there is a process called winlogon that runs all the time today, I don't think that is actually documented. If it goes away, your app will be busted. Also, injecting stuff into processes you don't control, especially processes which are part of windows, seems dangerous in general.
Stewart
Code injection is part of the Windows API - check out SetWindowsHookEx. Using Process Explorer I see that both KACE Networks and Check Point Software have injected themselves into winlogon.exe on my machine. Adding a handle to another process is legal. My main point was to add the handle to a process that lives longer than process A
Stephen Nutt
Memory-Mapped Files is a good solution for this. Here is a MSDN reference on creating Memory-Mapped Files and how to read then from a difference process. It has code samples.http://msdn.microsoft.com/en-us/library/ms810613.aspx
dlb
A: 

Well, I managed to a create a MailSlot on a Process which doesnt exit, the other two Processes can read and write to the MailSlot server as clients... Even if the clients exit, the Mailslot will still have the data... the MailSlot server enables me to store data in volatile memory has long as the MailSlot server process is up.. or the OS is up.. and vanishes on OS reboot... Thanks for all the ideas and help.... :)

Ganesh