-2

We can read / write data in memory like this :

static void Main(string[] args)
{
    using (var ms = new MemoryStream())
    {
        var sw = new StreamWriter(ms);
        sw.WriteLine("Hello World");
        sw.Flush();
        ms.Position = 0;
        var sr = new StreamReader(ms);
        var myStr = sr.ReadToEnd();
        Console.WriteLine(myStr);
    }

    Console.WriteLine("Press any key to continue.");
    Console.ReadKey();
}

Now i want to write data on Ram (Memory = Random Access Memory) and read those data from Another Application?
How can i do that and Share a string in memory?

Important Note : I don't want to write any data on hard disk.
Edit : My .net version is 3.5

SilverLight
  • 19,668
  • 65
  • 192
  • 300
  • 1
    Hmm, no, the process boundary is a very tall wall. Look at the System.IO.MemoryMappedFiles namespace to get somewhere. – Hans Passant Aug 31 '15 at 22:23
  • https://msdn.microsoft.com/en-us/library/system.io.memorymappedfiles.memorymappedfile%28v=vs.110%29.aspx I am using .net 3.5 – SilverLight Aug 31 '15 at 22:56
  • 2
    Well, stop using it. Nobody has to be stuck on an 8 year old version of free software. – Hans Passant Aug 31 '15 at 23:00
  • Depending on your requirements, you could write a service that acts as a delegate between the two processes. Without some sort of middle-man, it doesn't seem like you are going to be able to do what you want. Try giving more clarification in your question as to what you are actually trying to accomplish, and maybe someone can be more helpful. – dub stylee Aug 31 '15 at 23:03
  • 1
    For some reasons i can't install .net 4 on my server. what is the problem? – SilverLight Aug 31 '15 at 23:08
  • 1
    @MoonLight generally "read memory of other application" means something totally different compared to "share" (first usually mean `ReadPRocessMemory`, later imply active cooperation - Inter-Process-Communication). You could have improved your question by searching for either https://www.bing.com/search?q=c%23%20read%20memory%20different%20process OR https://www.bing.com/search?q=c%23%20ipc and adding information why top results did not work for your case. – Alexei Levenkov Sep 01 '15 at 00:21

1 Answers1

0

Update for .net 3.5
Memory mapped files have been in windows for a while, so even .net 3.5 don't have them, it is still possible to wrap native api. Github project filemap does that. Unfortunately, they don't have example how to share memory mapped file between two processes.


This answer is useful for .NET Framework 4.6 and 4.5
Not 3.5
I don't think you can share a memory stream between two processes. Most close would be memory mapped files. Check this answer for other options.
Community
  • 1
  • 1
Igor Yalovoy
  • 1,715
  • 1
  • 17
  • 22