Shared Memory Example (Python, ctypes, VC++)

Srijit Kumar Bhadra srijit at yahoo.com
Sat Mar 19 09:51:08 EST 2005


Hello,
Here are code snippets to create and access shared memory in Python
with and without ctypes module.

With regards,
Srijit

Filename : SharedMemCreate.py

import msvcrt, mmap
from ctypes import *

FILE_MAP_ALL_ACCESS = 0xF001F
INVALID_HANDLE_VALUE = 0xFFFFFFFF
SHMEMSIZE = 256
PAGE_READWRITE = 0x04
szName = c_char_p("MyFileMappingObject_ctypes")
szMsg = "Message from Python(ctypes) process"

hMapObject = windll.kernel32.CreateFileMappingA(INVALID_HANDLE_VALUE,
None, PAGE_READWRITE, 0, SHMEMSIZE, szName)
if (hMapObject == 0):
    print "Could not open file mapping object"
    raise WinError()

pBuf = windll.kernel32.MapViewOfFile(hMapObject, FILE_MAP_ALL_ACCESS,
0, 0, SHMEMSIZE)
if (pBuf == 0):
    print "Could not map view of file"
    raise WinError()
else:
    memcpy = cdll.msvcrt.memcpy
    memcpy(pBuf, szMsg, len(szMsg))

shmem = mmap.mmap(0, 256, "MyFileMappingObject", mmap.ACCESS_WRITE)
shmem.write("Message from Python process")

msvcrt.getch()

windll.kernel32.UnmapViewOfFile(pBuf)
windll.kernel32.CloseHandle(hMapObject)
shmem.close()

Filename : SharedMemAccess.py

from ctypes import *
import mmap

FILE_MAP_ALL_ACCESS = 0xF001F
INVALID_HANDLE_VALUE = 0xFFFFFFFF
FALSE = 0
TRUE = 1
SHMEMSIZE = 256
szName = c_char_p("MyFileMappingObject")

hMapObject = windll.kernel32.OpenFileMappingA(FILE_MAP_ALL_ACCESS,
FALSE, szName)
if (hMapObject == 0):
    print "Could not open file mapping object"
    raise WinError()

pBuf = windll.kernel32.MapViewOfFile(hMapObject, FILE_MAP_ALL_ACCESS,
0, 0, 0)

if (pBuf == 0):
    print "Could not map view of file"
    raise WinError()
else:
    pBuf_str = cast(pBuf, c_char_p)
    print pBuf_str.value

windll.kernel32.UnmapViewOfFile(pBuf)
windll.kernel32.CloseHandle(hMapObject)

shmem = mmap.mmap(0, 256, "MyFileMappingObject_ctypes",
mmap.ACCESS_READ)
print shmem.read(64)
shmem.close()


Source code to access shared memory, created in Python, from VC++
program

// Cplusplus_SharedMemoryAccess.cpp : Defines the entry point for the
console application.
//

#include "stdafx.h"
using namespace std;

#include <windows.h>
#include <memory.h>
#include <conio.h>
#include <stdio.h>

#define SHMEMSIZE 256

HANDLE hMapObject = NULL;  // handle to file mapping
LPCTSTR pBuf;
TCHAR szName[]= TEXT("MyFileMappingObject");

int _tmain(int argc, _TCHAR* argv[])
{
   hMapObject = OpenFileMapping(
                   FILE_MAP_ALL_ACCESS,   // read/write access
                   FALSE,                 // do not inherit the name
                   szName                 // name of mapping object
		   );

   if (hMapObject == NULL || hMapObject == INVALID_HANDLE_VALUE) {
      cout << "Could not open file mapping object " << GetLastError()
<< endl;
      return 0;
   }
   pBuf = (LPTSTR) MapViewOfFile(hMapObject,    // handle to mapping
object
               FILE_MAP_ALL_ACCESS,             // read/write
permission
               0,
               0,
               SHMEMSIZE
	       );

   if (pBuf == NULL)
   {
      cout << "Could not map view of file "  << GetLastError() << endl;

      return 0;
   }

   cout << "pBuf = " << pBuf << endl;
   MessageBox(NULL, pBuf, TEXT("Process2"), MB_OK);

   UnmapViewOfFile(pBuf);
   CloseHandle(hMapObject);

   return 0;
}




More information about the Python-list mailing list