John M. Dlugosz has asked for the wisdom of the Perl Monks concerning the following question:

Is there an easy way to access shared memory in Win32 from Perl? Say I have a named shared memory "section" defined for communicating between applications. How can I efficiently, say, alias a scalar to this address?

In Win32, I could use file IO to access it too, but that would be my second choice.

—John

Replies are listed 'Best First'.
Re: Access shared memory from Perl?
by perrin (Chancellor) on Nov 07, 2003 at 19:21 UTC
    It's not that simple. You basically have to serialize your Perl data, write it to a section of memory, and unserialize it on the other side, being mindful of loxking and race conditions the whole time. This is what IPC::Shareable does.

    Perhaps if you explained what you're trying to do, we could make alternative suggestions. The easiest way to share data portably is by using MLDBM::Sync.

      I'm writing a Windows Service (like a unix deamon; runs with nobody logged on) to communicate with the PowerLinc USB interface to the X-10 powerline protocol.

      Any user program can ask the service to send a command, such as "A1 on" to turn on lamp A1. For this, the natural mechanism is a Windows Mailslot with record-oriented mode. Many programs can open handles simultainiusly, and mine will read from the single other end and see atomic entries in the queue. Now what about the other way? only my program will read from the device, getting status reports such as "A1 is on" and commands such as "A1 on" seen on the wire issued by other control panels. Either way, now it knows A1 is on and remembers that fact. When a user program wants to "read" he doesn't care about reading from the data stream to the device; rather, he wants to know something about the current state.

      So, I want to make the current state available (read-only) to any user program. A simple way to do this is with a memory-mapped file holding an array of simple structures.

      The state info is very simple. The actual device is either on/off or maybe has a dimming level. In addition, I'll have bits to indicate whether this ID is used at all, is a real X-10 module or a macro in the program, and things like that.

      It takes something like 0.86 second to transmit one command on the powerline, and the state is simple, so the serialization isn't a problem.

      I'll post more about it in another thread, probably on CUFP.

      —John

Re: Access shared memory from Perl?
by BrowserUk (Patriarch) on Nov 07, 2003 at 19:33 UTC
Re: Access shared memory from Perl?
by meetraz (Hermit) on Nov 07, 2003 at 20:00 UTC
    I would recommend Tie::Win32MemMap --- That way you don't have to worry about serializing perl data!
      Hmm, it looks like that uses the shared memory block in its own way as a keyed database, so is only good if all programs use this module (and are in Perl). I want to alias a SV's storage to the memory-mapped area, and fiddle with the contents using pack/unpack for example.