Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Memory mapped file questions

by faustf (Novice)
on Oct 09, 2020 at 17:18 UTC ( #11122637=perlquestion: print w/replies, xml ) Need Help??

faustf has asked for the wisdom of the Perl Monks concerning the following question:

Hi PerlMonks , i have a software in C# this software create a memory mapped file
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO.MemoryMappedFiles; using System.ComponentModel; using System.Windows.Input; using System.IO; namespace MemoryMappedCS_1 { class Program { static void Main(string[] args) { var file = MemoryMappedFile.CreateOrOpen("Test", 1000, Mem +oryMappedFileAccess.ReadWrite); try { byte[] de = Encoding.Unicode.GetBytes("Demade"); MemoryMappedViewAccessor accessor = file.CreateViewAcc +essor(); accessor.Write(0, (char)de.Length); accessor.WriteArray(0, de, 0, de.Length); } catch { } Console.WriteLine("Run memory mapped file reader before ex +it"); Console.WriteLine("Press any key to exit ..."); Console.ReadLine(); } } }
i want try to read this string by perl, i use strawberry Perl 5, version 32, subversion 0 (v5.32.0) built for MSWin32-x64-multi-thread, i don't find ,a example for do it anyone can help me ? thanks at all

Replies are listed 'Best First'.
Re: Memory mapped file questions
by Corion (Pope) on Oct 09, 2020 at 17:46 UTC

    The parts where C# writes the data to the file are:

    accessor.Write(0, (char)de.Length); accessor.WriteArray(0, de, 0, de.Length);

    That suggests that there will be a single byte written containing the length of the string, and then the string. Maybe consider confirming this using a hex viewer, like hexdump or Perl code like the following:

    perl -nE 'BEGIN{ binmode $ARGV; $/=\16;}; say map {sprintf "%02x ", or +d $_}split //;' /path/to/file

    You will want to look at open, binmode (or use that in open directly), then read to get the data, and then unpack (see its documentation in pack. Most likely, the c/a template will be what you want to read in a length-prefixed string. To make it readable, see Encode::decode to decode it from UTF-8 to printable.

    Update: Added description of where the "interesting" part of writing the data happens in the C# code

      Thanks for answer, i don't have a real file in HDD, C# create a data-file directly in memory ,i never used hexdump , your example include a path to file , what should I call ? thanks

        How much of your C# code do you understand?

        Reading your code, there is the line:

        MemoryMappedFile.CreateOrOpen("Test", 1000, MemoryMappedFileAccess.Rea +dWrite);

        This suggests that the code opens or creates a file named Test in the current directory, and as jcb suggests, you could use that as the filename.

        If this is your code, maybe you want to use a different approach than a memory mapped file. If this is not your code, consider talking to the original author about using a different serialization scheme, like JSON, or a well-defined binary layout, or just not using a memory mapped file when a plain file will do just as well.

        Your C# code appears to be creating a file named "Test" in its current directory. You will want perl to open that file.

        Note that synchronization between memory-mapped files and the rest of the filesystem API is usually unspecified until msync() or its equivalent is called or the file is unmapped, so your data may not be actually written to the file until your C# program exits or performs some kind of synchronization call.

Re: Memory mapped file questions
by perlfan (Vicar) on Oct 11, 2020 at 03:54 UTC
    Fundamentally you would leverage Perl variables. There is a module that looks like it might be what you want, File::Map. I had to look up what a memory mapped file was and I realized it's pretty much what we do when we slurp a file into a datastructure. I know that doesn't really mean we use file accessor primitives, but things in Perl are done on a higher level via data structure manipulation. (update) somewhat related, Filesys::POSIX - Provide POSIX-like filesystem semantics in pure Perl
      Exactly i just saw the File::Map , but not exist example for read , and documentation is very gaunt, anyone have some example ? thankz a lot

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11122637]
Approved by marto
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (1)
As of 2021-10-22 01:24 GMT
Find Nodes?
    Voting Booth?
    My first memorable Perl project was:

    Results (85 votes). Check out past polls.