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

Heya, I'm new at the monastery, and fairly new to perl. I have some background in PHP and C++.

I'm looking for a way to access the shared memory of non-perl win32 apps. I've tried installing Win32::IPC from CPAN, but nmake throws me a bunch of errors related to the fact that it doesn't know what struct a msqid_ds is. I looked through ppm, but no luck with the default repositories.

I need to be able to acquire and release a mutex, listen to two different events and write to and read from a portion of memory at a specified offset.

The specs for what I need to get at are here.

If anyone's interested, I'm trying to write a module for an Artificial Life program called Creatures. (More information here: www.gamewaredevelopment.co.uk)

Thanks in advance!

- nornagon

Replies are listed 'Best First'.
Re: Win32 Shared Memory
by BrowserUk (Patriarch) on May 15, 2004 at 08:03 UTC

    Probably your best bet would be to take a look at Win32::API (and/or Win32::API::Prototype which will take care of some of the details for you) and build your own perl wrappers around the required APis.


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
      Yer, I would if I knew what functions to import and from where :(

        The first page you linked to contains pretty much all you need to locate that information.

        Example:

        HANDLE memory_handle = OpenFileMapping(MAP_FILE_ALL_ACCESS, FALSE, "Creatures 3_mem");

        Googling for "OpenFileMapping MSDN" brings up http://msdn.microsoft.com/library/en-us/fileio/base/openfilemapping.asp as the first link. Pasting the prototype given at the top of the page into Win32::API::Prototype along with "Kernel32.dll" located near the bottom, and supply the constants and you should be able to get access to the shared memory segement (assuming you have whatever application creates it running on your machine).

        A similar process for each of the other APIs listed at your link would get you a long way towards your goal.


        Examine what is said, not who speaks.
        "Efficiency is intelligent laziness." -David Dunham
        "Think for yourself!" - Abigail
Re: Win32 Shared Memory
by Roger (Parson) on May 15, 2004 at 11:51 UTC
    You could have a look at the source code of Win32::MMF to see how this module implements shared memory for Perl under windows. The source code has code on creating and accessing shared memory blocks which you could borrow. The shared memory interface of the game is doing something just like that. I would just open the shared memory with ClaimNamespace and MapViewOfFile, and then use pack/unpack to retrieve data bound to the shared memory.