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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Srijit Kumar Bhadra

    #1

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

    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_AC CESS = 0xF001F
    INVALID_HANDLE_ VALUE = 0xFFFFFFFF
    SHMEMSIZE = 256
    PAGE_READWRITE = 0x04
    szName = c_char_p("MyFil eMappingObject_ ctypes")
    szMsg = "Message from Python(ctypes) process"

    hMapObject = windll.kernel32 .CreateFileMapp ingA(INVALID_HA NDLE_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_AC CESS,
    0, 0, SHMEMSIZE)
    if (pBuf == 0):
    print "Could not map view of file"
    raise WinError()
    else:
    memcpy = cdll.msvcrt.mem cpy
    memcpy(pBuf, szMsg, len(szMsg))

    shmem = mmap.mmap(0, 256, "MyFileMappingO bject", mmap.ACCESS_WRI TE)
    shmem.write("Me ssage from Python process")

    msvcrt.getch()

    windll.kernel32 .UnmapViewOfFil e(pBuf)
    windll.kernel32 .CloseHandle(hM apObject)
    shmem.close()

    Filename : SharedMemAccess .py

    from ctypes import *
    import mmap

    FILE_MAP_ALL_AC CESS = 0xF001F
    INVALID_HANDLE_ VALUE = 0xFFFFFFFF
    FALSE = 0
    TRUE = 1
    SHMEMSIZE = 256
    szName = c_char_p("MyFil eMappingObject" )

    hMapObject = windll.kernel32 .OpenFileMappin gA(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_AC CESS,
    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 .UnmapViewOfFil e(pBuf)
    windll.kernel32 .CloseHandle(hM apObject)

    shmem = mmap.mmap(0, 256, "MyFileMappingO bject_ctypes",
    mmap.ACCESS_REA D)
    print shmem.read(64)
    shmem.close()


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

    // Cplusplus_Share dMemoryAccess.c pp : 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("MyFileMap pingObject");

    int _tmain(int argc, _TCHAR* argv[])
    {
    hMapObject = OpenFileMapping (
    FILE_MAP_ALL_AC CESS, // 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(h MapObject, // handle to mapping
    object
    FILE_MAP_ALL_AC CESS, // 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(hMa pObject);

    return 0;
    }

  • Do Re Mi chel La Si Do

    #2
    Re: Shared Memory Example (Python, ctypes, VC++)

    Thanks.




    Comment

    Working...